assert

FunctionParams and return types changed in PHP 8.0

Checks an assertion.

PHP 5
PHP 7.0
Improved
PHP 7.1-7.4
PHP 8.0
Improved
PHP 8.1
PHP 8.2
PHP 8.3
PHP 8.4
PHP 8.5

assert Function synopsis

assert(mixed $assertion, Throwable|string|null $description = null): bool

Parameters

$assertion

Typemixed

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

OptionalTypeThrowable|string|nullDefault valuenull

If $description is an instance of Throwable, it will be thrown only if the $assertion is executed and fails.

Return value

Typebool

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.

Changes to the assert Function

PHP 8.0

  • Return type added: bool
  • Parameter type added for parameter #1 ($assertion): mixed
  • Parameter type added for parameter #2 ($description): Throwable|string|null
  • Parameter default value added for position #2 ($description): null
- assert($assertion, $description)
+ assert(mixed $assertion, Throwable|string|null $description = null): bool

PHP 7.0

  • Optional parameter added - #2: $description
- assert($assertion)
+ assert($assertion, $description)

assert Function Availability

PHP VersionAvailability
PHP 8.5Future Release Yes
PHP 8.4Upcoming Release Yes
PHP 8.3Supported (Latest) Yes
PHP 8.2Supported Yes
PHP 8.1Security-Fixes Only Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes