(PHP 4, PHP 5, PHP 7, PHP 8) assert — Checks if assertion is DescriptionPHP 5 and 7 assert(mixed Traditional assertions (PHP 5 and 7) If the Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be Assertions should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated. The behavior of assert() may be configured by assert_options() or by .ini-settings described in that functions manual page. The assert_options() function and/or assert() callbacks are particularly useful for building automated test suites because they allow you to easily capture the code passed to the assertion, along with information on where the assertion was made. While this information can be captured via other methods, using assertions makes it much faster and easier! The callback function should accept three arguments. The first argument will contain the file the assertion failed in. The second argument will contain the line the assertion failed on and the third
argument will contain the expression that failed (if any — literal values such as 1 or "two" will not be passed via this argument). Users of PHP 5.4.8 and later may also provide a fourth optional argument, which will contain the Expectations (PHP 7 only)assert() is a language construct in PHP 7, allowing for the definition of expectations: assertions that take effect in development and testing environments, but are optimised away to have zero cost in production. While assert_options() can still be used to control behaviour as described above for backward compatibility reasons, PHP 7 only code should use the two new configuration directives to control the behaviour of assert() and not call assert_options(). PHP 7 configuration directives for assert()
Parametersassertion The assertion. In PHP 5, this must be either a string to be evaluated or a bool to be tested. In PHP 7, this may also be any expression that returns a value, which will be executed and the result used to indicate whether the assertion succeeded or failed. Warning Using string as the description An optional description that will be included in the failure message if the exception In PHP 7, the second parameter can be a Throwable object instead of a descriptive string, in which case this is the object that will be thrown if the assertion fails and the assert.exception configuration directive is enabled. Return Values
Changelog
ExamplesTraditional assertions (PHP 5 and 7)Example #1 Handle a failed assertion with a custom handler
Example #2 Using a custom handler to print a description
The above example will output: Assertion failed at test.php:21: 2 < 1 Assertion failed at test.php:22: 2 < 1: Two is less than one Expectations (PHP 7 only)Example #3 Expectations without a custom exception
With zend.assertions set to 0, the above example will output: With zend.assertions set to 1 and assert.exception set to 0, the above example will output: Warning: assert(): assert(true == false) failed in - on line 2 Hi! With zend.assertions set to 1 and assert.exception set to 1, the above example will output: Fatal error: Uncaught AssertionError: assert(true == false) in -:2 Stack trace: #0 -(2): assert(false, 'assert(true == ...') #1 {main} thrown in - on line 2 Example #4 Expectations with a custom exception
With zend.assertions set to 0, the above example will output: With zend.assertions set to 1 and assert.exception set to 0, the above example will output: Warning: assert(): CustomError: True is not false! in -:4 Stack trace: #0 {main} failed in - on line 4 Hi! With zend.assertions set to 1 and assert.exception set to 1, the above example will output: Fatal error: Uncaught CustomError: True is not false! in -:4 Stack trace: #0 {main} thrown in - on line 4 hodgman at ali dot com dot au ¶ 14 years ago
Tom ¶ 2 years ago
mail<at>aaron-mueller.de ¶ 15 years ago
(ASSERT_ACTIVE, 1); Krzysztof 'ChanibaL' Bociurko ¶ 14 years ago
Julien MOREAU aka PixEye ¶ 7 months ago
jason at jaypha dot com ¶ 4 years ago
uramihsayibok, gmail, com ¶ 12 years ago
('is_int($int) /* $int parameter must be an int, not just numeric */');// and my personal favorite office dot stojmenovic at gmail dot com ¶ 8 years ago
assert($assertion, $description); Ben ¶ 6 years ago
|