YouTip LogoYouTip

Php Expectations

# PHP 7 Exceptions [![Image 3: PHP 7 New Features](#) PHP 7 New Features](#) PHP 7 Exceptions are used for backward compatibility and to enhance the old `assert()` function. It enables zero-cost assertions in production environments and provides the ability to throw custom exceptions and errors. The old API will continue to be maintained for compatibility purposes. `assert()` is now a language construct that allows the first parameter to be an expression, not just a string to be evaluated or a boolean to be tested. * * * ## assert() Configuration | Configuration Item | Default Value | Optional Values | | --- | --- | --- | | zend.assertions | 1 | * **1** - Generate and execute code (development mode) * **0** - Generate code, but skip it during execution * **-1** - Do not generate code (production environment) | | assert.exception | 0 | * **1** - Throw on assertion failure, can throw an exception object. If no exception is provided, an AssertionError object instance is thrown. * **0** - Use or generate Throwable, generate warnings based on objects instead of throwing objects (compatible with PHP 5) | ### Parameters **assertion** The assertion. In PHP 5, it was a string to be executed or a boolean to be tested. In PHP 7, it can be an expression that returns any value. Its execution result is used to indicate whether the assertion succeeded. **description** If `assertion` fails, the optional `description` will be included in the failure message. **exception** In PHP 7, the second parameter can be a **Throwable** object instead of a string. If the assertion fails and `assert.exception` is enabled, this object will be thrown. ### Example Set `zend.assertions` to 0: ## Example The output of the above program execution is: Hi! Set `zend.assertions` to 1 and `assert.exception` to 1: ## Example The output of the above program execution is: Fatal error: Uncaught AssertionError: assert(true == false) in -:2Stack trace:#0 -(2): assert(false, 'assert(true == ...')#1 {main} thrown in - on line 2 * * PHP 7 New Features](#)
← Php Error HandlingPhp Expectations β†’