$arr = array(); // is this line needed?
$arr[] = 5;
我知道没有第一行就可以使用它,但是通常会在实践中包含它。
是什么原因?没有它是否不安全?
我知道您也可以这样做:
$arr = array(5);
但我说的是您需要一一添加项目的情况。
Answers:
如果您不声明新数组,并且创建/更新数组的数据由于任何原因而失败,那么以后尝试使用该数组的任何代码都会E_FATAL
因为该数组不存在。
例如,foreach()
如果未声明该数组且未添加任何值,将抛出错误。但是,如果数组只是空的,则不会发生任何错误,就像您声明它的情况一样。
foreach
示例,因为该示例以及错误被触发的事实显然取决于您所运行的PHP版本。
只是想指出,PHP文档arrays
实际上在文档中对此进行了讨论。
在PHP网站上,并附有代码段:
$arr[key] = value;
$arr[] = value;
// key may be an integer or string
// value may be any value of any type
“如果
$arr
还不存在,将创建它,因此这也是创建数组的另一种方法。”
但是,正如其他答案所说的那样……您确实应该为变量声明一个值,因为如果不这样做,可能会发生各种不良情况。
想想追随您的编码员!如果您只是看到$arr[] = 5
,那么$arr
不阅读范围中的所有前面的代码,您可能不知道会是什么。显式的$arr = array()
行使它清楚。
我强烈建议在添加值之前声明数组。除了上面提到的所有内容之外,如果数组在循环内,您可能会无意中将元素推入数组。我刚刚观察到这造成了代价高昂的错误。
//Example code
foreach ($mailboxes as $mailbox){
//loop creating email list to get
foreach ($emails as $email){
$arr[] = $email;
}
//loop to get emails
foreach ($arr as $email){
//oops now we're getting other peoples emails
//in other mailboxes because we didn't initialize the array
}
}
在使用数组之前不声明它会引起问题。我刚刚发现的一种经验是,我这样称呼此测试脚本:indextest.php?file = 1STLSPGTGUS这样可以正常工作。
//indextest.php?file=1STLSPGTGUS
$path['templates'] = './mytemplates/';
$file['template'] = 'myindex.tpl.php';
$file['otherthing'] = 'otherthing';
$file['iamempty'] = '';
print ("path['templates'] = " . $path['templates'] . "<br>");
print ("file['template'] = " . $file['template'] . "<br>");
print ("file['otherthing'] = " . $file['otherthing'] . "<br>");
print ("file['iamempty'] = " . $file['iamempty'] . "<br>");
print ("file['file'] = " . $file['file'] . "<br>");// should give: "Notice: Undefined index: file"
print ("file = " . $file);// should give: "Notice: Undefined index: file"
//the Output is:
/*
path['templates'] = ./mytemplates/
file['template'] = myindex.tpl.php
file['otherthing'] = otherthing
file['iamempty'] =
Notice: Undefined index: file in D:\Server\Apache24\htdocs\DeliverText\indextest.php on line 14
file['file'] =
Notice: Array to string conversion in D:\Server\Apache24\htdocs\DeliverText\indextest.php on line 15
file = Array
*/
现在,我只需要我购买的另一个脚本中的文件,就在我的顶部,我们可以看到数组$ file的值完全错误,而数组$ path正确:“ checkgroup.php”是有罪的。
//indextest.php?file=1STLSPGTGUS
require_once($_SERVER['DOCUMENT_ROOT']."/IniConfig.php");
$access = "PUBLIC";
require_once(CONFPATH . "include_secure/checkgroup.php");
$path['templates'] = './mytemplates/';
$file['template'] = 'myindex.tpl.php';
$file['otherthing'] = 'otherthing.php';
$file['iamempty'] = '';
print ("path['templates'] = " . $path['templates'] . "<br>");
print ("file['template'] = " . $file['template'] . "<br>");
print ("file['otherthing'] = " . $file['otherthing'] . "<br>");
print ("file['iamempty'] = " . $file['iamempty'] . "<br>");
print ("file['file'] = " . $file['file'] . "<br>");
print ("file = " . $file);
//the Output is:
/*
path['templates'] = ./mytemplates/
file['template'] = o
file['otherthing'] = o
file['iamempty'] = o
file['file'] = o
file = oSTLSPGTGUS
*/
初始化数组之前,那就没问题!
//indextest.php?file=1STLSPGTGUS
require_once($_SERVER['DOCUMENT_ROOT']."/IniConfig.php");
$access = "PUBLIC";
require_once(CONFPATH . "include_secure/checkgroup.php");
$path = array();
$file = array();
$path['templates'] = './mytemplates/';
$file['template'] = 'myindex.tpl.php';
$file['otherthing'] = 'otherthing.php';
$file['iamempty'] = '';
print ("path['templates'] = " . $path['templates'] . "<br>");
print ("file['template'] = " . $file['template'] . "<br>");
print ("file['otherthing'] = " . $file['otherthing'] . "<br>");
print ("file['iamempty'] = " . $file['iamempty'] . "<br>");
print ("file['file'] = " . $file['file'] . "<br>");
print ("file = " . $file);
//the Output is:
/*
path['templates'] = ./mytemplates/
file['template'] = myindex.tpl.php
file['otherthing'] = otherthing.php
file['iamempty'] =
file['file'] =
file = Array
*/
这就是我意识到初始化变量的重要性,因为我们永远不知道以后会遇到什么问题,而只是为了节省时间,最终可能会浪费更多。我希望这会对像我这样不专业的人有所帮助。
同意@djdy,这只是我想发布的一种选择:
<?php
// Passed array is empty, so we'll never have $items variable available.
foreach (array() AS $item)
$items[] = $item;
isset($items) OR $items = array(); // Declare $items variable if it doesn't exist
?>
$foo = array()
,它不是将字符串放入数组等)。