title: How Does PHP assert() Work Exactly?
body: <p>I was trying to do a simple PHP assertion to verify that a string is not empty, but got confusing results.</p>
  
  <p>I wasn't aware of the string evaluation feature in assert(), but checked the docs and understood that part.</p>
  
  <p>I included the code with the all the test cases, followed by the output, then followed by the questions. When a case fails with syntax error, I just comment it out and repeat the test so the following cases can get executed, and I show the output for each execution.</p>
  
  <p>The code is about locating an XML element using XMLReader, but that just happened to be my code. So here goes the code first:</p>
  
  <pre><code>&lt;?php
  
  $xml = &lt;&lt;&lt; XML
  &lt;depts  xmlns:apl="urn:my:ns" &gt;
      &lt;apl:dept&gt;Dept One&lt;/apl:dept&gt;
      &lt;apl:dept&gt;Dept Tow&lt;/apl:dept&gt;
  &lt;/depts&gt;
  XML;
  
  
  $elmLocal = 'dept';
  $elmUrn = "urn:my:ns";
  
  $xr = new XMLReader;
  $xr-&gt;XML($xml);
  
  // move to the first desired element node
  $found = false;
  while ($xr-&gt;read()) {
      if ($xr-&gt;localName === $elmLocal &amp;&amp; $xr-&gt;namespaceURI === $elmUrn) {
          $found = true;
          break;
      }
  }
  if (!$found) {
      exit("---Error--- No element found with given Name/NS.\n");
  }
  
  
  
  $var = $xr-&gt;name;
  
  echo "---------------- Test Results ----------------&gt;\n";
  
  echo "----- var    =={$var}==\n";
  echo "----- xr name=={$xr-&gt;name}==\n\n";
  
  
  echo "------ assertion 00 ------&gt;\n";
  assert ($xr-&gt;name);
  
  echo "------ assertion 01 ------&gt;\n";
  assert ($var);
  
  echo "------ assertion 02 ------&gt;\n";
  assert ('$xr-&gt;name!==""');   
  
  echo "------ assertion 03 ------&gt;\n";
  assert ('$var!==""');   
  
  echo "------ assertion 04 ------&gt;\n";
  assert ("{$xr-&gt;name}!==''");   
  
  echo "------ assertion 05 ------&gt;\n";
  assert ("{$var}!==''");   
  
  echo "------ assertion 06 ------&gt;\n";
  assert(!empty($xr-&gt;name));
  
  echo "------ assertion 07 ------&gt;\n";
  assert(!empty($var));
  
  echo "------ assertion 08 ------&gt;\n";
  assert('!empty($xr-&gt;name)');
  
  echo "------ assertion 09 ------&gt;\n";
  assert('!empty($var)');
  
  echo "------ assertion 10 ------&gt;\n";
  assert("!empty({$xr-&gt;name})");
  
  echo "------ assertion 11 ------&gt;\n";
  assert("!empty({$var})");
  
  
  
  // do some processing
  $domNode = $xr-&gt;expand(); // DOMNode XMLReader::expand ([ DOMNode $basenode ] )
  
  
  ?&gt;
  </code></pre>
  
  <p>And here goes the execution outputs - notice again that there are commenting out on fatal syntax failure and repeat execution, you can just assume that each assertion was tested separately:</p>
  
  <pre><code>#tests#php -v
  PHP 5.3.3-7+squeeze3 with Suhosin-Patch (cli) (built: Jun 28 2011 13:13:26)
  Copyright (c) 1997-2009 The PHP Group
  Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
      with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
      with Suhosin v0.9.32.1, Copyright (c) 2007-2010, by SektionEins GmbH
  
  
  #tests#php -q vars.php
  ---------------- Test Results ----------------&gt;
  ----- var    ==apl:dept==
  ----- xr name==apl:dept==
  
  ------ assertion 00 ------&gt;
  PHP Parse error:  syntax error, unexpected ':' in /shared/tests/vars.php(40) : assert code on line 1
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:40
  PHP Catchable fatal error:  assert(): Failure evaluating code:
  apl:dept in /shared/tests/vars.php on line 40
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:40
  
  #tests#php -q vars.php
  ---------------- Test Results ----------------&gt;
  ----- var    ==apl:dept==
  ----- xr name==apl:dept==
  
  ------ assertion 01 ------&gt;
  PHP Parse error:  syntax error, unexpected ':' in /shared/tests/vars.php(43) : assert code on line 1
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:43
  PHP Catchable fatal error:  assert(): Failure evaluating code:
  apl:dept in /shared/tests/vars.php on line 43
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:43
  
  #tests#php -q vars.php
  ---------------- Test Results ----------------&gt;
  ----- var    ==apl:dept==
  ----- xr name==apl:dept==
  
  ------ assertion 02 ------&gt;
  ------ assertion 03 ------&gt;
  ------ assertion 04 ------&gt;
  PHP Parse error:  syntax error, unexpected ':' in /shared/tests/vars.php(52) : assert code on line 1
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:52
  PHP Catchable fatal error:  assert(): Failure evaluating code:
  apl:dept!=='' in /shared/tests/vars.php on line 52
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:52
  
  #tests#php -q vars.php
  ---------------- Test Results ----------------&gt;
  ----- var    ==apl:dept==
  ----- xr name==apl:dept==
  
  ------ assertion 02 ------&gt;
  ------ assertion 03 ------&gt;
  ------ assertion 05 ------&gt;
  PHP Parse error:  syntax error, unexpected ':' in /shared/tests/vars.php(55) : assert code on line 1
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:55
  PHP Catchable fatal error:  assert(): Failure evaluating code:
  apl:dept!=='' in /shared/tests/vars.php on line 55
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:55
  
  #tests#php -q vars.php
  ---------------- Test Results ----------------&gt;
  ----- var    ==apl:dept==
  ----- xr name==apl:dept==
  
  ------ assertion 02 ------&gt;
  ------ assertion 03 ------&gt;
  ------ assertion 06 ------&gt;
  PHP Warning:  assert(): Assertion failed in /shared/tests/vars.php on line 58
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:58
  ------ assertion 07 ------&gt;
  ------ assertion 08 ------&gt;
  PHP Warning:  assert(): Assertion "!empty($xr-&gt;name)" failed in /shared/tests/vars.php on line 64
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:64
  ------ assertion 09 ------&gt;
  ------ assertion 10 ------&gt;
  PHP Parse error:  syntax error, unexpected ':', expecting T_PAAMAYIM_NEKUDOTAYIM in /shared/tests/vars.php(70) : assert code on line 1
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:70
  PHP Catchable fatal error:  assert(): Failure evaluating code:
  !empty(apl:dept) in /shared/tests/vars.php on line 70
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:70
  
  #tests#php -q vars.php
  ---------------- Test Results ----------------&gt;
  ----- var    ==apl:dept==
  ----- xr name==apl:dept==
  
  ------ assertion 02 ------&gt;
  ------ assertion 03 ------&gt;
  ------ assertion 06 ------&gt;
  PHP Warning:  assert(): Assertion failed in /shared/tests/vars.php on line 58
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:58
  ------ assertion 07 ------&gt;
  ------ assertion 08 ------&gt;
  PHP Warning:  assert(): Assertion "!empty($xr-&gt;name)" failed in /shared/tests/vars.php on line 64
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:64
  ------ assertion 09 ------&gt;
  ------ assertion 11 ------&gt;
  PHP Parse error:  syntax error, unexpected ':', expecting T_PAAMAYIM_NEKUDOTAYIM in /shared/tests/vars.php(73) : assert code on line 1
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:73
  PHP Catchable fatal error:  assert(): Failure evaluating code:
  !empty(apl:dept) in /shared/tests/vars.php on line 73
  PHP Stack trace:
  PHP   1. {main}() /shared/tests/vars.php:0
  PHP   2. assert() /shared/tests/vars.php:73
  </code></pre>
  
  <p>Now some comments:</p>
  
  <p>Cases 1,2 are understandable - ie the string evaluation "feature".</p>
  
  <p>All cases with the assertion expression surrounded by double quotes - figured them out too: variable parsing is done first which results in the syntax error in the execution. So to make case 4 for example work, we can do:</p>
  
  <pre><code>echo "------ assertion 04 ------&gt;\n";
  assert ("'{$xr-&gt;name}'!==''");   
  </code></pre>
  
  <p>Now the real issue is with cases 6,7. Why is 6 failing while 7 is succeeding. The same with cases 8,9 - 8 failing while 9 succeeding.</p>
  
  <p>Regards.</p>