skip to Main Content

How do you trigger a E_NOTICE error in PHP8? A lot of things that used to be E_NOTICE in PHP7 has been increased in severity to E_WARNING, here are things i’ve already tried, that does not work:

echo $undefinedVar; // PHP>=8 E_WARNING
$array = [];
echo $array['undefined_key']; // PHP>=8 E_WARNING
class Test {
    public $name;
}
$obj = new Test();
echo $obj->age; // PHP>=8 E_WARNING
$string = "abc";
echo $string[5]; // PHP>=8 E_WARNING
compact('undefinedVar'); // PHP>=8 E_WARNING
strlen(); // PHP>=8 ArgumentCountError

4

Answers


  1. Chosen as BEST ANSWER

    Found a way!

    spl_autoload_register(null,false);
    

    will trigger E_NOTICE:

    Notice: PHP Request Startup: Argument #2 ($do_throw) has been ignored, spl_autoload_register() will always throw
    

  2. EDIT: Ah dang! I just read that all wrong and triggered E_WARNING instead of E_NOTICE. 🙁

    OK, best I got is:

    end(glob(0));
    

    Not sure if that actually causes a disk read or not. Some slightly longer versions:

    end(str_split(0));
    end(explode(0,0));
    end((fn()=>[])());
    

    This is interesting, it’s like a weird code golf problem, try to find
    the shortest code that triggers a warning.

    Using an undefined variable is a warning, so you could just do:

    $x=$x;

    Or referencing an undefined array index:

    [][0];

    Or both at the same time gives you two warnings:

    $x[0];

    Login or Signup to reply.
  3. You can always trigger error yourself using trigger_error() function.

    trigger_error("My custom notice!", E_USER_NOTICE);
    

    See live example: https://3v4l.org/8eLEn

    Login or Signup to reply.
  4. You will find the full list of notices raised by PHP 8.3 here.

    A few examples (demo):

    session_start();
    session_start();
    
    ob_end_clean();
    
    date_default_timezone_set('unknown');
    

    Output:

    Notice: session_start(): Ignoring session_start() because a session is already active (started from /in/KUrgb on line 2) in /in/KUrgb on line 3
    
    Notice: ob_end_clean(): Failed to delete buffer. No buffer to delete in /in/KUrgb on line 5
    
    Notice: date_default_timezone_set(): Timezone ID 'unknown' is invalid in /in/KUrgb on line 7
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search