PHP built-in unit function that can be used to debug echo

Source: Internet
Author: User
Today, I found a function assert and assert_options, which can complete a simple phpunit function in combination, but it is too simple, so it is not very useful, but I should record it. the important title is a prompt that you cannot define your own mistakes.

Today, I found a function assert and assert_options, which can complete a simple phpunit function in combination, but it is too simple, so it is not very useful, but I should record it.

The important title is a prompt message that cannot be customized incorrectly. only the file and number of rows of the title can be displayed.

For more information, see <> or <>

At the same time, you can combine the things in the chapter "XXVII. Error Handling and Logging Functions" to apply them together.

The following is a test file I wrote that contains all the test functions. However, ASSERT_QUIET_EVAL has never been quite clear about the specific function of ASSERT_QUIET_EVAL.

Function assert_failed ($ file, $ line, $ expr ){
Print 'assertion failed in $ file [$ line]: $ expr
';
}
// Set error_reporting to 0, which is equivalent to calling assert_options (ASSERT_WARNING, 0 );
// Error_reporting (0 );
// Whether to enable support for ASSERT_ACTIVE
Assert_options (ASSERT_ACTIVE, 1 );
// Whether the script is executed when the first wanning is sent
Assert_options (ASSERT_BAIL, 0 );
// I don't know how to use it. I can't test it.
// Assert_options (ASSERT_QUIET_EVAL, 0 );
Echo 'Step 1
';
Assert (1 = 1 );
Echo 'Step 2
';
Assert (2 = 1 );
Echo 'step 3
';
// Set the callback style of assert. you can customize the style when wanning information is displayed.
Assert_options (ASSERT_CALLBACK, 'assert _ failed ');
// Assert () is not displayed. if ASSERT_CALLBACK is set, the information of the ASSERT_CALLBACK function is still displayed, but the $ expr parameter in the function does not work.
// Assert_options (ASSERT_WARNING, 1 );
Assert (1 = 1 );
Assert (1/0)> 2 );
Echo 'step 4
';
?>

The following section is directly copied from

The assert () function is a clever one that works along the same lines as our print statements, but it only works if a certain condition is not matched. essentially, assert () is used to say 'This statement must be trueif it isn' t, please tell me. 'For example:
Print 'stage 1 \ n ';
Assert (1 = 1 );
Print 'stage 2 \ n ';
Assert (1 = 2 );
Print 'Stage 3 \ n ';

Here we have two assert () s, with the first call asserting that one must be equal to one, and the second call asserting that one must be equal to two. as it is impossible to redefine constants like 1 and 2, the first assert () will always evaluate to true, and the second will always evaluate to false. here is the output from the script:
Stage 1
Stage 2
Warning: assert () [http://www.php.net/function.assert?: Assertion failed
In/home/paul/sandbox/php/assert. php on line 5
Stage 3

The first assert () is not seen in the output at all because it evaluated to TRue, whereas the second assert () evaluated to false, so we get a warning about an assertion failure. however, script execution carries on so that we see 'stage 3' after the assertion failure warning. as long as assertions evaluate to true, they have no effect on the running of the script, which means you can insert them for debugging purposes and not have to worry about taking them out once you are finished debugging.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.