arrays - PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" -


i running php script, , keep getting errors like:

notice: undefined variable: my_variable_name in c:\wamp\www\mypath\index.php on line 10

notice: undefined index: my_index c:\wamp\www\mypath\index.php on line 11

line 10 , 11 looks this:

echo "my variable value is: " . $my_variable_name; echo "my index value is: " . $my_array["my_index"]; 

what these errors mean?

why appear of sudden? used use script years , i've never had problem.

what need fix them?

this general reference question people link duplicate, instead of having explain issue on , on again. feel necessary because real-world answers on issue specific.

related meta discussion:

notice: undefined variable

from vast wisdom of php manual:

relying on default value of uninitialized variable problematic in case of including 1 file uses same variable name. major security risk register_globals turned on. e_notice level error issued in case of working uninitialized variables, not in case of appending elements uninitialized array. isset() language construct can used detect if variable has been initialized. additionally , more ideal solution of empty() since not generate warning or error message if variable not initialized.

from php documentation:

no warning generated if variable not exist. means empty() concise equivalent !isset($var) || $var == false.

this means use empty() determine if variable set, , in addition checks variable against following, 0,"",null.

example:

$o = []; @$var = ["",0,null,1,2,3,$foo,$o['myindex']]; array_walk($var, function($v) {     echo (!isset($v) || $v == false) ? 'true ' : 'false';     echo ' ' . (empty($v) ? 'true' : 'false');     echo "\n"; }); 

test above snippet in 3v4l.org online php editor

although php not require variable declaration, recommend in order avoid security vulnerabilities or bugs 1 forget give value variable used later in script. php in case of undeclared variables issue low level error, e_notice, 1 not reported default, manual advises allow during development.

ways deal issue:

  1. recommended: declare variables, example when try append string undefined variable. or use isset() / !empty() check if declared before referencing them, in:

    //initializing variable $value = ""; //initialization value; examples              //"" when want append stuff later              //0  when want add numbers later //isset() $value = isset($_post['value']) ? $_post['value'] : ''; //empty() $value = !empty($_post['value']) ? $_post['value'] : ''; 

    this has become cleaner of php 7.0, can use null coalesce operator:

    // null coalesce operator - no need explicitly initialize variable. $value = $_post['value'] ?? ''; 
  2. set custom error handler e_notice , redirect messages away standard output (maybe log file):

    set_error_handler('myhandlerforminorerrors', e_notice | e_strict) 
  3. disable e_notice reporting. quick way exclude e_notice is:

    error_reporting( error_reporting() & ~e_notice ) 
  4. suppress error @ operator.

note: it's recommended implement point 1.

notice: undefined index / undefined offset

this notice appears when (or php) try access undefined index of array.

ways deal issue:

  1. check if index exists before access it. can use isset() or array_key_exists():

    //isset() $value = isset($array['my_index']) ? $array['my_index'] : ''; //array_key_exists() $value = array_key_exists('my_index', $array) ? $array['my_index'] : ''; 
  2. the language construct list() may generate when attempts access array index not exist:

    list($a, $b) = array(0 => 'a'); //or list($one, $two) = explode(',', 'test string'); 

two variables used access 2 array elements, there 1 array element, index 0, generate:

notice: undefined offset: 1

$_post / $_get / $_session variable

the notices above appear when working $_post, $_get or $_session. $_post , $_get have check if index exists or not before use them. $_session have make sure have session started session_start() , index exists.

also note 3 variables superglobals. means need written in uppercase.

related:


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -