How to fix undefined index in PHP

Summary:

Undefined index or array key error occur when your code is trying to access an array key that doesn’t exist. This can easily be handled by checking if the key is set and using a conditional to handle logic. One such example is $name = isset($arr['name']) ? $arr['name'] : 'N/A';

Undefined Index in PHP

The undefined index in PHP or undefined array key in PHP arises when trying to access an array key that doesn’t exist. Here’s one of the recommended fixes from this article.

<?php
$arr = [
    "Name" => "Selina",
    "Job Title" => "Software Developer",
];
 
$name = isset($arr["Name"]) ? $arr["Name"] : "N/A";
$title = isset($arr["Job Title"]) ? $arr["Job Title"] : "N/A";
$experience = isset($arr["Experience"]) ? $arr["Experience"] : "N/A";
 
echo "Hi, this is ".$name.PHP_EOL;
echo "I am a ".$title.PHP_EOL;
echo "I have ".$experience." years of experience".PHP_EOL; //Key doesn't exist.
 
/*
OUTPUT
Hi, this is Selina
I am a Software Developer
I have N/A years of experience
*/
?>

Stay tuned to the article to learn more about the undefined array key in PHP and how to fix it.

Introduction

PHP associative arrays have key and value pairs. The keys are either numeric or string. The associated values can be directly retrieved using the key.

undefined index in php

However, accessing a key that doesn’t exist either results in a notice or a warning, depending on the PHP version. PHP issues a notice with a message, “undefined in index” or “undefined offset.”

Latest versions of PHP issue a warning with a message, “undefined array key.” So, this article explores how to fix undefined index errors in PHP.

Undefined Index in PHP

Here’s a code snippet trying to access a key that doesn’t exist.

<?php
$arr = [
    "Name" => "Selina",
    "Job Title" => "Software Developer"
];
 
echo "Hi, this is ".$arr["Name"].PHP_EOL;
echo "I am a ".$arr["Job Title"].PHP_EOL;
echo "I have ".$arr["Experience"]." years of experience".PHP_EOL; //Key doesn't exist.
 
/*
OUTPUT
Hi, this is Selina
I am a Software Developer
PHP Warning:  Undefined array key "Experience"...
*/
?>

The undefined array key notice in PHP is much more commonplace when accessing form inputs from $_POST or $_GET in an application. Fortunately, the fixes that we’re going to see apply to those scenarios.

Fix #1 – Undefined Index in PHP using array_key_exists

One recommended way to fix undefined index PHP is to check if the key exists before accessing it. PHP array_key_exists function determines if a key exists. Using it to check keys makes the code more error-prone.

<?php
$arr = [
    "Name" => "Selina",
    "Job Title" => "Software Developer"
];
 
$name = array_key_exists("Name", $arr) ? $arr["Name"] : "N/A";
$title = array_key_exists("Job Title", $arr) ? $arr["Job Title"] : "N/A";
$experience = array_key_exists("Experience", $arr) ? $arr["Experience"] : "N/A";
 
echo "Hi, this is ".$name.PHP_EOL;
echo "I am a ".$title.PHP_EOL;
echo "I have ".$experience." years of experience".PHP_EOL; //Key doesn't exist.
 
/*
OUTPUT
Hi, this is Selina
I am a Software Developer
I have N/A years of experience
*/
?>

Voila! The ternary operator works like an if/else statement. If the key exists, it assigns the associated value to the variable. Else returns “N/A.”

So, we run out of the risk of getting an undefined array key error in PHP. Up next is another similar fix for the undefined key in PHP.

Fix #2 – Undefined Index in PHP using isset

The PHP isset function is also helpful in determining if an array key exists and its value is not null. Keys with null values return true on array_key_exists but couldn’t escape the isset function. 

<?php
$arr = [
    "Name" => "Selina",
    "Job Title" => "Software Developer",
];
 
$name = isset($arr["Name"]) ? $arr["Name"] : "N/A";
$title = isset($arr["Job Title"]) ? $arr["Job Title"] : "N/A";
$experience = isset($arr["Experience"]) ? $arr["Experience"] : "N/A";
 
echo "Hi, this is ".$name.PHP_EOL;
echo "I am a ".$title.PHP_EOL;
echo "I have ".$experience." years of experience".PHP_EOL; //Key doesn't exist.
 
/*
OUTPUT
Hi, this is Selina
I am a Software Developer
I have N/A years of experience
*/
?>

Perfect! It works like a charm and avoids the undefined key error in PHP.

Fix #3 – Undefined Array Key in PHP using null coalesce operator.

PHP 7.0 features the null coalesce operator, a more compact form of the ternary operator. Here’s how it works.

<?php
$arr = [
    "Name" => "Selina",
    "Job Title" => "Software Developer",
];
 
$name = $arr["Name"] ?? "N/A";
$title = $arr["Job Title"] ?? "N/A";
$experience = $arr["Experience"] ?? "N/A";
 
echo "Hi, this is ".$name.PHP_EOL;
echo "I am a ".$title.PHP_EOL;
echo "I have ".$experience." years of experience".PHP_EOL; //Key doesn't exist.
 
/*
OUTPUT
Hi, this is Selina
I am a Software Developer
I have N/A years of experience
*/
?>

Voila! Much cleaner and more compact. 

Fix #4 – Undefined Index in PHP by disabling E_Notice

A not so recommended way is to turn off the PHP notices. Notices don’t break the code but disabling them allows minor issues like these to leak out at the users’ end. So, this is a way out but not something we would use very often.

To disable PHP notice, add the following line to the top of your script.


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

We’re good to go. Time for the wrap up!

Wrap Up

This article includes fixes for the undefined array key in PHP. Undefined index or array key error occurs when accessing an array key that doesn’t exist. Recommended fixes use a proactive approach of checking the key before accessing it.

Hopefully, you’ve enjoyed the article. Stay tuned for more articles like these at FuelingPHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.


Article Categories

Article Tags

© 2022 Confident.Systems