¿Por qué un objeto SimpleXML se evalúa como booleano TRUE?

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) devuelve falso

pero $xml->childen($namespace) no.

Please advise TIA Ephraim

preguntado el 08 de noviembre de 11 a las 12:11

Both snippets print true for me... what's your PHP version? -

Which version of php do you use? With my php5.3.5/win32 it prints true. -

The second one throws the following: Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /t.php:2 aquí: codepad.org/KIbscPGh -

@BrendanBullen - There was never an empty string. It remained invisible because code wasn't formatted as code. -

3 Respuestas

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:

Al convertir a booleano, se consideran los siguientes valores FALSE:
[...] the special type NULL (including unset variables)
[...] SimpleXML objects created from empty tags

Cualquier otro valor se considera VERDADERO (incluido cualquier recurso).

En segundo lugar, la 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);

respondido 08 nov., 11:16

You can find everything that evaluates to true/false on http://www.php.net/manual/en/language.types.boolean.php (find "When converting to boolean, the following values are considered FALSE:")

Creating a SimpleXMLElement object will throw an exception if it fails. Following code should do what you need:

try
{
    $xml=new SimpleXMLElement('');
    var_dump($xml);
    echo 'true';
}
catch
{
    echo 'false';
}

To evaluate to true/false, you might be looking into ===, to explicitly evaluate to false.

$test = '';
echo $test ? 'true' : 'false'; // will output false
echo $test !== false ? 'true' : 'false'; // will output true

respondido 08 nov., 11:16

Objects in PHP (5+) evaluate to TRUE, Siempre hay .

El uno exception are SimpleXML objects created from empty tags - see http://php.net/manual/en/language.types.boolean.php

$xml = new SimpleXMLElement('<b></b>');

qualifies as such a FALSE SimpleXML objetoFALSE === (bool) $xml;).

Sin embargo

$xml = new SimpleXMLElement('');

Throws an exception and will halt your program if not caught. If caught, $xml no está configurado.

respondido 08 nov., 11:16

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.