Latest web development tutorials

PHP 7 abnormal

PHP 7 New Features PHP 7 New Features

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
  • 1-- generate and execute code (development model)

  • 0 - generate code, but skips it during execution

  • -1-- Not generate code (production)

assert.exception 0
  • 1-- thrown when an assertion fails, you can throw an exception object, if no exception is thrown AssertionError object instance.

  • 0 - used or generated Throwable, only a warning is generated based on the object rather than throwing an object (PHP 5-compatible)

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

<? php
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

<? php
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

PHP 7 New Features PHP 7 New Features