Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 2103

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Checks if assertion is FALSE</title>

 </head>
 <body><div class="manualnavbar" 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.cli-get-process-title.html">cli_get_process_title</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 <strong><code>FALSE</code></strong></span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.assert-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>assert</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$assertion</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$description</code></span>
  ] )</div>

  <p class="para rdfs-comment">
    <span class="function"><strong>assert()</strong></span> will check the given
   <em><code class="parameter">assertion</code></em> and take appropriate action if
   its result is <strong><code>FALSE</code></strong>.
  </p>
  <p class="para">
   If the <em><code class="parameter">assertion</code></em> is given as a string it
   will be evaluated as PHP code by  <span class="function"><strong>assert()</strong></span>.
   The advantages of a string <em><code class="parameter">assertion</code></em> are
   less overhead when assertion checking is off and messages
   containing the <em><code class="parameter">assertion</code></em> expression when
   an assertion fails. This means that if you pass a boolean condition
   as <em><code class="parameter">assertion</code></em> this condition will not show up as
   parameter to the assertion function which you may have defined with the
    <span class="function"><a href="function.assert-options.html" class="function">assert_options()</a></span> function, the condition is converted
   to a string before calling that handler function, and the boolean <strong><code>FALSE</code></strong>
   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 <strong><code>TRUE</code></strong> 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  <span class="function"><strong>assert()</strong></span> may be configured by
    <span class="function"><a href="function.assert-options.html" class="function">assert_options()</a></span> or by .ini-settings described
   in that functions manual page.
  </p>
  <p class="para">
   The  <span class="function"><a href="function.assert-options.html" class="function">assert_options()</a></span> function and/or
   <strong><code>ASSERT_CALLBACK</code></strong> configuration directive allow a
   callback function to be set to handle failed assertions.
  </p>
  <p class="para">
    <span class="function"><strong>assert()</strong></span> 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). Users of PHP 5.4.8 and later may also provide a fourth
   optional argument, which will contain the
   <em><code class="parameter">description</code></em> given to  <span class="function"><strong>assert()</strong></span>, if
   it was set.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.assert-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">assertion</code></em></span>
     <dd>

      <p class="para">
       The assertion.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">description</code></em></span>
     <dd>

      <p class="para">
       An optional description that will be included in the failure message if
       the <em><code class="parameter">assertion</code></em> fails.
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.assert-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   <strong><code>FALSE</code></strong> if the assertion is false, <strong><code>TRUE</code></strong> otherwise.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.assert-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.4.8</td>
       <td>
        The <em><code class="parameter">description</code></em> parameter was added. The
        <em><code class="parameter">description</code></em> is also now provided to a callback
        function in <strong><code>ASSERT_CALLBACK</code></strong> mode as the fourth
        argument.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.assert-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-478">
    <p><strong>Example #1 Handle a failed assertion with a custom handler</strong></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;'</span><span style="color: #0000BB">$file</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line&nbsp;'</span><span style="color: #0000BB">$line</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Code&nbsp;'</span><span style="color: #0000BB">$code</span><span style="color: #DD0000">'&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>
  <p class="para">
   <div class="example" id="example-479">
    <p><strong>Example #2 Using a custom handler to print a description</strong></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">,&nbsp;</span><span style="color: #0000BB">$desc&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Assertion&nbsp;failed&nbsp;at&nbsp;</span><span style="color: #0000BB">$file</span><span style="color: #DD0000">:</span><span style="color: #0000BB">$line</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #0000BB">$code</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$desc</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">":&nbsp;</span><span style="color: #0000BB">$desc</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n"</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">'2&nbsp;&lt;&nbsp;1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #DD0000">'2&nbsp;&lt;&nbsp;1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Two&nbsp;is&nbsp;less&nbsp;than&nbsp;one'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Assertion failed at test.php:21: 2 &lt; 1
Assertion failed at test.php:22: 2 &lt; 1: Two is less than one
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.assert-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.assert-options.html" class="function" rel="rdfs-seeAlso">assert_options()</a> - Set/get the various assert flags</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" 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.cli-get-process-title.html">cli_get_process_title</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>