为什么没有参数的 PHP 函数需要括号?

发布于 2024-09-01 12:26:32 字数 262 浏览 7 评论 0原文

我只是花了 3 个小时想知道为什么我无法启动会话,然后意识到我使用了:

session_start;

当我应该使用:

session_start();

我根本没有收到任何错误消息!

也许那时,我想,这是区分函数和变量的一种懒惰方法 - 但后来想起它不能,因为变量需要 $

谁能告诉我为什么需要括号,以及为什么在不需要括号时不会给出错误没用过?

I just spent 3 hours wondering why I couldn't start a session, then realised that I used:

session_start;

when I should have used:

session_start();

I received no error messages at all!

Perhaps then, I thought, it's a lazy way to differentiate functions from variables - but then remembered that it can't be as variables require a $

Can anyone tell me why parentheses are required then, and also why no error is given when they aren't used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空城缀染半城烟沙 2024-09-08 12:26:32

我收到通知(已将 error_reporting 设置为 E_ALL):

注意:使用未定义的常量 session_start - 在 /t.php 第 5 行假定为“session_start”

函数总是需要括号,否则(如您所见)您无法区分函数和常量之间的区别。由于代码应该被编写成可以被阅读它的人理解,因此不允许读者查看它是常量还是函数调用,这使得阅读和调试变得更加困难。

我认为这是一个通知而不是一个错误,因为向后兼容,因为一些较旧的代码倾向于使用不带引号的字符串访问数组,如果不存在具有该名称的常量,PHP 会将其视为带引号的字符串。

$myArray[myString] = 25;

I get the notice (Having set error_reporting to E_ALL):

Notice: Use of undefined constant session_start - assumed 'session_start' in /t.php on line 5

A function always needs the parentheses as else (as you can see) you can't tell the difference between a function and a constant. As code should be written so it can be understood by the human reading it, not allowing the reader to see if it is a constant or function call makes it that bit harder to read and debug.

I assume it is a notice rather than an error error due to backwards compatibility in that some older code tends to access arrays using unquoted strings which PHP would treat as quoted strings if there wasn't a constant with that name.

$myArray[myString] = 25;
泪眸﹌ 2024-09-08 12:26:32

如果函数没有括号,则无法区分函数和常量...但奇怪的是它不会抛出错误...您是否在 php.ini 文件中关闭了错误?

You can not make a difference between function and a constant if function doesn't have parantheses... But it is strange that it doesn't throw the errors...have you turned errors off in your php.ini file?

深白境迁sunset 2024-09-08 12:26:32

括号是必需的,因为 PHP 的语言规范是这么说的(尽管似乎没有找到该语言的明确的正式规范)。

当 PHP 遇到未知标识符时,它会输出一个 E_NOTICE 错误,让您知道使用了未知标识符,然后假设您打算将该标识符用作字符串

<?php
$foo = unknown_identifier;
echo 'Now printing: ' . $foo; // prints 'Now printing: unknown_identifier' (PHP 5.2.6)
?>

您没有看到任何错误的原因可能是因为 E_NOTICE 错误低于您的错误报告阈值。您可以使用 error_reporting 指令更改此阈值:

<?php
error_reporting(E_ALL);

$foo = unknown_identifier;
echo 'Now printing: ' . $foo;
?>

这将输出 Yacoby 上面发布的“Notice”错误消息。

请注意,E_ALL 错误报告指令并不具有误导性。事实上,包括 PHP 5.x 中的所有报告级别。在 PHP 5 中,您需要使用 E_ALL | E_STRICT 启用所有错误消息。

——编辑:被很多人打败了。

The parentheses are required because PHP's language specification says so (even though there appears to be no explicit formal specification of the language to be found).

What PHP does when it encounters an unknown identifier is output an E_NOTICE error to let you know that an unknown identifier was used, and then assumes that you meant to use that identifier as a string.

<?php
$foo = unknown_identifier;
echo 'Now printing: ' . $foo; // prints 'Now printing: unknown_identifier' (PHP 5.2.6)
?>

The reason you're not seeing any error is likely because E_NOTICE errors are below your error reporting threshold. You can change this threshold using the error_reporting directive:

<?php
error_reporting(E_ALL);

$foo = unknown_identifier;
echo 'Now printing: ' . $foo;
?>

This will output the "Notice" error message as posted above by Yacoby.

Note that, misleadingly, the E_ALL error reporting directive does not in fact include all reporting levels in PHP 5.x. In PHP 5, you need to use E_ALL | E_STRICT to enable all error messages.

-- edit: beaten to the punch by many people.

盛装女皇 2024-09-08 12:26:32

我所知道或曾经使用过的所有语言都使用括号来描述函数,无论它们不带参数(void)还是有参数列表(可选或不)。
你会习惯的。也许这是 C 语言及其所有函数原型等的后遗症。

话虽如此,PHP 中的 echo 函数是一个奇怪的函数,因为它不完全是一个函数,而是一个语句 - 想想看。

看看 http://en.wikipedia.org/wiki/Function(computer_science)

All the languages I know or have ever used use brackets to describe functions whether they take no parameters (void) or have a parameter list (optional or not).
You get used to it. Maybe it's a hangover from C with all it's function prototypes and all.

Having said that, the echo functon in PHP is a weird one as it's not quite a function but it's a statement - go figure.

Have a look at http://en.wikipedia.org/wiki/Function(computer_science)

山人契 2024-09-08 12:26:32

() 表示要调用该函数。没有它,您就可以传递引用。例如(PHP 5.3):

$callable = session_start;
$callable();

() denotes that the function is to be called. Without it, you are able to pass the reference around. For example (PHP 5.3):

$callable = session_start;
$callable();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文