Skip to main content
added 1071 characters in body
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374

This error may appear in two cases:

1. Genuine error

You are getting this error when the code is trying to access a variable or an array element that doesn't exist: either it was never set or just misspelled.

  • for a variable, check the previous code where it was supposed to be defined. Verify the spelling (note that variable names are case sensitive) or create this variable if it wasn't.
  • for an array element, do a var_dump($array) and study the output - may be the key was just misspelled (note that array keys are case sensitive so you need to use exactly same case).
    In case it is not set at all, check the previous code where the key was supposed to be defined and create an array element with such key.

2. Negligence

This error message is meant to help a PHP programmer to spot a typo or a mistake when accessing a variable (or an array element) that doesn't exist. but in some cases it may appear due to just negligence: since undefined variable is not a fatal error, and it was possible to hide such errors, some programmers tend to just ignore them. But in this case one will lose genuine errors as well, and get undesirable results.

So a good programmer:

Check the good practices listed below:

This error message is meant to help a PHP programmer to spot a typo or a mistake when accessing a variable (or an array element) that doesn't exist. So a good programmer:

This error may appear in two cases:

1. Genuine error

You are getting this error when the code is trying to access a variable or an array element that doesn't exist: either it was never set or just misspelled.

  • for a variable, check the previous code where it was supposed to be defined. Verify the spelling (note that variable names are case sensitive) or create this variable if it wasn't.
  • for an array element, do a var_dump($array) and study the output - may be the key was just misspelled (note that array keys are case sensitive so you need to use exactly same case).
    In case it is not set at all, check the previous code where the key was supposed to be defined and create an array element with such key.

2. Negligence

This error message is meant to help a PHP programmer to spot a typo or a mistake when accessing a variable (or an array element) that doesn't exist. but in some cases it may appear due to just negligence: since undefined variable is not a fatal error, and it was possible to hide such errors, some programmers tend to just ignore them. But in this case one will lose genuine errors as well, and get undesirable results.

So a good programmer:

Check the good practices listed below:

added 592 characters in body
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374
  1. Makes sure that every variable or array key is already defined by the time it's going to be used. In case a variable is needed to be used inside a function, it must be passed to that function as a parameter.
  2. Pays attention to this error and proceeds to fix it, just like with any other error. It may indicate a spelling error or that some procedure didn't return the data it should.
  3. Only on a rare occasion, when things are not under the programmer's control, a code can be added to circumvent this error. But by no means it should be a mindless habit.
  1. Recommended: Declare every variable before use. This way you will see this error only when you actually make a mistake, trying to use a non-existent variable - the very reason this error message exists.

     //Initializing a variable
     $value = ""; //Initialization value; 0 for int, [] for array, etc.
     echo $value; // no error
     echo $vaule; // an error pinpoints a misspelled variable name
    
  2. Suppress the error with null coalescing operator. But remember that this way PHP won't be able to notify you about using wrong variable name.

     // Null coalescing operator
     echo $value ?? '';
    

    For the ancient PHP versions (< 7.0) isset() with ternary can be used

     echo isset($value) ? $value : '';
    

    Be aware though, that it's still essentially an error suppression, though for just one particular error. So it may prevent PHP from helping you by marking an unitialized variable.

  3. Suppress the error with the @ operator. Left here for the historical reasons but seriously, it just shouldn't happen.

  • a special case when a variable is defined but is not visible in a function. Functions in PHP have own variable scope, and if you need to use in a function a variable from outside, its value must be passed as a function's parameter:

    function test($param) {
        return $param + 1; 
    }
    $var = 0;
    echo test($var); // now $var's value is accessible inside through $param
    
  1. Suppress the error with null coalescing operator. But remember that this way PHP won't be able to notify you about using wrong variable name.

     // Null coalescing operator
     echo $value ?? '';
    

    For the ancient PHP versions (< 7.0) isset() with ternary can be used

     echo isset($value) ? $value : '';
    

    Be aware though, that it's still essentially an error suppression, though for just one particular error. So it may prevent PHP from helping you by marking an unitialized variable.

  2. Suppress the error with the @ operator. Left here for the historical reasons but seriously, it just shouldn't happen.

  1. Makes sure that every variable or array key is already defined by the time it's going to be used.
  2. Pays attention to this error and proceeds to fix it, just like with any other error. It may indicate a spelling error or that some procedure didn't return the data it should.
  3. Only on a rare occasion, when things are not under the programmer's control, a code can be added to circumvent this error. But by no means it should be a mindless habit.
  1. Recommended: Declare every variable before use. This way you will see this error only when you actually make a mistake, trying to use a non-existent variable - the very reason this error message exists.

     //Initializing a variable
     $value = ""; //Initialization value; 0 for int, [] for array, etc.
     echo $value; // no error
     echo $vaule; // an error pinpoints a misspelled variable name
    
  2. Suppress the error with null coalescing operator. But remember that this way PHP won't be able to notify you about using wrong variable name.

     // Null coalescing operator
     echo $value ?? '';
    

    For the ancient PHP versions (< 7.0) isset() with ternary can be used

     echo isset($value) ? $value : '';
    

    Be aware though, that it's still essentially an error suppression, though for just one particular error. So it may prevent PHP from helping you by marking an unitialized variable.

  3. Suppress the error with the @ operator. Left here for the historical reasons but seriously, it just shouldn't happen.

  1. Makes sure that every variable or array key is already defined by the time it's going to be used. In case a variable is needed to be used inside a function, it must be passed to that function as a parameter.
  2. Pays attention to this error and proceeds to fix it, just like with any other error. It may indicate a spelling error or that some procedure didn't return the data it should.
  3. Only on a rare occasion, when things are not under the programmer's control, a code can be added to circumvent this error. But by no means it should be a mindless habit.
  1. Recommended: Declare every variable before use. This way you will see this error only when you actually make a mistake, trying to use a non-existent variable - the very reason this error message exists.

     //Initializing a variable
     $value = ""; //Initialization value; 0 for int, [] for array, etc.
     echo $value; // no error
     echo $vaule; // an error pinpoints a misspelled variable name
    
  • a special case when a variable is defined but is not visible in a function. Functions in PHP have own variable scope, and if you need to use in a function a variable from outside, its value must be passed as a function's parameter:

    function test($param) {
        return $param + 1; 
    }
    $var = 0;
    echo test($var); // now $var's value is accessible inside through $param
    
  1. Suppress the error with null coalescing operator. But remember that this way PHP won't be able to notify you about using wrong variable name.

     // Null coalescing operator
     echo $value ?? '';
    

    For the ancient PHP versions (< 7.0) isset() with ternary can be used

     echo isset($value) ? $value : '';
    

    Be aware though, that it's still essentially an error suppression, though for just one particular error. So it may prevent PHP from helping you by marking an unitialized variable.

  2. Suppress the error with the @ operator. Left here for the historical reasons but seriously, it just shouldn't happen.

added 318 characters in body
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374
  1. Makes sure that every variable or array key is already defined by the time it's going to be used.
  2. Pays attention to this error and proceeds to fix it, just like with any other error. It may indicate a spelling error or that some procedure that didn't return the data it should.
  3. Only on a rare occasion, when things are not under the programmer's control, they may add a code can be added to circumvent this error. ByBut by no means it should be a mindless habit.
    //Initializing a variable
    $array['value'] = ""; //Initialization value; 0 for int, [] for array, etc.
    echo $array['value']; // no error
    echo $array['vaule']; // an error indicates a misspelled key

A special case is when some function returns either an array or some other value such as null or false. Then it has to be tested before trying to access the array elements, such as

$row = $stmt->fetch();
if ($row) { // the record was found and can be worked with
    echo $row['name']; 
}

But assignments should be done at the very beginning of the script. Validate all input*, assign it to local variables, and use them all the way in the code. So every variable you're going to access would deliberately existsexist.

  1. Makes sure that every variable or array key is already defined by the time it's going to be used.
  2. Pays attention to this error and proceeds to fix it, just like with any other error. It may indicate a spelling error or some procedure that didn't return the data it should.
  3. Only on a rare occasion, when things are not under the programmer's control, they may add a code to circumvent this error. By by no means it should be a mindless habit.
    //Initializing a variable
    $array['value'] = ""; //Initialization value; 0 for int, [] for array, etc.
    echo $array['value']; // no error
    echo $array['vaule']; // an error indicates a misspelled key

But assignments should be done at the very beginning of the script. Validate all input*, assign it to local variables, and use them all the way in the code. So every variable you're going to access deliberately exists.

  1. Makes sure that every variable or array key is already defined by the time it's going to be used.
  2. Pays attention to this error and proceeds to fix it, just like with any other error. It may indicate a spelling error or that some procedure didn't return the data it should.
  3. Only on a rare occasion, when things are not under the programmer's control, a code can be added to circumvent this error. But by no means it should be a mindless habit.
    //Initializing a variable
    $array['value'] = ""; //Initialization value; 0 for int, [] for array, etc.
    echo $array['value']; // no error
    echo $array['vaule']; // an error indicates a misspelled key

A special case is when some function returns either an array or some other value such as null or false. Then it has to be tested before trying to access the array elements, such as

$row = $stmt->fetch();
if ($row) { // the record was found and can be worked with
    echo $row['name']; 
}

But assignments should be done at the very beginning of the script. Validate all input, assign it to local variables, and use them all the way in the code. So every variable you're going to access would deliberately exist.

added 639 characters in body
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374
Loading
this answer initially dwelled on how to circumvent the error, I tried to make it actually helpful.
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374
Loading
more clarification
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374
Loading
fix word 'avoid'
Source Link
CarlosCarucce
  • 3.6k
  • 3
  • 31
  • 56
Loading
a fixed reference to PHP manual
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374
Loading
using empty() in context of this error makes absolutely no sense
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374
Loading
Obsoleted suggestions removed. Undefined variable is now a Warning. Also consistent examples. Man it needs a complete rewrite
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374
Loading
that intro about empty() seems completely off the track. Nobody asked
Source Link
Your Common Sense
  • 158.2k
  • 42
  • 226
  • 374
Loading
eugh; markdown is the worst
Source Link
IMSoP
  • 99.7k
  • 18
  • 135
  • 183
Loading
There are more values considered as falsy by the function empty
Source Link
Loading
deleted 31 characters in body
Source Link
AbraCadaver
  • 79.2k
  • 7
  • 75
  • 91
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
Loading
Broke up blockquote into 3 parts. Second part is quoted from a different page. Third part doesn't appear to be quoted from anywhere. Formatted quote according to original. Edited code so it actually compares the two results. Moved code to site that maps URL to a *single* version of the code.
Source Link
Nisse Engström
  • 4.8k
  • 23
  • 29
  • 44
Loading
Added the null coalesce operator of PHP 7
Source Link
glaux
  • 710
  • 7
  • 21
Loading
added 864 characters in body
Source Link
user2560539
user2560539
Loading
added 292 characters in body
Source Link
AbraCadaver
  • 79.2k
  • 7
  • 75
  • 91
Loading
added 292 characters in body
Source Link
AbraCadaver
  • 79.2k
  • 7
  • 75
  • 91
Loading
added 175 characters in body
Source Link
Rizier123
  • 59.8k
  • 17
  • 106
  • 167
Loading
added 283 characters in body
Source Link
Rizier123
  • 59.8k
  • 17
  • 106
  • 167
Loading
added 975 characters in body
Source Link
Rizier123
  • 59.8k
  • 17
  • 106
  • 167
Loading
Formatting
Source Link
Tomasz
  • 5.2k
  • 2
  • 37
  • 42
Loading
Expanding answer to empty().
Source Link
kenorb
  • 168.7k
  • 95
  • 712
  • 796
Loading