Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

assert() Throws Exception

assert() is the PHP native implementation of assertions. Until PHP 8.0, it would raise an error, while now, it throws an exception.

PHP code

<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
        print __METHOD__;

    return true;
}

set_error_handler('myErrorHandler');

try {
        assert(false);
} catch (\Error $e) {
        print $e->getMessage();
}

?>

Before

myErrorHandler

After

assert(false)

PHP version change

This behavior changed in 8.0.

Extension

© 2023-2026, Damien Seguy - Exakat