How to avoid undefined offset error in PHP ? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The Offset that does not exist in an array then it is called as an undefined offset. Undefined offset error is similar to ArrayOutOfBoundException in Java. If we access an index that does not exist or an empty offset, it will lead to an undefined offset error.Example: Following PHP code explains how we can access array elements. If the accessed index is not present then it gives an undefined offset error. php <?php // Declare and initialize an array // $students = ['Rohan', 'Arjun', 'Niharika'] $students = array( 0 => 'Rohan', 1 => 'Arjun', 2 => 'Niharika' ); // Rohan echo $students[0]; // ERROR: Undefined offset: 5 echo $students[5]; // ERROR: Undefined index: key echo $students[key]; ?> Output: There are some methods to avoid undefined offset error that are discussed below: isset() function: This function checks whether the variable is set and is not equal to null. It also checks if array or array key has null value or not.Example: php <?php // Declare and initialize an array // $students = ['Rohan', 'Arjun', 'Niharika'] $students = array( 0 => 'Rohan', 1 => 'Arjun', 2 => 'Niharika' ); if(isset($students[5])) { echo $students[5]; } else { echo "Index not present"; } ?> Output:Index not presentempty() function: This function checks whether the variable or index value in an array is empty or not. php <?php // Declare and initialize an array // $students = ['Rohan', 'Arjun', 'Niharika'] $students = array( 0 => 'Rohan', 1 => 'Arjun', 2 => 'Niharika' ); if(!empty($students[5])) { echo $students[5]; } else { echo "Index not present"; } ?> Output:Index not presentarray_key_exists() function for associative arrays: Associative array stores data in the form of key-value pair and for every key there exists a value. The array_key_exists() function checks whether the specified key is present in the array or not.Example: php <?php // PHP program to illustrate the use // of array_key_exists() function function Exists($index, $array) { if (array_key_exists($index, $array)) { echo "Key Found"; } else{ echo "Key not Found"; } } $array = array( "ram" => 25, "krishna" => 10, "aakash" => 20 ); $index = "aakash"; print_r(Exists($index, $array)); ?> Output:Key Found PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. Create Quiz Comment N nikitadabral30 Follow 0 Improve N nikitadabral30 Follow 0 Improve Article Tags : Web Technologies PHP PHP Programs PHP-Misc Explore PHP Tutorial 8 min read BasicsPHP Syntax 4 min read PHP Variables 5 min read PHP | Functions 8 min read PHP Loops 4 min read ArrayPHP Arrays 5 min read PHP Associative Arrays 4 min read Multidimensional arrays in PHP 5 min read Sorting Arrays in PHP 4 min read OOPs & InterfacesPHP Classes 2 min read PHP | Constructors and Destructors 5 min read PHP Access Modifiers 4 min read Multiple Inheritance in PHP 4 min read MySQL DatabasePHP | MySQL Database Introduction 4 min read PHP Database connection 2 min read PHP | MySQL ( Creating Database ) 3 min read PHP | MySQL ( Creating Table ) 3 min read PHP AdvancePHP Superglobals 6 min read PHP | Regular Expressions 12 min read PHP Form Handling 4 min read PHP File Handling 4 min read PHP | Uploading File 3 min read PHP Cookies 9 min read PHP | Sessions 7 min read Like