PHP.nl

assert

assert

Checks an assertion

bool **assert** mixed $assertion  $description

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. assert

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 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. not

will check that the expectation given in
holds.

If not, and thus the result is false, it will take the appropriate action depending on how was configured. assert``assertion``assert

The behaviour of is dictated by the following INI settings:

assert

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.

Waarschuwing: > Prior to PHP 8.0.0, if was a it was interpreted as PHP code and executed via . This string would be passed to the callback as the third argument. This behaviour was in PHP 7.2.0, and in PHP 8.0.0. assertion``string``evalDEPRECATED**REMOVED

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

  `description``Throwable``assertion`> **Opmerking:** > As of PHP 8.0.0, this is done  to calling
     the potentially defined assertion callback.
    *prior*

Opmerking: > As of PHP 8.0.0, the object will be thrown regardless of the configuration of . assert.exception

Opmerking: > As of PHP 8.0.0, the

     setting has no effect in this case.
    assert.bail
   If  is 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
   the  fails.
  `description``assertion`


   If  is omitted.
   
   A default description equal to the source code for the invocation of
    is created at compile time.
  `description``assert`




will always return true if at least one of the following is true:

assert

zend.assertions=0``zend.assertions=-1``assert.active=0``assert.exception=1``assert.bail=1``description If none of the conditions are true will return true if is truthy and false otherwise. assert``assertion

Voorbeeld: example

<?php
assert(1 > 2);
echo 'Hi!';
 If assertions are enabled ()
 the above example will output:
zend.assertions=1
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
 If assertions are disabled ( or )
 the above example will output:
`zend.assertions=0``zend.assertions=-1`
Hi!

Voorbeeld: 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 2

If assertions are disabled the above example will output:

Hi!

Voorbeeld: 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 4

If assertions are disabled the above example will output:

Hi!

assert_options