为什么没有参数的PHP函数需要括号? [英] Why does a PHP function with no parameters require parentheses?

查看:136
本文介绍了为什么没有参数的PHP函数需要括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚花了3个小时想知道为什么我不能开始一个会议,然后意识到我用过:

  session_start; 

当我应该使用时:

 在session_start(); 

我完全没有收到任何错误讯息!

<可能那么,我想,这是一种懒惰的方式来区分函数和变量 - 但后来记住它不能作为变量需要一个$



任何人都可以告诉我为什么要求括号,那么为什么没有错误时,他们不使用? 解决方案

我得到通知(已将error_reporting设置为 E_ALL ):
$ b


注意:使用未定义的常量session_start - 在第5行的/t.php中假设'session_start'

一个函数总是需要括号作为else(如你所见)你不能分辨函数和常量之间的区别。由于代码应该被写入,所以人们可以通过阅读它来理解它,而不是让读者看到它是否是一个常量或函数调用,这使得阅读和调试有点难。



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

  $ myArray [myString] = 25; 


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?

解决方案

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;

这篇关于为什么没有参数的PHP函数需要括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆