Why do we use assert() and assert_options() in PHP?

后端 未结 2 1037
情歌与酒
情歌与酒 2021-02-05 21:25

I am new to using PHP and am learning it by reading the documentation on php.net- currently the page for assert() to know about those assert()

2条回答
  •  余生分开走
    2021-02-05 21:31

    The assert() function is a good way to ensure certain conditions are true throughout the life of your code. To quote this article by Paul Hudson:

    Essentially, assert() is used to say "This statement must be true - if it isn't, please tell me".

    To enable assertion handling, use assert_options(ASSERT_ACTIVE), and also use assert_options() with other arguments to control what happens when assertions fail (e.g. ending the PHP script execution when an assertion fails, calling a handler function- which could be used to send emails, log data in files and/or database tables, etc.). Refer to the parameters section for the list of all options and their outcomes.

    Try out some of the options in this playground example.

    Read that article for more information about both of those functions.

提交回复
热议问题