PHP 7 abnormal
PHP 7 exception for backward compatibility and enhanced the old assert () function. It can achieve zero cost assertion in a production environment, and to provide an exception is thrown and the ability to customize error.
Old versions of the API for compatibility purposes will continue to be maintained, assert () is now a language structure that allows the first argument is an expression, not just a string to be calculated to be tested or a boolean.
assert () Configuration
Configuration Item | Defaults | Available Values |
---|---|---|
zend.assertions | 1 |
|
assert.exception | 0 |
|
parameter
- assertion
assertion. In PHP 5, it is a Boolean value to a string for execution or for testing. In PHP 7, you can return a value of any expression, it will be used to indicate the results of the assertion is successful.
- description
If the
assertion
fails, the option will be included in the description in the failure information.- exception
In PHP 7, the second parameter can be a Throwable object rather than a string, if the assertion fails and enabled assert.exception the object will be thrown.
Examples
The zend.assertions set to 0:
Examples
ini_set ( 'zend.assertions', 0) ;
assert (true == false);
echo 'Hi!';
?>
The above program execution output is:
Hi!
The zend.assertions set to 1, assert.exception set to 1:
Examples
ini_set ( 'zend.assertions', 1) ;
ini_set ( 'assert.exception', 1) ;
assert (true == false);
echo 'Hi!';
?>
The above program execution output is:
Fatal error: Uncaught AssertionError: assert(true == false) in -:2 Stack trace: #0 -(2): assert(false, 'assert(true == ...') #1 {main} thrown in - on line 2