PHP is a widely used scripting language that is mainly used for web development purposes. It is a scripting language and finds application in automating tasks that would otherwise be impossible to implement with just human intervention. Being a server-side language, it primarily takes care of things at the server’s end. Launched in 1995 for public use, it has remained a popular choice among web developers since then.

Programming is a tricky business. It is pretty normal to stumble upon errors and warnings while making a program. The same happens a lot to PHP developers, like when they face an Undefined index in PHP. However, such errors are not hard to deal with with a little bit of knowledge and guidance.

Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program
Want a Top Software Development Job? Start Here!

What Is an Undefined Index PHP Error?

Websites often use forms to collect data from visitors. PHP uses $GET and $POST methods for such data collection. This data is collected and saved in variables that are used by the website to work and serve the visitor further. Many a time, some fields are left blank by the user. But the website tries to refer to these fields for proceeding further. That means the PHP code tries to get the value of the field that no one has defined and thus does not exist. Quite expectedly, it does not work and raises a notice called Undefined Index in PHP.

Code: 

<?php

$name = $_GET['name'];

$age = $_GET['age'];

$grade = $_GET[‘grade’];

echo 'Name: '.$name;

echo '<br>Age: '.$age;

echo ‘<br>Grade: ‘.$grade;

?>

Result: 

Undefined_Index_Php_1

You can fix it using the isset() function, which we will discuss further in the upcoming sections.

Undefined Index is a notice in PHP, and it is a choice of the developer to either ignore it or fix it.

How to Ignore PHP Notice: Undefined Index?

Undefined Index in PHP is a Notice generated by the language. The simplest way to ignore such a notice is to ask PHP to stop generating such notices. You can either add a small line of code at the top of the PHP page or edit the field error_reporting in the php.ini file.

1. Adding Code at the Top of the Page

A simple way to ask PHP to disable reporting of notices is to put a line of code at the beginning of the PHP page.

Code:

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

Or you can add the following code which stops all the error reporting,

<?php error_reporting(0); ?> 

2. Changes in php.ini 

Php.ini is a configuration file and is essential for all the programs running on PHP. Open this file and find the field error_reporting. The easiest way is to use the ctrl + F shortcut key. By default, error reporting is set to E_ALL. That means all errors are reported. Change this to E_ALL & ~E_NOTICE. It means all errors except for the notices will now be reported.

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

How to Fix the PHP Notice: Undefined Index?

We know the cause of the error. It occurs when we use $_GET and $_POST methods to get input, but we reference it even when it has not been set. The solution simply is to check if it has been set before referencing.

We can use the isset() function, which checks if the variable ‘is set’ or not before referencing to them. Isset() function returns True or False depending on it.

Undefined Index in PHP $_GET

When using the $_GET method, the following code might show the notice Undefined index in PHP:

Code:

<?php

$name = $_GET[‘name’];

$age = $_GET[‘age’];

$grade = $_GET[‘grade’];

echo 'Name: '.$name;

echo '<br>Age:  '.$age;

echo '<br>Grade:  '.$grade;

?>

As we have not set any of these variables, this code shows the Undefined Index notice.

Change it to the following code to fix this issue:

Code:

<?php

if(isset($_GET['name'])){

    $name = $_GET['name'];

}else{

    $name = " John - This is our default name";

}

if(isset($_GET['age'])){

    $age = $_GET['age'];

}else{

    $age = "10 - This is our default age. Let's leave the grade blank.";

}

if(isset($_GET['grade'])){

    $grade = $_GET['grade'];

}else{

    $grade = "";

}

echo 'Name: '.$name;

echo '<br>Age: '.$age;

echo '<br>Grade: '.$grade;

?>

Result: 

undefined_Index_Php_2.

Want a Top Software Development Job? Start Here!

Full Stack Development-MEANExplore Program
Want a Top Software Development Job? Start Here!

We can use a single line of code like this instead.

Code:

<?php

$name = isset($_GET[‘name’]) ? $_GET[‘name’]: “John- This is our default name”;

Echo ‘Name: ‘.$name;

?>

This approach also achieves the intended goal.

Result:

undefined_Index_Php_3.

Notice: Undefined Variable

PHP shows this notice when we try to use a variable even before defining it.

Code:

<?php

$echo $name;

<?

Result:

undefined_Index_Php_4

It can be solved either by declaring a variable global and then using isset() to see if it is set or not. It can be echoed only if it has been set. Alternatively, we can use isset(X) ? Y to set a default.

Code:

<?php

global $name;

if(isset($name))

{ echo $name;

}

?>

Result: 

undefined_Index_Php_5.

We can set it after the isset() check like this.

Code: 

<?php

$name= isset($name) ? $name:"Default";

echo $name;

?>

Result:

undefined_Index_Php_6.

Preparing Your Blockchain Career for 2024

Free Webinar | 5 Dec, Tuesday | 9 PM ISTRegister Now
Preparing Your Blockchain Career for 2024

Notice: Undefined Offset

It shows Undefined Offset Notice in PHP when we are referring to a key in an array that has not been set for the array.

Here’s an example.

Code:

<?php

$nameArray = array(1=>'one', 2=>'two', 4=>'four');

echo $nameArray[3];

?>

Result:

It might show an undefined array key warning for some. It means the same thing that you are trying to access an undefined array key.

undefined_Index_Php_7.

It can be solved similarly like the last two cases by using isset(), arrayexists() or empty() function.

Code: 

<?php

$nameArray = array(1=>'one', 2=>'two', 4=>'four');

echo 'using isset()';

echo '<br>key 3 <br>';

if(isset($nameArray[3]))

    {echo  $nameArray[3];

    }

echo 'key 1 <br>';

if(isset($nameArray[1]))

    {echo $nameArray[1];

    }    

echo '<br> using array_key_exists <br>';

echo 'key 3 <br>';

echo array_key_exists(3, $nameArray);

echo '<br> key 2 <br>';

echo array_key_exists(2, $nameArray);

echo '<br>using empty <br>';

echo 'key 3 <br>';

if(!empty($nameArray[3]))

    {echo $nameArray[3]; }

echo '<br>using key 4 <br>';

if(!empty($nameArray[4]))

    {echo $nameArray[4]; }

?>

Result:

undefined_Index_Php_8.

Learn right from the basics of JavaScript to advanced concepts of Angular, Spring Boot, Hibernate, JSPs, MVC, etc. Enroll in our PGP in Full Stack Web Development today!

Conclusion

In this article, we learned about the undefined index in PHP. However, it is just one of the many errors, warnings, and notices that a PHP developer has to deal with. If you are looking to make a career out of developing web applications and handling such errors, Simplilearn is offering a Post Graduate Program in Full stack Web Development, in partnership with Caltech CTME. Simplilearn is the world’s #1 online Bootcamp that has helped advance 2,000,000 careers through collaboration with World’s top-ranking universities.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 15 Apr, 2024

6 Months$ 8,000
Automation Test Engineer

Cohort Starts: 28 Feb, 2024

11 Months$ 1,750
Full Stack Java Developer

Cohort Starts: 19 Mar, 2024

6 Months$ 2,000
Full Stack Developer Course - MERN Stack

Cohort Starts: 3 Apr, 2024

6 Months$ 1,500