Successfully parsed SimpleXMLElement comparison to 'false' returning 'true'

后端 未结 3 1625
抹茶落季
抹茶落季 2021-01-21 04:11

I got a very awkward and specific issue with a simplexml evaluation.

The code:

$simplexml = simplexml_load_string($xmlstring);
var_dump($simplexml);
var_         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 04:58

    var_dump($simplexml == false); //returns false
    

    This is expected behavior and it is explained by data comparison via "loose" data typing. In PHP, NULL, zero, and boolean FALSE are considered "Falsy" values; everything else is considered "Truthy." Inside the parentheses, PHP performs an evaluation of the expression. In this case, PHP evaluates a comparison of the named variable OBJECT and the boolean FALSE. They are not the same, so the return value from the comparison is FALSE and this is what *var_dump()* prints.

    You can use this to your advantage in the if() statement. Example:

    $simplexml = SimpleXML_Load_String('test');
    if ($simplexml) { /* process the object */ }
    else { /* process the failure to load the XML */ }
    

提交回复
热议问题