在用[]添加值之前是否需要声明PHP数组?
$arr = array(); // is this line needed?
$arr[] = 5;
我知道它不需要第一行就可以工作,但它经常包含在实践中。
原因是什么?没有它就不安全吗?
我知道你也可以这样做:
$arr = array(5);
但我说的是你需要一项一项地添加项目的情况。
$arr = array(); // is this line needed?
$arr[] = 5;
I know it works without the first line, but it's often included in practice.
What is the reason? Is it unsafe without it?
I know you can also do this:
$arr = array(5);
but I'm talking about cases where you need to add items one by one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您没有声明新数组,并且创建/更新数组的数据由于任何原因而失败,那么以后尝试使用该数组的任何代码都将
E_FATAL
,因为该数组不存在。例如,如果未声明数组且未向其中添加任何值,
foreach()
将引发错误。但是,如果数组只是空的,则不会发生错误,就像您声明它的情况一样。If you don't declare a new array, and the data that creates / updates the array fails for any reason, then any future code that tries to use the array will
E_FATAL
because the array doesn't exist.For example,
foreach()
will throw an error if the array was not declared and no values were added to it. However, no errors will occur if the array is simply empty, as would be the case had you declared it.只是想指出 关于数组的 PHP 文档< /a> 实际上在文档中讨论了这一点。
来自 PHP 站点,附带代码片段:
但是,正如其他答案所述......您确实应该为变量声明一个值,因为如果不这样做,可能会发生各种不好的事情。
Just wanted to point out that the PHP documentation on
arrays
actually talks about this in documentation.From the PHP site, with accompanying code snippet:
But, as the other answers stated...you really should declare a value for your variables because all kind of bad things can happen if you don't.
Php 是一种松散类型的语言。这是完全可以接受的。
Php is a loosely typed language. It's perfectly acceptable.
想想那些追随你的程序员吧!如果您只看到
$arr[] = 5
,则在不阅读范围内所有前面的代码的情况下,您不知道$arr
可能是什么。明确的$arr = array()
行使这一点变得清晰。Think of the coders who come after you! If you just see
$arr[] = 5
, you have no idea what$arr
might be without reading all the preceding code in the scope. The explicit$arr = array()
line makes it clear.这只是一个很好的做法。假设您要在循环内附加到数组(相当常见的做法),但然后在所述循环之外访问数组。如果没有数组声明,如果您从未进入循环,您的代码就会抛出错误。
it's just good practice. Let's say you were appending to your array inside a loop (fairly common practice), but then accessing the array outside of said loop. Without an array declaration, your code would throw errors if you never made it into the loop.
我强烈建议在添加值之前声明数组。除了上面提到的所有内容之外,如果数组位于循环内,您可能会无意中将元素推入数组中。我刚刚观察到这会造成一个代价高昂的错误。
I strongly recommend to declare the array before adding values. Besides everything mentioned above, if the array is inside a loop you might unintentionally push elements into your array. I just observed this creating a costly bug.
在使用数组之前不声明它确实会导致问题。
我刚刚发现的一种体验,我这样调用这个测试脚本:indextest.php?file=1STLSPGTGUS
这按预期工作。
现在我只需要一个来自我购买的另一个脚本的文件,在我的顶部,我们可以看到数组 $file 的值完全错误,而数组 $path 则正常:
“checkgroup.php”是有罪的。
之前初始化数组就没有问题了!
这就是我意识到初始化变量的重要性,因为我们永远不知道以后可能会遇到什么问题,而仅仅为了节省时间,我们最终可能会浪费更多时间。
希望对像我这样非专业人士有所帮助。
By not declaring an array before using it can really cause problems.
One experience I just found, I called this test script like this: indextest.php?file=1STLSPGTGUS
This works as expected.
Now I will just require a file, from another script I bought, at the top of mine and we can see how values are completly wrong for the array $file while array $path is OK:
"checkgroup.php" is the guilty one.
Initialising the array before, then no problem!
That's how I realised how important it is to initialise variables as we never know what problem we might end up with later, and just for wanting to save time we might end up wasting even more at the end.
I hope this will be helpful for those like me who are not professional.
老问题,但我分享这个,因为这是一个用例,如果未声明数组,代码会更简单。
假设您有一个对象列表,需要根据其属性之一建立索引。
Old question but I share this as this is one use case where code is simpler if arrays are not declared.
Say you have a list of objects you need to index on one of their properties.
这取决于你的错误检查。如果您在严格上有错误报告,它会给您一个通知,但从技术上讲,即使没有它,它仍然可以工作。
It depends on your error checking. If you have error reporting on strict it'll give you a notice but it should still technically work without it.
当您需要它作为全局变量或想要在不同的函数中一次又一次地重用相同的数组时,它很有用
Its good in case when you need it as a global variable or want to reuse the same array again and again in different functions
这是你的代码
工作正常!直到有人在你迷人的代码之前声明相同的变量名
糟糕!
这是一种可能的(有时不可预测的)情况,所以需要
$var = array()
输出
This is your code
Works fine! until someone declares the same variable name before your charming code
Oops!
This is one possible (sometimes unpredictable) case, so yes
$var = array()
is neededOutput
对于
foreach
循环,如果您不确定数据,那么您可以执行以下操作:如果未设置
$users
(空合并会isset($users)
)然后你会得到空数组[]
,因此PHP不会循环foreach
,因为没有任何东西可以循环 - 没有错误、警告或通知。我不同意评论/答案中的一些观点,我认为您不应该仅仅为了它而简单地声明空数组或初始化变量,作为某种安全网。在我看来,这种方法是糟糕的编程。必要时明确执行。
老实说,如果您必须初始化一个空数组,请考虑代码是否可以更好地结构化,以及稍后如何检查数据等等。
下面的代码是毫无意义的,它没有显示“意图”,它只会让人们困惑为什么要初始化它(充其量只是一些毫无意义的阅读和处理):
第二行还声明了新数组并且永远不会失败。
For
foreach
loops if you're unsure of the data, then you can do this:If
$users
is not set (null coalesce doesisset($users)
) then you'll get the empty array[]
and thus PHP won't loop theforeach
as there's nothing to loop - no errors, warnings, or notices.I disagree with some in the comments/answers, I don't think you should simply declare empty arrays or initialise variables just for the sake of it, as some kind of safety net. Such approaches is bad programming in my opinion. Do it explicitly when it's necessary.
And to be honest, if you have to initialise an empty array, consider if the code could be better structured, how you check for data later on or whatever.
The following code is pointless, it doesn't show "intent" it can just confuse people as to why it's initialised (at best it's just something pointless to read and process):
The 2nd line also declares the new array and will never fail.
同意@djdy,我只想发布一种替代方案:
Agree with @djdy, just one alternative I'd love to post: