search
HomeBackend DevelopmentPHP7How to properly use PHP7 expectations

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()

##InstructionsDefault valuePossible valueszend.assertions1assert.exception0

1 \- Generate and execute code (development mode )

0 \- Generate code, but skip it at runtime

-1 \- Do not generate code (production mode)

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)

Parameters:


  • Assertion - Assertion. In

    PHP 5, it must be a string to be evaluated or a boolean value to be tested. In PHP 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 a Throwable object instead of a descriptive string, in which case This is the object that will be thrown if the assertion fails and the assert.exception configuration directive is enabled.

Return value

If the assertion is false, it returns

FALSE, otherwise it returns TRUE .

Example:

<?php
   ini_set(&#39;assert.exception&#39;, 1);

   class CustomError extends AssertionError {}

   assert(false, new CustomError(&#39;Custom Error Message!&#39;));
?>

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function