难道一个PHP数组需要在使用前声明? [英] Does a PHP array need to be declared before use?

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

问题描述

在写一个最新的应用程序,我不小心开始填写一个数组我宣布它。

 的error_reporting(E_ALL);
$数组['值'] ='测试字符串;

我用E_ALL错误报告,并没有抛出一个错误。它是否正确?如果是的话,是否有与声明数组值,而从来没有声明实际阵列的任何问题?
也许它只是不遵循良好的编程标准。


解决方案

  

在写一个最新的应用程序我
  意外启动填充数组
  之前,我曾宣布它。


PHP是弱类型语言。您的语句:

  $数组['值'] ='测试字符串;

是一个关联数组的隐式声明(通过分配)。因此,通知将不会生成。

不过,如果你写:

 回声$阵列['值'];

这是assigment之前,那么你会收到一封未定义变量的通知。

While writing a recent application I accidentally started filling an array before I had declared it.

error_reporting ( E_ALL);  
$array['value'] = 'Test string';

I use E_ALL error reporting and an error was not thrown. Is this correct? And if so, are there any issues with declaring array values whilst never declaring the actual array? Perhaps it just doesn't follow good programming standards.

解决方案

While writing a recent application I accidentally started filling an array before I had declared it.

PHP is a weakly typed language. Your statement:

$array['value'] = 'Test string';

is an implicit declaration (through assignment) of an associative array. So, a notice will not be generated.

However, if you were to write:

echo $array['value'];

before an assigment, then you'll receive an Undefined variable notice.

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

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