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.
[pastacode lang=”javascript” manual=”%24o%20%3D%20%5B%5D%3B%0A%24var%20%3D%20%5B%22%22%2C0%2Cnull%2C1%2C2%2C3%2C%24foo%2C%24o%5B’myIndex’%5D%5D%3B%0Aarray_walk(%24var%2Cfunction(%24v)%0A%7B%0Aif(!isset(%24v)%20%7C%7C%20%24v%20%3D%3D%20false)%20%0A%7B%0Aecho%20%22empty%5Cn%22%3B%0A%7D%0Aif(empty(%24v))%20%0A%7B%0Aecho%20%22empty%5Cn%22%3B%0A%7D%0A%7D)%3B%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • 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:
[pastacode lang=”javascript” manual=”%2F%2FInitializing%20variable%0A%24value%20%3D%20%22%22%3B%20%2F%2FInitialization%20value%3B%20Examples%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%22%22%20When%20you%20want%20to%20append%20stuff%20later%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F0%20%20When%20you%20want%20to%20add%20numbers%20later%0A%2F%2Fisset()%0A%24value%20%3D%20isset(%24_POST%5B’value’%5D)%20%3F%20%24_POST%5B’value’%5D%20%3A%20”%3B%0A%2F%2Fempty()%0A%24value%20%3D%20!empty(%24_POST%5B’value’%5D)%20%3F%20%24_POST%5B’value’%5D%20%3A%20”%3B%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • Set a custom error handler for E_NOTICE and redirect the messages away from the standard output.
[pastacode lang=”php” manual=”set_error_handler(‘myHandlerForMinorErrors’%2C%20E_NOTICE%20%7C%20E_STRICT)%20%0A” message=”php code” highlight=”” provider=”manual”/]
  • Disable E_NOTICE from reporting. A quick way to exclude just E_NOTICE is:
[pastacode lang=”php” manual=”error_reporting(%20error_reporting()%20%26%20~E_NOTICE%20)%20%0A” message=”php code” highlight=”” provider=”manual”/] [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.
[pastacode lang=”javascript” manual=”%24data%20%3D%20array(‘foo’%20%3D%3E%20’42’%2C%20’bar’)%3B%0Aecho%20%24data%5B’spinach’%5D%3B%0Aecho%20%24data%5B1%5D%3B%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • 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]:
[pastacode lang=”javascript” manual=”%24data%20%3D%20array(‘foo’%20%3D%3E%20’42’%2C%20’bar’)%3B%0Aif%20(array_key_exists(‘spinach’%2C%20%24data))%20%0A%7B%0A%20%20%20%20echo%20%24data%5B’spinach’%5D%3B%0A%7D%0Aelse%20%0A%7B%0A%20%20%20%20echo%20’No%20key%20spinach%20in%20array’%3B%0A%7D%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • If you have code like:
[pastacode lang=”php” manual=”%3C%3Fphp%20echo%20%24_POST%5B’message’%5D%3B%20%3F%3E%0A%3Cform%20method%3D%22post%22%20action%3D%22%22%3E%0A%20%20%20%20%3Cinput%20type%3D%22text%22%20name%3D%22message%22%3E%0A%20%20%20%20…%0A” message=”php code” highlight=”” provider=”manual”/]
  • 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:
[pastacode lang=”javascript” manual=”if%20(%24_POST)%20%20..%20%20%2F%2F%20if%20the%20%24_POST%20array%20is%20not%20empty%0A%2F%2F%20or%0Aif%20(%24_SERVER%5B’REQUEST_METHOD’%5D%20%3D%3D%20’POST’)%20..%0A” message=”javascript code” highlight=”” provider=”manual”/] [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()

[pastacode lang=”javascript” manual=”%2F%2Fisset()%0A%24value%20%3D%20isset(%24array%5B’my_index’%5D)%20%3F%20%24array%5B’my_index’%5D%20%3A%20”%3B%0A%2F%2Farray_key_exists()%0A%24value%20%3D%20array_key_exists(‘my_index’%2C%20%24array)%20%3F%20%24array%5B’my_index’%5D%20%3A%20”%3B%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • The language construct list()may generate this when it attempts to access an array index that does not exist:
[pastacode lang=”javascript” manual=”list(%24a%2C%20%24b)%20%3D%20array(0%20%3D%3E%20’a’)%3B%0A%2F%2For%0Alist(%24one%2C%20%24two)%20%3D%20explode(‘%2C’%2C%20’test%20string’)%3B%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • 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: