Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6191861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:50:28+00:00 2026-05-24T02:50:28+00:00

I was trying to do a simple PHP assertion to verify that a string

  • 0

I was trying to do a simple PHP assertion to verify that a string is not empty, but got confusing results.

I wasn’t aware of the string evaluation feature in assert(), but checked the docs and understood that part.

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.

The code is about locating an XML element using XMLReader, but that just happened to be my code. So here goes the code first:

<?php

$xml = <<< XML
<depts  xmlns:apl="urn:my:ns" >
    <apl:dept>Dept One</apl:dept>
    <apl:dept>Dept Tow</apl:dept>
</depts>
XML;


$elmLocal = 'dept';
$elmUrn = "urn:my:ns";

$xr = new XMLReader;
$xr->XML($xml);

// move to the first desired element node
$found = false;
while ($xr->read()) {
    if ($xr->localName === $elmLocal && $xr->namespaceURI === $elmUrn) {
        $found = true;
        break;
    }
}
if (!$found) {
    exit("---Error--- No element found with given Name/NS.\n");
}



$var = $xr->name;

echo "---------------- Test Results ---------------->\n";

echo "----- var    =={$var}==\n";
echo "----- xr name=={$xr->name}==\n\n";


echo "------ assertion 00 ------>\n";
assert ($xr->name);

echo "------ assertion 01 ------>\n";
assert ($var);

echo "------ assertion 02 ------>\n";
assert ('$xr->name!==""');   

echo "------ assertion 03 ------>\n";
assert ('$var!==""');   

echo "------ assertion 04 ------>\n";
assert ("{$xr->name}!==''");   

echo "------ assertion 05 ------>\n";
assert ("{$var}!==''");   

echo "------ assertion 06 ------>\n";
assert(!empty($xr->name));

echo "------ assertion 07 ------>\n";
assert(!empty($var));

echo "------ assertion 08 ------>\n";
assert('!empty($xr->name)');

echo "------ assertion 09 ------>\n";
assert('!empty($var)');

echo "------ assertion 10 ------>\n";
assert("!empty({$xr->name})");

echo "------ assertion 11 ------>\n";
assert("!empty({$var})");



// do some processing
$domNode = $xr->expand(); // DOMNode XMLReader::expand ([ DOMNode $basenode ] )


?>

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:

#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 ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 00 ------>
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 ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 01 ------>
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 ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 02 ------>
------ assertion 03 ------>
------ assertion 04 ------>
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 ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 02 ------>
------ assertion 03 ------>
------ assertion 05 ------>
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 ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 02 ------>
------ assertion 03 ------>
------ assertion 06 ------>
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 ------>
------ assertion 08 ------>
PHP Warning:  assert(): Assertion "!empty($xr->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 ------>
------ assertion 10 ------>
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 ---------------->
----- var    ==apl:dept==
----- xr name==apl:dept==

------ assertion 02 ------>
------ assertion 03 ------>
------ assertion 06 ------>
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 ------>
------ assertion 08 ------>
PHP Warning:  assert(): Assertion "!empty($xr->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 ------>
------ assertion 11 ------>
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

Now some comments:

Cases 1,2 are understandable – ie the string evaluation “feature”.

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:

echo "------ assertion 04 ------>\n";
assert ("'{$xr->name}'!==''");   

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.

Regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:50:29+00:00Added an answer on May 24, 2026 at 2:50 am

    It’s not about how assert() works but how empty() works.
    When used with $object->inaccessibleProp empty first tries the same thing as isset() and if this “test” returns false empty() doesn’t even try to access the element but returns true.
    And the XMLReader class obviously doesn’t implement the __isset() method the way you’d need it.

    <?php
    $xml = <<< XML
    <depts  xmlns:apl="urn:my:ns" >
        <apl:dept>Dept One</apl:dept>
        <apl:dept>Dept Tow</apl:dept>
    </depts>
    XML;
    
    $elmLocal = 'dept';
    $elmUrn = "urn:my:ns";
    
    $xr = new XMLReader;
    $xr->XML($xml);
    
    // move to the first desired element node
    $found = false;
    while ($xr->read()) {
        if ($xr->localName === $elmLocal && $xr->namespaceURI === $elmUrn) {
            $found = true;
            break;
        }
    }
    if (!$found) {
        exit("---Error--- No element found with given Name/NS.\n");
    }
    
    $var = $xr->name;
    var_dump(isset($xr->name));
    var_dump(empty($xr->name));
    echo $xr->name;
    

    prints

    bool(false)
    bool(true)
    apl:dept
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple PHP function that can take a string like
I am trying something very simple, but for some reason it does not work.
I'm trying a simple multiple conditional statement which should work fine with PHP but
I'm trying to build an application in simple PHP but could be done in
I'm trying to write a simple php function that will strip everything between two
I am trying to create a simple php if statement that goes something like
I'm trying to create a very simple PHP CLI application that can be run
I got this error when I am trying to run a simple PhP script
I have a simple PHP class, I'm trying to access a property of that
I'm trying to just write a simple PHP if statement that checks if a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.