$foo["bar"] = 1; Can I ask PHP to complain if $foo doesn't exist? -


take line of php:

$foo["bar"] = 1; 

i php throw exception if $foo doesn't exist. right now, doesn't throw exception, or print error or warning display_errors set 1 , error_reporting called e_all. instead, creates array $foo , sets $foo["bar"] 1, though variable $foo did not exist beforehand.

is there declare(strict_types=1); enable checking this?

the reason want can more detect typos when accidentally misspell variable name.

unfortunately, setting array command. why php throw exception if setting this?

it's assigning value variable , asking why did php assign value variable?

$foo["bar"] = 1;  print_r($foo); // prints following:  // array ( [bar] => 1 ) 

the correct way of checking be:

if(isset($foo)) {   $foo['bar'] = 1; } else {   // if $foo not exist or null } 

hope helps! in short answer question no: there isn't way make php throw exception or print warning in example.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -