使用类似数组变量之前分配阵列() [英] Assigning array() before using a variable like array

查看:143
本文介绍了使用类似数组变量之前分配阵列()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个合适的和解释性冠军,但我做不到,我会尽量解释什么,我问在这里:

I tried to find a proper and explanatory title but I couldn't and I will try to explain what I am asking here:

一般来说,如果你没有一个空数组赋值给一个变量,你就可以开始分配值,以这样的指标:

Normally if you don't assign an empty array to a variable, you can start assign values to indexes like this:

$hello["world"] = "Hello World";
...
echo $hello["world"];

但我总是会遇到这样定义的:

but I always encounter such definition:

$hello = array() //assigning an empty array first
$hello["hello"] = "World";
...
echo $hello["hello"];

为什么用了很多。是否有性能提升或者一些与第二个?

Why is it used a lot. Is there a performance gain or something with the second one?

感谢。

推荐答案

两个原因:


  • 更好的可读性(你知道数组在这一点初始化)

  • 安全 - 以 register_globals的一个系统上运行时启用用户可以添加例如你好[哞] =东西来查询字符串和数组会已与此进行初始化。 $你好=阵列(); 覆盖此值虽然因为新数组创建

  • Better readability (you know the array is initialized at this point)
  • Security - when running on a system with register_globals enabled a user could add e.g. hello[moo]=something to the query string and the array would already be initialized with this. $hello = array(); overwrites this value though since a new array is created.

这篇关于使用类似数组变量之前分配阵列()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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