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

  • SEARCH
  • Home
  • 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 6775613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:55:38+00:00 2026-05-26T15:55:38+00:00

I am a newcomer to PHP and SimpleXMl. I would not expect false under

  • 0

I am a newcomer to PHP and SimpleXMl.

I would not expect false under either of these tests.

  $xml=new SimpleXMLElement('<a><b>123</b></a>');
  var_dump($xml);
  echo $xml ? "true": "false";

or

  $xml=new SimpleXMLElement('<a><b></b></a>');
  var_dump($xml);
  echo $xml ? "true": "false";

however the second returns false even though an XMLSimpleElement object is returned.
I have the same issue with an xml doc with namespaces everywhere.

it means I cannot test for a failed XML parsing as
if (!xml) returns false

but $xml->childen($namespace) does not.

Please advise
TIA
Ephraim

  • 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-26T15:55:38+00:00Added an answer on May 26, 2026 at 3:55 pm

    I can’t reproduce your issue but I’ll try to provide a couple of hints.

    First, here’s the rule when converting objects to booleans:

    When converting to boolean, the following values are considered FALSE:
    […] the special type NULL (including unset variables)
    […] SimpleXML objects created from empty
    tags

    Every other value is considered TRUE (including any resource).

    Second, the SimpleXMLElement constructor always returns an object but it can throw a warning and an exception.

    So these are the possibilities:

    // Casts as TRUE because it's an object
    $xml=new SimpleXMLElement('<a><b>123</b></a>');
    var_dump($xml, (bool)$xml);
    unset($xml);
    
    // Casts as FALSE because it's an SimpleXMLElement object for an empty tag
    $xml=new SimpleXMLElement('<a />');
    var_dump($xml, (bool)$xml);
    unset($xml);
    
    // Casts as FALSE because the $xml variable was never set sucessfully so it's not even set
    try{
        $xml=new SimpleXMLElement('');
    }catch(Exception $e){
    }
    var_dump($xml, (bool)$xml); // Notice: Undefined variable: xml
    unset($xml);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to XML and AJAX and am only a newcomer to Javascript
I am a newcomer to ASP, and would like to experiment with it by
I'm a newcomer to the idea of aspect-oriented programming but I would like to
I'm a new comer in web development, so I would like to clarify one
I'm a Go newcomer, but I have read that Go regulars do not miss
For a newcomer to .NET Web Development and programming in general, who chooses C#
As a newcomer to iPhone development and Objective-C in general over the last few
I am a newcomer to TDD. What are some of the techniques you guys
I'm a newcomer to PDO and have to say that I like it so
I'm a newcomer trying to get my feet wet with Ruby and Sinatra. I

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.