assert()
assert() — Checks an assertion.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
The behaviour of assert() is dictated by the following INI settings: Assert Configure Options 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: function assert_callback( string $file, int $line, null $assertion, string $description = ?): void Prior to PHP 8.0.0, the signature of the callback should be: function assert_callback( string $file, int $line, string $assertion, string $description = ?): void Deprecated as of PHP 8.3.0. assert.exception true If true will throw an AssertionError if the expectation isn't ...
Signature
assert(mixed $assertion, Throwable|string|null $description = null): bool assert_callback(string $file, int $line, null $assertion, string $description = ?): void assert_callback(string $file, int $line, string $assertion, string $description = ?): void
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $assertion | mixed | This is any expression that returns a value, which will be executed and the result is used to indicate whether the assertion succeeded or failed. |
| $description | Throwable|string|null | If description is an instance of Throwable, it will be thrown only if the assertion is executed and fails. Note: As of PHP 8.0.0, this is done prior to calling the potentially defined assertion callback. Note: As of PHP 8.0.0, the object will be thrown regardless of the configuration of assert.exception. Note: As of PHP 8.0.0, the assert.bail setting has no effect in this case. |
Return value
assert() will always return true if at least one of the following is true: If none of the conditions are true assert() will return true if assertion is truthy and false otherwise.
Example
A runnable snippet:
assert(1 > 2); echo 'Hi!';
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 2
Changelog
| Version | Change |
|---|---|
| 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 php.ini directive and the ASSERT_QUIET_EVAL constant have also been removed, as they would no longer have any ... |
| 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. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page assert() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.