Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone give me a good reason for why assert in php behaves the way it does?

Tags:

php

assert

PHP's assert statement doesn't behave like most other languages.

assert('return false'); actually evaluates the string and then asserts its result (false).

Instead of comparing the parameter to true, it goes through the extra step of examining the argument, and if it's a string evaluating it, then performing the comparison.

Very strange indeed.

My problem is not in understanding the behaviour, my problem is coming up with a valid reason for this behaviour, esp. since you now have to do the extra mental work of thinking... "does that evaluate to a string?".

like image 683
Allain Lalonde Avatar asked Sep 16 '25 20:09

Allain Lalonde


1 Answers

The advantages of a string assertion are less overhead when assertion checking is off and messages containing the assertion expression when an assertion fails. This means that if you pass a boolean condition as assertion this condition will not show up as parameter to the assertion function which you may have defined with the assert_options() function, the condition is converted to a string before calling that handler function, and the boolean FALSE is converted as the empty string.

from http://www.php.net/manual/en/function.assert.php

like image 65
Jakob Stoeck Avatar answered Sep 19 '25 02:09

Jakob Stoeck