⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.assert.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Checks if assertion is FALSE</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.assert-options.html">assert_options</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.dl.html">dl</a></div> <div class="up"><a href="ref.info.html">PHP Options/Info Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.assert" class="refentry"> <div class="refnamediv">  <h1 class="refname">assert</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">assert</span> &mdash; <span class="dc-title">Checks if assertion is <b><tt>FALSE</tt></b></span></p> </div>  <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">bool</span> <span class="methodname"><b><b>assert</b></b></span>    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$assertion</tt></span>   )</div>  <p class="para rdfs-comment">   <b>assert()</b> will check the given   <i><tt class="parameter">assertion</tt></i> and take appropriate action if   its result is <b><tt>FALSE</tt></b>.  </p>  <p class="para">   If the <i><tt class="parameter">assertion</tt></i> is given as a string it   will be evaluated as PHP code by <b>assert()</b>.   The advantages of a string <i><tt class="parameter">assertion</tt></i> are   less overhead when assertion checking is off and messages   containing the <i><tt class="parameter">assertion</tt></i> expression when   an assertion fails. This means that if you pass a boolean condition   as <i><tt class="parameter">assertion</tt></i> this condition will not show up as   parameter to the assertion function which you may have defined with the   <a href="function.assert-options.html" class="function">assert_options()</a> function, the condition is converted   to a string before calling that handler function, and the boolean <b><tt>FALSE</tt></b>   is converted as the empty string.  </p>  <p class="para">   Assertions should be used as a debugging feature only. You may   use them for sanity-checks that test for conditions that should   always be <b><tt>TRUE</tt></b> and that indicate some programming errors if not   or to check for the presence of certain features like extension   functions or certain system limits and features.  </p>  <p class="para">   Assertions should not be used for normal runtime operations like   input parameter checks. As a rule of thumb your code should   always be able to work correctly if assertion checking is not   activated.  </p>  <p class="para">   The behavior of <b>assert()</b> may be configured by   <a href="function.assert-options.html" class="function">assert_options()</a> or by .ini-settings described   in that functions manual page.  </p>  <p class="para">   The <a href="function.assert-options.html" class="function">assert_options()</a> function and/or   ASSERT_CALLBACK configuration directive allow a callback function   to be set to handle failed assertions.  </p>  <p class="para">   <b>assert()</b> callbacks are particularly useful for   building automated test suites because they allow you to easily   capture the code passed to the assertion, along with information   on where the assertion was made. While this information can be   captured via other methods, using assertions makes it much faster   and easier!  </p>  <p class="para">   The callback function should accept three arguments. The first   argument will contain the file the assertion failed in. The   second argument will contain the line the assertion failed on and   the third argument will contain the expression that failed (if   any - literal values such as 1 or &quot;two&quot; will not be passed via   this argument)  </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">assertion</tt></i></span>     <dd>      <p class="para">       The assertion.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   <b><tt>FALSE</tt></b> if the assertion is false, <b><tt>TRUE</tt></b> otherwise.  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 Handle a failed assertion with a custom handler</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Active&nbsp;assert&nbsp;and&nbsp;make&nbsp;it&nbsp;quiet<br /></span><span style="color: #0000BB">assert_options</span><span style="color: #007700">(</span><span style="color: #0000BB">ASSERT_ACTIVE</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">assert_options</span><span style="color: #007700">(</span><span style="color: #0000BB">ASSERT_WARNING</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">assert_options</span><span style="color: #007700">(</span><span style="color: #0000BB">ASSERT_QUIET_EVAL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;a&nbsp;handler&nbsp;function<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">my_assert_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$code</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;hr&gt;Assertion&nbsp;Failed:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;'$file'&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line&nbsp;'$line'&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Code&nbsp;'$code'&lt;br&nbsp;/&gt;&lt;hr&nbsp;/&gt;"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Set&nbsp;up&nbsp;the&nbsp;callback<br /></span><span style="color: #0000BB">assert_options</span><span style="color: #007700">(</span><span style="color: #0000BB">ASSERT_CALLBACK</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_assert_handler'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Make&nbsp;an&nbsp;assertion&nbsp;that&nbsp;should&nbsp;fail<br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #DD0000">'mysql_query("")'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.assert-options.html">assert_options</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.dl.html">dl</a></div> <div class="up"><a href="ref.info.html">PHP Options/Info Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -