是否有必要在 PHP 中初始化/声明变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30955639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it necessary to Initialize / Declare variable in PHP?
提问by Omer
This question's purpose is to only gain knowledge or information for me and many like me.
这个问题的目的只是为我和许多像我一样的人获取知识或信息。
So my question is:
所以我的问题是:
Is it necessary to Initialize / Declare a variable before a loop or a function?
是否有必要在循环或函数之前初始化/声明变量?
Asking this question is for my confusion because whether I initialize / declare variable before or not my code still works.
问这个问题是为了让我感到困惑,因为我是否在代码之前初始化/声明变量仍然有效。
I'm sharing a demo code for what I actually mean:
我正在分享一个演示代码,说明我的实际意思:
$cars = null;
foreach ($build as $brand) {
$cars .= $brand . ",";
}
echo $cars;
OR
或者
foreach ($build as $brand) {
$cars .= $brand . ",";
}
echo $cars;
Both piece of code works same for me, so is necessary to Initialize / Declare a variable at the beginning?
这两段代码对我来说都是一样的,所以有必要在开始时初始化/声明一个变量吗?
回答by Alexander
PHP does not require it, but it is a good practice to always initialize your variables.
PHP 不需要它,但始终初始化变量是一个好习惯。
If you don't initialize your variables with a default value, the PHP engine will do a type cast depending on how you are using the variable. This sometimes will lead to unexpected behaviour.
如果您不使用默认值初始化变量,PHP 引擎将根据您使用变量的方式进行类型转换。这有时会导致意外行为。
So in short, in my opinion, always set a default value for your variables.
简而言之,在我看来,始终为变量设置默认值。
P.S. In your case the value should be set to "" (empty string), instead of null, since you are using it to concatenate other strings.
PS 在您的情况下,该值应设置为“”(空字符串),而不是 null,因为您使用它来连接其他字符串。
Edit
编辑
As others (@n-dru) have noted, if you don't set a default value a notice will be generated.
正如其他人 (@n-dru) 所指出的,如果您未设置默认值,则会生成一条通知。
回答by n-dru
You had better assign it an empty string $cars = '';
, otherwise (in case you have error reporting on) you should see a notice:
你最好给它分配一个空字符串$cars = '';
,否则(如果你有错误报告)你应该看到一个通知:
Notice: Undefined variable: cars
注意:未定义变量:汽车
PHP will assume it was empty and the resultant string will be the same, but you should prefer not to cause an extra overhead caused by a need of logging that Notice. So performance-wise it is better to assign it empty first.
PHP 会假设它是空的并且结果字符串是相同的,但是您不应该因为需要记录该通知而导致额外的开销。因此,性能方面最好先将其分配为空。
Besides, using editors like Aptana, etc, you may wish to press F3
to see where given variable originated from. And it is so comfortable to have it initialized somewhere. So debugging-wise it is also better to have obvious place of the variable's birth.
此外,使用 Aptana 等编辑器,您可能希望按F3
查看给定变量的来源。在某处初始化它是如此舒适。因此,在调试方面,最好有变量出生的明显位置。
回答by Nicole
It depends: If you declare a variable outside a function, it has a "global scope", that means it can only be accessed outside of a function.
这取决于:如果您在函数外部声明一个变量,它具有“全局范围”,这意味着它只能在函数外部访问。
If a variable is declared inside a function, it has a "local scope" and can be used only inside that function. (http://www.w3schools.com/php/php_variables.asp)
如果在函数内部声明了变量,则它具有“局部作用域”并且只能在该函数内部使用。( http://www.w3schools.com/php/php_variables.asp)
But it seems that the variable "cars" that you defined outside the function works for your function even without the global keyword...
但是,即使没有 global 关键字,您在函数外部定义的变量“cars”似乎也适用于您的函数......