While working in PHP, you will come across two methods called $_POST and $_GET. These methods are used for obtaining values from the user through a form. When using them, you might encounter an error called “Notice: Undefined Index”.
This error means that within your code, there is a variable or constant that has no value assigned to it. But you may be trying to use the values obtained through the user form in your PHP code.
The error can be avoided by using the isset() function. This function will check whether the index variables are assigned a value or not, before using them.
Undefined Index PHP Error
An undefined index is a 'notice' such as the 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) Resolve 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 favourite 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 errors except 'Notice.'
2. PHP Code
If you don’t have access to make changes in the 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 errors except 'Notice.'
Solution or Fix for PHP Notice: Undefined Index
Cause of error:
This error occurs 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 ‘ names’ & ‘age,’ but we did set only the name variable through the $_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 the 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 the ‘name’ and ‘age’ variable, but we didn’t set the ‘age’ variable.
Solutions:
To fix this type of error, you can define the variable as global and use the 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 occurs with arrays when we use the key of an array, which is not set.
In the following, given an 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);
?>