Online Tutorials & Training Materials | STechies.com
Register Login

How to Solve PHP Notice: Undefined Index?

|| 0

How to Solve PHP Notice: Undefined Index?
Stechies

Undefined index is a 'notice' such as following:

“Notice: Undefined variable”, 

“Notice: Undefined index”, and “Notice: Undefined offset”

As you can see above are all notices, here are two ways to deal with such notices.

1) Ignore such notices
2) Resolove such notices.

How to ignore PHP Notice: Undefined index

You can ignore this notice by disabling reporting of notice with option error_reporting.

1.php.ini

Open php.ini file in your favorite editor and search for text “error_reporting” the default value is E_ALL you can change it to E_ALL & ~E_NOTICE.

By default:

error_reporting = E_ALL

Change it to:

error_reporting = E_ALL & ~E_NOTICE

Now your PHP compiler will show all error except 'Notice'.

2.PHP Code

If you don’t have access to make changes in php.ini file, In this case you need to disable the notice by adding the following code on the top of your php page.

<?php error_reporting (E_ALL ^ E_NOTICE); ?>

Now your PHP compiler will show all error except 'Notice'.

Resolve or Fix for PHP Notice: Undefined index

Cause of error:

This error occur with $ _POST and $ _GET method, when you use index or variables which have not set through $ _POST or $ _GET method but you are already using their value in your PHP code.

Undefined index in php $_get

Example using $_GET

In the following example we have used two variables ‘name’ & ‘age’ but we did set only the name variable through $_GET method that’s why it throws the notice.

http://yoursite.com/index.php?name=ram

<?php 
$name = $_GET['name'];
$age = $_GET['age'];

echo $name;
echo $age;
?>

OUTPUT:

Notice: Undefined index: age \index.php on line 5

Solution

To solve such error you can use isset() function which will check whether the index or variable is set or not, If not then don’t use it.

if(isset($_GET[index error name]))

Code with Error resolved using isset() function:

http://yoursite.com/index.php?name=ram

<?php
if(isset($_GET['name'])){
      $name = $_GET['name']; 
 }else{
      $name = "Name not set in GET Method";
 }
if(isset($_GET['age'])){
      $name = $_GET['age']; 
 }else{
      $name = "<br>Age not set in GET Method";
 }
echo $name;
echo $age;
?>

OUTPUT:

ram
Age not set in GET Method

Set Index as blank

We can also set the index as blank index:

// example with $_POST method

$name = isset($_POST['name']) ? $_POST['name'] : '';
$name = isset($_POST['age']) ? $_POST['age'] : '';

// example with $_GET method

$name = isset($_GET['name']) ? $_GET['name'] : '';
$name = isset($_GET['age']) ? $_GET['age'] : '';

Notice: Undefined variable

This notice occurs when you use any variable in your PHP code which is not set.

Example:

<?php 
$name='RAM';

echo $name;
echo $age;
?>

Output:

Notice: Undefined variable: age in D:\xampp\htdocs\testsite.loc\index.php on line 7

In the above example we are displaying value stored in ‘name’ and ‘age’ variable, but we didn’t set the ‘age’ variable.

Solutions:

To fix this type of error you can define variable as global and use isset() function to check if this set or not.

<?php 
global $name;
global $age; 
echo $name;
?>

<?php
if(isset($name)){echo $name;}
if(isset($age)){echo $age;}
?>

<?php
// Set Variable as Blank 
$name = isset($name) ? $name : '';
$age= isset($age) ? $age: '';
?>

Notice: Undefined offset

This type of error occur with arrays when we use the key of an array which is not set.

In the following given example we are displaying the value store in array key 1 but we did not set while declaring array “$colorarray”.

Example:

<?php 
// declare an array with key 2, 3, 4, 5 
$colorarray = array(2=>'Red',3=>'Green',4=>'Blue',5=>'Yellow');

// echo value of array at offset 1.
echo $colorarray[1];
?>

Output: 

Notice: Undefined offset: 1 in \index.php on line 5

Solutions:

Check the value of offset array with function isset() & empty(), and use array_key_exists() function to check if key exist or not.

<?php 
$colorarray = array(2=>'Red',3=>'Green',4=>'Blue',5=>'Yellow');

// isset() function to check value at offset 1 of array
if(isset($colorarray[1])){echo $colorarray[1];}

// empty() function to check value at offset 1 of array
if(!empty($colorarray[1])){echo $colorarray[1];}

// array_key_exists() of check if key 1 is exist or not
echo array_key_exists(1, $colorarray);
?>