assert

PHP 4, PHP 5, PHP 7, PHP 8
assert - Checks if assertion is false
Manual
Code Examples

assert( mixed$assertion, [string$description] ): bool

PHP 7

boolassert mixedassertion Throwableexception

Parameters

assertion

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 assertion is DEPRECATED as of PHP 7.2.0 and REMOVED as of PHP 8.0.0.

description

An optional description that will be included in the failure message if the assertion fails. From PHP 7, if no description is provided, a default description equal to the source code for the invocation of assert is provided.

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

false if the assertion is false, true otherwise.

Changelog

Version Description
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 effect.
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.
7.0.0 assert is now a language construct and not a function. assertion can now be an expression. The second parameter is now interpreted either as an exception (if a Throwable object is given), or as the description supported from PHP 5.4.8 onwards.

Related Functions

Example of assert

Show all examples for assert

PHP Version: