assert
(PHP 4, PHP 5, PHP 7, PHP 8)
Checks an assertion
Description
assert() allows for the definition of expectations: assertions that take effect in development and testing environments, but are optimised away to have zero cost in production.
Assertions can be used to aid debugging. One use case for them is to act as sanity-checks for preconditions that should always be true and that if they aren't upheld this indicates some programming errors. Another use case is to ensure the presence of certain features like extension functions or certain system limits and features.
As assertions can be configured to be eliminated, they should not be used for normal runtime operations like input parameter checks. As a rule of thumb code should behave as expected even if assertion checking is deactivated.
assert() will check that the expectation given in assertion holds. If not, and thus the result is false, it will take the appropriate action depending on how assert() was configured.
The behaviour of assert() is dictated by the following INI settings:
| Name | Default | Description | Changelog |
|---|---|---|---|
| zend.assertions | 1 | 1: generate and execute code (development mode)0: generate code but jump around it at runtime-1: do not generate code (production mode) | |
| assert.active | true | If false, assert() does not check the expectation and returns true, unconditionally. | Deprecated as of PHP 8.3.0. |
| assert.callback | null | A user defined function to call when an assertion fails. Its signature should be: voidassert_callbackstringfileintlinenullassertionstringdescription | Prior to PHP 8.0.0, the signature of the callback should be: voidassert_callbackstringfileintlinestringassertionstringdescriptionDeprecated as of PHP 8.3.0. |
| assert.exception | true | If true will throw an AssertionError if the expectation isn't upheld. | Deprecated as of PHP 8.3.0. |
| assert.bail | false | If true will abort execution of the PHP script if the expectation isn't upheld. | Deprecated as of PHP 8.3.0. |
| assert.warning | true | If true, will emit an E_WARNING if the expectation isn't upheld. This INI setting is ineffective if assert.exception is enabled. | Deprecated as of PHP 8.3.0. |
Parameters
assertionThis is any expression that returns a value, which will be executed and the result is used to indicate whether the assertion succeeded or failed.
WarningPrior to PHP 8.0.0, if
assertionwas astringit was interpreted as PHP code and executed viaeval(). This string would be passed to the callback as the third argument. This behaviour was DEPRECATED in PHP 7.2.0, and REMOVED in PHP 8.0.0.descriptionIf
descriptionis an instance ofThrowable, it will be thrown only if theassertionis executed and fails.NoteAs of PHP 8.0.0, this is done prior to calling the potentially defined assertion callback.
NoteAs of PHP 8.0.0, the Object will be thrown regardless of the configuration of assert.exception.
NoteAs of PHP 8.0.0, the assert.bail setting has no effect in this case.
If
descriptionis a String this message will be used if an exception or a warning is emitted. An optional description that will be included in the failure message if theassertionfails.If
descriptionis omitted. A default description equal to the source code for the invocation ofassert()is created at compile time.
Return Values
assert() will always return true if at least one of the following is true:
zend.assertions=0zend.assertions=-1assert.active=0
If none of the conditions are true assert() will return true if assertion is truthy and false otherwise.
Changelog
| Version | Description |
|---|---|
| 8.3.0 | All assert. INI settings have been deprecated. |
| 8.0.0 | assert() will no longer evaluate string arguments, instead they will be treated like any other argument. assert($a == $b) should be used instead of assert('$a == $b'). The assert.quiet_eval Ini directive and the ASSERT_QUIET_EVAL constant have also been removed, as they would no longer have any effect. |
| 8.0.0 | If description is an instance of Throwable, the object is thrown if the assertion fails, regardless of the value of assert.exception. |
| 8.0.0 | If description is an instance of Throwable, no user callback is called even if it set. |
| 8.0.0 | Declaring a function called assert() inside a namespace is no longer allowed, and issues E_COMPILE_ERROR. |
| 7.3.0 | Declaring a function called assert() inside a namespace became deprecated. Such declaration now emits an E_DEPRECATED. |
| 7.2.0 | Usage of a string as the assertion became deprecated. It now emits an E_DEPRECATED notice when both assert.active and zend.assertions are set to 1. |
Examples
assert() example
<?php
assert(1 > 2);
echo 'Hi!';If assertions are enabled (zend.assertions=1) the above example will output:
Fatal error: Uncaught AssertionError: assert(1 > 2) in example.php:2
Stack trace:
#0 example.php(2): assert(false, 'assert(1 > 2)')
#1 {main}
thrown in example.php on line 2If assertions are disabled (zend.assertions=0 or zend.assertions=-1) the above example will output:
Hi!Using a custom message
<?php
assert(1 > 2, "Expected one to be greater than two");
echo 'Hi!';If assertions are enabled the above example will output:
Fatal error: Uncaught AssertionError: Expected one to be greater than two in example.php:2
Stack trace:
#0 example.php(2): assert(false, 'Expected one to...')
#1 {main}
thrown in example.php on line 2If assertions are disabled the above example will output:
Hi!Using a custom exception class
<?php
class ArithmeticAssertionError extends AssertionError {}
assert(1 > 2, new ArithmeticAssertionError("Expected one to be greater than two"));
echo 'Hi!';If assertions are enabled the above example will output:
Fatal error: Uncaught ArithmeticAssertionError: Expected one to be greater than two in example.php:4
Stack trace:
#0 {main}
thrown in example.php on line 4If assertions are disabled the above example will output:
Hi!