53

I am receiving the following error in PHP

Notice undefined offset 1: in C:\wamp\www\includes\imdbgrabber.php line 36

Here is the PHP code that causes it:

<?php

# ...

function get_match($regex, $content)  
{  
    preg_match($regex,$content,$matches);     

    return $matches[1]; // ERROR HAPPENS HERE
}

What does the error mean?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user272899
  • 871
  • 5
  • 16
  • 22
  • When I use: $url = 'http://www.imdb.com/title/tt0367882/'; It shows the movie info for that title. When I use $url = $_GET['link']; It doesn't show the data – user272899 Mar 24 '10 at 14:16

4 Answers4

45

If preg_match did not find a match, $matches is an empty array. So you should check if preg_match found an match before accessing $matches[0], for example:

function get_match($regex,$content)
{
    if (preg_match($regex,$content,$matches)) {
        return $matches[0];
    } else {
        return null;
    }
}
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • That fixed that error. But I still can't work out why it won't show the movie info when I use: $url = $_GET['link']; and only shows it when I use: $url = 'http://www.imdb.com/title/tt0367882/'; I have tested that I am getting the correct data from the variable with echo and I am but it doesn't work. – user272899 Mar 24 '10 at 14:20
  • 2
    The `else` block isn't necessary because the function will return `NULL` automatically anyway. – Amal Murali Feb 15 '14 at 08:00
40

How to reproduce this error in PHP:

Create an empty array and ask for the value given a key like this:

php> $foobar = array();

php> echo gettype($foobar);
array

php> echo $foobar[0];

PHP Notice:  Undefined offset: 0 in 
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) : 
eval()'d code on line 1

What happened?

You asked an array to give you the value given a key that it does not contain. It will give you the value NULL then put the above error in the errorlog.

It looked for your key in the array, and found undefined.

How to make the error not happen?

Ask if the key exists first before you go asking for its value.

php> echo array_key_exists(0, $foobar) == false;
1

If the key exists, then get the value, if it doesn't exist, no need to query for its value.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • I am getting an undefined offset error in my Magento 2.3 website, https://magento.stackexchange.com/q/321321/57334 Any help thanks – zus Sep 04 '20 at 14:22
5

Undefined offset error in PHP is Like 'ArrayIndexOutOfBoundException' in Java.

example:

<?php
$arr=array('Hello','world');//(0=>Hello,1=>world)
echo $arr[2];
?>

error: Undefined offset 2

It means you're referring to an array key that doesn't exist. "Offset" refers to the integer key of a numeric array, and "index" refers to the string key of an associative array.

pathe.kiran
  • 2,444
  • 1
  • 21
  • 27
1

Undefined offset means there's an empty array key for example:

$a = array('Felix','Jon','Java');

// This will result in an "Undefined offset" because the size of the array
// is three (3), thus, 0,1,2 without 3
echo $a[3];

You can solve the problem using a loop (while):

$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
    // Increase count by 1, thus, $i=1
    $i++;

    $groupname[$i] = base64_decode(base64_decode($row['groupname']));

    // Set the first position of the array to null or empty
    $groupname[0] = "";
}
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146