数组是否应该在使用前声明? [英] Should an array be declared before using it?

查看:36
本文介绍了数组是否应该在使用前声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码 A 或 B 哪个更好:

Which one is better practice, code A or B:

答:

    $array = array();
    $array[] = 'value';

B(使用数组而不将其声明为数组):

B ( use the array without declaring it an array ):

    $array[] = 'value'

推荐答案

最佳实践是声明它.

原因:如果由于某种原因有人在使用之前打开了 register_globals 并设置了 $array ,则最终可能会得到奇怪的结果.如果你声明它,你总是确定你有一个空数组.

Reason: if for some reason someone turned on register_globals and $array is set before you use it, you can end up with strange results. If you declare it, you are always sure you have an empty array.

这篇关于数组是否应该在使用前声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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