Home  >  Article  >  Backend Development  >  Example of assert() assertion detection function in PHP

Example of assert() assertion detection function in PHP

little bottle
little bottleforward
2019-04-18 13:21:162997browse

Assertion is a programming term, expressed as some Boolean expression, the programmer believes that the expression value is true at a certain point in the program, assertion verification can be enabled and disabled at any time, so assertions can be enabled during testing And disable assertions when deploying. Assertion functions are involved in many languages, and this article is about the assertion detection function in PHP. Friends who are interested can learn it together.

Similarly, after the program is put into operation, end users can re-enable assertions if they encounter problems.

assert_options function sets assertion parameters

assert function, detects an assertion

<?php
// 激活断言,并设置它为 quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
// 设置回调函数
assert_options(ASSERT_CALLBACK, &#39;my_assert_handler&#39;);
// 让一则断言失败
assert(&#39;mysql_query("")&#39;);
//创建处理函数
function my_assert_handler($file, $line, $code)
{
    echo "error:
        File &#39;$file&#39;
        Line &#39;$line&#39;
        Code &#39;$code&#39;";
}

Related courses: PHP video course

The above is the detailed content of Example of assert() assertion detection function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete