assert in PHP

Synatx of assert

bool assert(string|bool assertion[, string description] )

If assertion is true, generates a warning in executing the code. If assertion is a string, assert() evaluates that string as PHP code. The optional second argument allows for additional text to be added in with the failure message. Check the assert_options() function to see its related connection.

Example of assert in PHP

// Active assert and make it quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);

// Create a handler function
function my_assert_handler($file, $line, $code)
    echo "<hr>Assertion Failed:
        File '$file'<br />
        Line '$line'<br />
        Code '$code'<br /><hr />";

// Set up the callback
assert_options(ASSERT_CALLBACK, 'my_assert_handler');

// Make an assertion that should fail

Leave a Comment