How to use assert() to check if assertion is FALSE in PHP

6 Answers

0 votes
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);

// handler function
function assert_handler($file, $line, $code)
{
    echo "Assertion Failed:
        File '$file'<br />
        Line '$line'<br />
        Code '$code'<br />";
}

// Set up the callback handler function
assert_options(ASSERT_CALLBACK, 'assert_handler');

// Make failed assertion
assert('mysql_query("")');

 
/*
run: 

Assertion Failed: File 'C:\xampp\htdocs\knowrex.com\test.php'
Line '26'
Code 'mysql_query("")'

*/

 



answered May 31, 2016 by avibootz
edited May 31, 2016 by avibootz
0 votes
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);

// handler function
function assert_handler($file, $line, $code, $desc = null)
{
    echo "Assertion failed at $file:$line: $code";
    if ($desc) 
        echo ": $desc" . "<br />";
    echo "<br />";
}

// Set up the callback handler function
assert_options(ASSERT_CALLBACK, 'assert_handler');

// Make failed assertion 
assert('2 < 1');
assert('2 < 1', '2 is > than 1');

 
/*
run: 

Assertion failed at C:\xampp\htdocs\knowrex.com\test.php:26: 2 < 1
Assertion failed at C:\xampp\htdocs\knowrex.com\test.php:27: 2 < 1: 2 is > than 1

*/

?>

 



answered May 31, 2016 by avibootz
0 votes
//assert_options(ASSERT_WARNING, 0);
//assert_options(ASSERT_QUIET_EVAL, 1);

// handler function
function assert_handler($file, $line, $code, $desc = null)
{
    echo "Assertion failed at $file:$line: $code";
    if ($desc) 
        echo ": $desc" . "<br />";
    echo "<br />";
}

// Set up the callback handler function
assert_options(ASSERT_CALLBACK, 'assert_handler');

// Make failed assertion 
assert('2 < 1');
assert('2 < 1', '2 is > than 1');

 
/*
run: 

Assertion failed at C:\xampp\htdocs\knowrex.com\test.php:25: 2 < 1

Warning: assert(): Assertion "2 < 1" failed in C:\xampp\htdocs\knowrex.com\test.php on line 25
Assertion failed at C:\xampp\htdocs\knowrex.com\test.php:26: 2 < 1: 2 is > than 1


Warning: assert(): 2 is > than 1: "2 < 1" 
         failed in C:\xampp\htdocs\knowrex.com\test.php on line 26

*/

 



answered May 31, 2016 by avibootz
0 votes
assert(true == true);
echo 'PHP';

 
/*
run: 

PHP 

*/

 



answered May 31, 2016 by avibootz
0 votes
assert(true == false);
echo 'PHP';

 
/*
run: 

Warning: assert(): Assertion failed in C:\xampp\htdocs\knowrex.com\test.php on line 9
PHP  

*/

 



answered May 31, 2016 by avibootz
0 votes
function assert_fail_function()
{
    echo 'Assert failed';
}

function test_assert_function($parameter)
{
    assert(is_bool($parameter));
}

assert_options(ASSERT_BAIL,     true);
assert_options(ASSERT_WARNING,  false);
assert_options(ASSERT_CALLBACK, 'assert_fail_function');

// Make fail assert 
test_assert_function(100);

// This is never run due to ASSERT_BAIL 
echo 'Last Line Never reached';

 
/*
run: 

Assert failed  

*/

 



answered May 31, 2016 by avibootz

Related questions

2 answers 292 views
2 answers 240 views
1 answer 198 views
198 views asked Sep 23, 2021 by avibootz
1 answer 332 views
...