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 // 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 mothods 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// 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";}?>chevron_rightfilter_noneOutput:
Index not present
- empty() function: This function checks whether the variable or index value in an array is empty or not.
<?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";}?>chevron_rightfilter_noneOutput:
Index not present
- array_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 program to illustrate the use// of array_key_exists() functionfunctionExists($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));?>chevron_rightfilter_noneOutput:
Key Found
Recommended Posts:
- variable === undefined vs. typeof variable === “undefined” in JavaScript
- How to remove error Call to undefined function curl_init()?
- How to bind 'this' keyword to resolve classical error message 'state of undefined' in React?
- How to avoid Compile Error while defining Variables
- JavaScript undefined Property
- How to check empty/undefined/null string in JavaScript?
- What are undeclared and undefined variables in JavaScript?
- How to Replace a value if null or undefined in JavaScript?
- JavaScript ReferenceError - Reference to undefined property "x"
- Undefined Vs Null in JavaScript
- CSS | outline-offset Property
- How to get the client timezone offset in JavaScript?
- What is the difference between position() and offset() in jQuery ?
- How to get rid of an element offset using CSS ?
- How to set the offset property for ScrollSpy in Bootstrap ?
- How to set timezone offset using JavaScript ?
- How to avoid NullPointerException in Java using Optional class?
- How to avoid dropdown menu to close menu items on clicking inside ?
- How to avoid a new line with tag?
- How to avoid binding by using arrow functions in callbacks in ReactJS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

