<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.info.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.assert.php',
    1 => 'assert',
    2 => 'Checks an assertion',
  ),
  'up' => 
  array (
    0 => 'ref.info.php',
    1 => 'PHP Options/Info Functions',
  ),
  'prev' => 
  array (
    0 => 'ref.info.php',
    1 => 'PHP Options/Info Functions',
  ),
  'next' => 
  array (
    0 => 'function.assert-options.php',
    1 => 'assert_options',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/info/functions/assert.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.assert" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">assert</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">assert</span> &mdash; <span class="dc-title">Checks an assertion</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.assert-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>assert</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$assertion</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="class.throwable.php" class="type Throwable">Throwable</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span> <code class="parameter">$description</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>assert()</strong></span>
   allows for the definition of expectations: assertions that take effect
   in development and testing environments, but are optimised away to have
   zero cost in production.
  </p>
  <p class="para">
   Assertions can be used to aid debugging.
   One use case for them is to act as sanity-checks for preconditions
   that should always be <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> and that if they aren&#039;t upheld this indicates
   some programming errors.
   Another use case is to ensure the presence of certain features like
   extension functions or certain system limits and features.
  </p>
  <p class="para">
   As assertions can be configured to be eliminated, they should
   <em>not</em> be used for normal runtime operations like
   input parameter checks. As a rule of thumb code should behave as expected
   even if assertion checking is deactivated.
  </p>
  <p class="para">
   <span class="function"><strong>assert()</strong></span> will check that the expectation given in
   <code class="parameter">assertion</code> holds.
   If not, and thus the result is <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, it will take the appropriate action
   depending on how <span class="function"><strong>assert()</strong></span> was configured.
  </p>

  <p class="para">
   The behaviour of <span class="function"><strong>assert()</strong></span> is dictated by the
   following INI settings:
   <table class="doctable table">
    <caption><strong>Assert Configure Options</strong></caption>
    
     <thead>
      <tr>
       <th>Name</th>
       <th>Default</th>
       <th>Description</th>
       <th>Changelog</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><a href="ini.core.php#ini.zend.assertions" class="link">zend.assertions</a></td>
       <td><code class="literal">1</code></td>
       <td>
        <ul class="simplelist">
         <li>
          <code class="literal">1</code>: generate and execute code (development mode)
         </li>
         <li>
          
          <code class="literal">0</code>: generate code but jump around it at runtime
         </li>
         <li>
          <code class="literal">-1</code>: do not generate code (production mode)
         </li>
        </ul>
       </td>
       <td class="empty">&nbsp;</td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.active" class="link">assert.active</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td>
        If <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, <span class="function"><strong>assert()</strong></span> does not check the expectation
        and returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, unconditionally.
       </td>
       <td>
        Deprecated as of PHP 8.3.0.
       </td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.callback" class="link">assert.callback</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td>
       <td>
        <p class="para">
         A user defined function to call when an assertion fails.
         Its signature should be:
         <div class="methodsynopsis dc-description">
          <span class="methodname"><strong>assert_callback</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$file</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$line</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.null.php" class="type null">null</a></span> <code class="parameter">$assertion</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$description</code><span class="initializer"> = ?</span></span><br>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

        </p>
       </td>
       <td>
        <p class="para">
         Prior to PHP 8.0.0, the signature of the callback should be:
         <div class="methodsynopsis dc-description"><span class="methodname"><strong>assert_callback</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$file</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$line</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$assertion</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$description</code><span class="initializer"> = ?</span></span><br>): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>

        </p>
        <span class="simpara">
         Deprecated as of PHP 8.3.0.
        </span>
       </td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.exception" class="link">assert.exception</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td>
        If <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> will throw an <span class="classname"><a href="class.assertionerror.php" class="classname">AssertionError</a></span> if the
        expectation isn&#039;t upheld.
       </td>
       <td>
        Deprecated as of PHP 8.3.0.
       </td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.bail" class="link">assert.bail</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></td>
       <td>
        If <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> will abort execution of the PHP script if the
        expectation isn&#039;t upheld.
       </td>
       <td>
        Deprecated as of PHP 8.3.0.
       </td>
      </tr>

      <tr>
       <td><a href="info.configuration.php#ini.assert.warning" class="link">assert.warning</a></td>
       <td><strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></td>
       <td>
        If <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, will emit an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> if the
        expectation isn&#039;t upheld. This INI setting is ineffective if
        <a href="info.configuration.php#ini.assert.exception" class="link">assert.exception</a>
        is enabled.
       </td>
       <td>
        Deprecated as of PHP 8.3.0.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.assert-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">assertion</code></dt>
     <dd>
      <p class="para">
       This is any expression that returns a value, which will be executed
       and the result is used to indicate whether the assertion succeeded or failed.
      </p>

      <div class="warning"><strong class="warning">Warning</strong>
       <p class="para">
        Prior to PHP 8.0.0, if <code class="parameter">assertion</code> was a
        <span class="type"><a href="language.types.string.php" class="type string">string</a></span> it was interpreted as PHP code and executed via
        <span class="function"><a href="function.eval.php" class="function">eval()</a></span>.
        This string would be passed to the callback as the third argument.
        This behaviour was <em>DEPRECATED</em> in PHP 7.2.0,
        and <em>REMOVED</em> in PHP 8.0.0.
       </p>
      </div>
     </dd>
    
    
     <dt><code class="parameter">description</code></dt>
     <dd>
      <p class="para">
       If <code class="parameter">description</code> is an instance of
       <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span>, it will be thrown only if the
       <code class="parameter">assertion</code> is executed and fails.
       <blockquote class="note"><p><strong class="note">Note</strong>: 
        <p class="para">
         As of PHP 8.0.0, this is done <em>prior</em> to calling
         the potentially defined assertion callback.
        </p>
       </p></blockquote>
       <blockquote class="note"><p><strong class="note">Note</strong>: 
        <p class="para">
         As of PHP 8.0.0, the <span class="type"><a href="language.types.object.php" class="type object">object</a></span> will be thrown regardless of the configuration of
         <a href="info.configuration.php#ini.assert.exception" class="link">assert.exception</a>.
        </p>
       </p></blockquote>
       <blockquote class="note"><p><strong class="note">Note</strong>: 
        <p class="para">
         As of PHP 8.0.0, the
         <a href="info.configuration.php#ini.assert.bail" class="link">assert.bail</a>
         setting has no effect in this case.
        </p>
       </p></blockquote>
      </p>
      <p class="para">
       If <code class="parameter">description</code> is a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> this message
       will be used if an exception or a warning is emitted.
       An optional description that will be included in the failure message if
       the <code class="parameter">assertion</code> fails.
      </p>
      <p class="para">
       If <code class="parameter">description</code> is omitted.
       
       A default description equal to the source code for the invocation of
       <span class="function"><strong>assert()</strong></span> is created at compile time.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.assert-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   <span class="function"><strong>assert()</strong></span> will always return <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if at least one of the following is true:
  </p>
  <ul class="simplelist">
   <li><code class="literal">zend.assertions=0</code></li>
   <li><code class="literal">zend.assertions=-1</code></li>
   <li><code class="literal">assert.active=0</code></li>
   <li><code class="literal">assert.exception=1</code></li>
   <li><code class="literal">assert.bail=1</code></li>
   <li>A custom exception object is passed to <code class="parameter">description</code>.</li>
  </ul>
  <p class="para">
   If none of the conditions are true <span class="function"><strong>assert()</strong></span> will return <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> if
   <code class="parameter">assertion</code> is truthy and <strong><code><a href="reserved.constants.php#constant.false">false</a></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>8.3.0</td>
       <td>
        All <code class="literal">assert.</code> INI settings have been deprecated.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
         <span class="function"><strong>assert()</strong></span> will no longer evaluate string arguments, instead they will be
         treated like any other argument. <code class="code">assert($a == $b)</code> should be used instead of
         <code class="code">assert(&#039;$a == $b&#039;)</code>. The <code class="literal">assert.quiet_eval</code> <var class="filename">php.ini</var> directive and
         the <strong><code><a href="info.constants.php#constant.assert-quiet-eval">ASSERT_QUIET_EVAL</a></code></strong> constant have also been removed, as they would no longer
         have any effect.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        If <code class="parameter">description</code> is an instance of
        <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span>, the object is thrown if the assertion
        fails, regardless of the value of
        <a href="info.configuration.php#ini.assert.exception" class="link">assert.exception</a>.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        If <code class="parameter">description</code> is an instance of
        <span class="classname"><a href="class.throwable.php" class="classname">Throwable</a></span>, no user callback is called even
        if it set.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        Declaring a function called <code class="literal">assert()</code> inside a namespace is
        no longer allowed, and issues <strong><code><a href="errorfunc.constants.php#constant.e-compile-error">E_COMPILE_ERROR</a></code></strong>.
       </td>
      </tr>

      <tr>
       <td>7.3.0</td>
       <td>
        Declaring a function called <code class="literal">assert()</code> inside a namespace
        became deprecated. Such declaration now emits an <strong><code><a href="errorfunc.constants.php#constant.e-deprecated">E_DEPRECATED</a></code></strong>.
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        Usage of a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> as the <code class="parameter">assertion</code>
        became deprecated. It now emits an <strong><code><a href="errorfunc.constants.php#constant.e-deprecated">E_DEPRECATED</a></code></strong>
        notice when both <a href="info.configuration.php#ini.assert.active" class="link">assert.active</a>
        and <a href="ini.core.php#ini.zend.assertions" class="link">zend.assertions</a> are set
        to <code class="literal">1</code>.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.assert-examples">
  <h3 class="title">Examples</h3>
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>assert()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />assert</span><span style="color: #007700">(</span><span style="color: #0000BB">1 </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">'Hi!'</span><span style="color: #007700">;</span></span></code></div>
    </div>

    <div class="example-contents"><p>
     If assertions are enabled (<a href="ini.core.php#ini.zend.assertions" class="link"><code class="literal">zend.assertions=1</code></a>)
     the above example will output:
    </p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Fatal error: Uncaught AssertionError: assert(1 &gt; 2) in example.php:2
Stack trace:
#0 example.php(2): assert(false, &#039;assert(1 &gt; 2)&#039;)
#1 {main}
  thrown in example.php on line 2</pre>
</div>
    </div>
    <div class="example-contents"><p>
     If assertions are disabled (<code class="literal">zend.assertions=0</code> or <code class="literal">zend.assertions=-1</code>)
     the above example will output:
    </p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Hi!</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-2">
    <p><strong>Example #2 Using a custom message</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />assert</span><span style="color: #007700">(</span><span style="color: #0000BB">1 </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">"Expected one to be greater than two"</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">'Hi!'</span><span style="color: #007700">;</span></span></code></div>
    </div>

    <div class="example-contents"><p>
     If assertions are enabled the above example will output:
    </p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Fatal error: Uncaught AssertionError: Expected one to be greater than two in example.php:2
Stack trace:
#0 example.php(2): assert(false, &#039;Expected one to...&#039;)
#1 {main}
  thrown in example.php on line 2</pre>
</div>
    </div>
    <div class="example-contents"><p>
     If assertions are disabled the above example will output:
    </p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Hi!</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-3">
    <p><strong>Example #3 Using a custom exception class</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: #007700">class </span><span style="color: #0000BB">ArithmeticAssertionError </span><span style="color: #007700">extends </span><span style="color: #0000BB">AssertionError </span><span style="color: #007700">{}<br /><br /></span><span style="color: #0000BB">assert</span><span style="color: #007700">(</span><span style="color: #0000BB">1 </span><span style="color: #007700">&gt; </span><span style="color: #0000BB">2</span><span style="color: #007700">, new </span><span style="color: #0000BB">ArithmeticAssertionError</span><span style="color: #007700">(</span><span style="color: #DD0000">"Expected one to be greater than two"</span><span style="color: #007700">));<br />echo </span><span style="color: #DD0000">'Hi!'</span><span style="color: #007700">;</span></span></code></div>
    </div>

    <div class="example-contents"><p>
     If assertions are enabled the above example will output:
    </p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Fatal error: Uncaught ArithmeticAssertionError: Expected one to be greater than two in example.php:4
Stack trace:
#0 {main}
  thrown in example.php on line 4</pre>
</div>
    </div>
    <div class="example-contents"><p>
     If assertions are disabled the above example will output:
    </p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Hi!</pre>
</div>
    </div>
   </div>
 </div>


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


</div><?php manual_footer($setup); ?>