Notice: Undefined variable

  • Default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name.
  • It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array.
  • isset() language construct can be used to detect if a variable has been already initialized.
  • Additionally and more ideal is the solution of empty() since it does not generate a warning or error message if the variable is not initialized.
javascript code
$o = [];
$var = ["",0,null,1,2,3,$foo,$o['myIndex']];
array_walk($var,function($v)
{
if(!isset($v) || $v == false)
{
echo "empty\n";
}
if(empty($v))
{
echo "empty\n";
}
});
  • Although PHP does not require variable declaration, it does recommend it in order to avoid some security vulnerabilities or bugs where one would forget to give a value to a variable that he will use later in the script.
    • What PHP does in the case of undeclared variables is issue a very low level error, E_NOTICE, one that is not even reported by default, but the Manual advises to allow during development.

Ways to deal with the issue:

    1. Recommended: Declare your variables, for example when you try to append a string to an undefined variable. Or use isset() / !empty() to check if they are declared before referencing them, as in:
javascript code
//Initializing variable
$value = ""; //Initialization value; Examples
//"" When you want to append stuff later
//0 When you want to add numbers later
//isset()
$value = isset($_POST['value']) ? $_POST['value'] : '';
//empty()
$value = !empty($_POST['value']) ? $_POST['value'] : '';
  • Set a custom error handler for E_NOTICE and redirect the messages away from the standard output.
php code
set_error_handler('myHandlerForMinorErrors', E_NOTICE | E_STRICT) 
  • Disable E_NOTICE from reporting. A quick way to exclude just E_NOTICE is:
php code
error_reporting( error_reporting() & ~E_NOTICE ) 
[ad type=”banner”]

How To Fix PHP Error Notice: Undefined variable: index

Below are the steps to fix How To Fix PHP Error Notice: Undefined variable: index

  1. Open up wamp server -> php->php.ini
  2. Open up php.ini -> search for error_reporting = E_ALL and replace this with error_reporting = E_ALL & ~E_NOTICE
  3. Restart your wamp server

Notice: Undefined Index

  • Happens when you try to access an array by a key that does not exist in the array.
  • A typical example for an Undefined Index notice.
javascript code
$data = array('foo' => '42', 'bar');
echo $data['spinach'];
echo $data[1];
  • Both spinach and 1 do not exist in the array, causing an E_NOTICE to be triggered.
  • The solution is to make sure the index or offset exists prior to accessing that index.
  • This may mean that you need to fix a bug in your program to ensure that those indexes do exist when you expect them to. Or it may mean that you need to test whether the indexes exist using array_key_exists [http://php.net/array_key_exists] or isset[http://php.net/isset]:
javascript code
$data = array('foo' => '42', 'bar');
if (array_key_exists('spinach', $data))
{
echo $data['spinach'];
}
else
{
echo 'No key spinach in array';
}
  • If you have code like:
php code
<?php echo $_POST['message']; ?>
<form method="post" action="">
<input type="text" name="message">
...
  • then $_POST[‘message’] will not be set when this page is first loaded and you will get the above error.
  • Only when the form is submitted and this code is run a second time will the array index exist. You typically check for this with:
javascript code
if ($_POST)  ..  // if the $_POST array is not empty
// or
if ($_SERVER['REQUEST_METHOD'] == 'POST') ..
[ad type=”banner”]

Notice: Undefined index / Undefined offset

  • This notice appears when you (or PHP) try to access an undefined index of an array.
  • Ways to deal with the issue:

Check if the index exists before you access it. For this you can use isset() or array_key_exists()

javascript code
//isset()
$value = isset($array['my_index']) ? $array['my_index'] : '';
//array_key_exists()
$value = array_key_exists('my_index', $array) ? $array['my_index'] : '';
  • The language construct list()may generate this when it attempts to access an array index that does not exist:
javascript code
list($a, $b) = array(0 => 'a');
//or
list($one, $two) = explode(',', 'test string');
  • Two variables are used to access two array elements, however there is only one array element, index 0, so this will generate:
  • Notice: Undefined offset: 1

$_POST / $_GET / $_SESSION variable:

  • The notices above appear often when working with $_POST$_GET or $_SESSION. For $_POST and $_GET you just have to check if the index exists or not before you use them.
  • For $_SESSION you have to make sure you have the session started with session_start() and that the index also exists.
[ad type=”banner”]

Categorized in: