增加在PHP无法初始化数组值 [英] Increasing not initialize array value in php
问题描述
海兰
我有一个foreach循环,增加了数组键到另一个阵列。我想知道这是否是安全的增加(与++)和未初始化的元素。
此刻我的code是:
Hy I have a foreach loop that adds array keys to another array. I wanted to know if it' safe to increment (with ++) and uninitialize element. At the moment my code is:
foreach($SociBdP as $id=>$socio)
{
if(!isset($provenienza[$option_name]))
$provenienza[$option_name]=0;
$provenienza[$option_name]++;
}
我想知道,如果它的安全做
I wanted to know if it's safe to do
foreach($SociBdP as $id=>$socio)
{
$provenienza[$option_name]++;
}
,或者如果存在这样的风险(如在C ++),该阵列的默认值不为0
or if there is a risk (like in c++) that the default value of the array isn't 0
推荐答案
虽然这是一个的记录的行为你可以信任:
While it's a documented behaviour you can trust:
这是没有必要初始化在PHP变量但它是一个
非常好的做法。未初始化的变量有一个默认值
根据上下文其类型在它们所使用的 - 布尔
默认为FALSE,整形和浮点默认为零,字符串(例如
回声使用)设置为空字符串和数组成为空
数组。
It is not necessary to initialize variables in PHP however it is a very good practice. Uninitialized variables have a default value of their type depending on the context in which they are used - booleans default to FALSE, integers and floats default to zero, strings (e.g. used in echo) are set as an empty string and arrays become to an empty array.
...这也美元,以通知的好处p $ pvents你,因为你需要降低下来您的错误报告一>设置,这样他们就不会在开发阶段显示出来:
... it also prevents you from taking benefit of notices since you need to lower down your error reporting settings so they don't show up in the development phase:
var_dump($foo);
Notice: Undefined variable: foo in D:\tmp\borrame.php on line 3
NULL
通告常常被看作是由一个新手,但烦恼,他们实际上是当场傻错别字一个了不起的工具。
Notices are often seen as an annoyance by newbies but they're actually a terrific tool to spot silly typos.
这篇关于增加在PHP无法初始化数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!