php7Expect backward-compatible enhancements to the old assert() function. Expectations allow zero-cost assertions in production code and provide the ability to throw custom exceptions when assertions fail. assert() is not a language construct where the first argument is a comparison of an expression string or boolean for testing.
Configuration directive assert()
| Default value | Possible values | |
| 1 | 1 \- Generate and execute code (development mode ) 0 \- Generate code, but skip it at runtime -1 \- Do not generate code (production mode) | |
| 0 | 1 \- Throws the exception object provided when the assertion fails, or throws a new AssertionError object if no exception is provided. . 0 \- Use or generate a Throwable as above, but only generate a warning based on that object instead of throwing it (compatible with PHP 5 behavior) |
- Assertion - Assertion. In
PHP 5
, it must be astringto be evaluated or aboolean valueto be tested. InPHP 7, this can also be any expression that returns a value that will be executed and the result used to indicate whether the assertion succeeded or failed. - Description - An optional description that will be included in the failure message if the assertion fails.
- Exception - In
PHP 7
, the second parameter can be aThrowableobject instead of a descriptive string, in which case This is the object that will be thrown if the assertion fails and theassert.exceptionconfiguration directive is enabled.
FALSE, otherwise it returns TRUE .
<?php
ini_set('assert.exception', 1);
class CustomError extends AssertionError {}
assert(false, new CustomError('Custom Error Message!'));
?>It produces the following browser output:Fatal error: Uncaught CustomError: Custom Error Message! in...Recommended:
php video tutorial php7 tutorial
The above is the detailed content of How to properly use PHP7 expectations. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor





