Is it necessary to declare null variables manually in php? -
i aware php considers undefined variables null. despite this, when want use 1 undefined variable throws e_notice error saying variable undefined. prevent this, should fix e_notice setting variables manually null?
for example:
class myclass{ private $var1; private $var2; public function __construct($settings){ $allowedkeys = array("var1","var2"); foreach($allowedkeys $key => $value){ if(!isset($settings[$value])){ $settings[$value] = null; } } $this->var1 = $settings['var1']; $this->var2 = $settings['var2']; } }
you have 4 options prevent e_notice:
one set variable either null, string, integer before use variable. i.e.:
$variable = null; $variable = ''; $variable = 0; $variable = []; ... if(empty($variable)) { // situation }
the other check if variable exists. did in 1 line:
if(isset($variable)){ ... }
third 1 turn off e_notice in scipt:
error_reporting(e_all & ~e_notice);
forth 1 turn off in php.ini file (though not recommend this):
error_reporting = e_all & ~e_notice // must find line in php.ini
Comments
Post a Comment