Avoiding undefined index / offset errors in PHP.

This is a PHP tutorial on how to “fix” undefined index and undefined offset errors.

These are some of the most common notices that a beginner PHP developer will come across. However, there are two simple ways to avoid them.

What is an index?

Firstly, you will need to understand what an index is.

When you add an element to array, PHP will automatically map that element to an index number.

For example, if you add an element to an empty array, PHP will give it the index “0“. If you add another element after that, it will be given the index “1“.

This index acts as an identifier that allows you to interact with the element in question. Without array indexes, we would be unable to keep track of which element is which.

Take the following array as an example.

//Example array
$animals = array(
    0 => 'Cat',
    1 => 'Dog',
    2 => 'Bird'

As you can see, Cat has the index “0” and Dog has the index “1“. If I want to print the word “Cat” out onto the page, I will need to access the “Cat” element via its array index.

//We know that "Cat" is at index 0.
echo $animals[0]; //Prints out "Cat"

As you can see, we were able to access the “Cat” element by specifying the index “0“.

Similarly, if we want to print out the word “Dog”, then we can access it via the index “1“.

//Print out "Dog"
echo $animals[1];

If we want to delete the “Dog” element from our PHP array, then we can do the following.

//Deleting "Dog" from our array.

The unset function will remove the element in question. This means that it will no longer exist.

Undefined offset and index errors.

An undefined offset notice will occur if you attempt to access an index that does not exist.

This is where you’ll encounter nasty errors such as the following.

Notice: Undefined offset: 1 in /path/to/file.php on line 2

Or, if you are using array keys instead of numerical indexes.

Notice: Undefined index: test in /path/to/file.php on line 2

PHP will display these notices if you attempt to access an array index that does not exist.

Although it is not a fatal error and the execution of your script will continue, these kind of notices tend to cause bugs that can lead to other issues.

$_POST, $_GET and $_SESSION.

A lot of beginner PHP developers fail to realize that $_POST, $_GET, $_SESSION, $_FILES, $_COOKIE, $_SERVER and $_REQUEST are all predefined arrays.

These are what we call superglobal variables.

In other words, they exist by default. Furthermore, developers should treat them like regular arrays.

When you access a GET variable in PHP, what you’re actually doing is accessing a GET variable that has been neatly packaged into an array called $_GET.

//$_GET is actually an array
echo $_GET['test'];

In the above piece of code, you are accessing an array element with the index “test”.

Avoiding index errors.

To avoid these errors, you can use two different methods.

The first method involves using the isset function, which checks to see if a variable exists.

//Check if the index "test" exists before accessing it.
    //It exists. We can now use it.
    echo $_GET['test'];
} else{
    //It does not exist.

The second method involves using the function array_key_exists, which specifically checks to see if an array index exists.

if(array_key_exists('test', $_GET)){
    //It exists
    echo $_GET['test'];
} else{
    //It does not exist

When you are dealing with GET variables and POST variables, you should never assume that they exist. This is because they are external variables.

In other words, they come from the client.

For example, a hacker can remove a GET variable or rename it. Similarly, an end user can inadvertently remove a GET variable by mistake while they are trying to copy and paste a URL.

Someone with a basic knowledge of HTML can delete form elements using the Inspect Element tool. As a result, you cannot assume that a POST variable will always exist either.