Does a PHP array need to be declared before use?

后端 未结 3 2029
闹比i
闹比i 2021-01-17 19:09

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

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


        
3条回答
  •  温柔的废话
    2021-01-17 20:01

    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.

提交回复
热议问题