I am writing a script to handle jsonp response. So that I can interpret it, send it to client, and use my own callback in ajax. Anyway, the response from server is json if it is successful. If the request fails, it returns an xml.
I checked some answers before.
Check whether returned file is XML or not
How check if a String is a Valid XML with-out Displaying a Warning in PHP
This answer works fine, my bad.
I write this.
$xml = simplexml_load_string($result,'SimpleXmlElement', LIBXML_NOERROR+LIBXML_ERR_FATAL+LIBXML_ERR_NONE);
if(!$xml){// json}
This gives out warning,
node no longer exist
The warning enters the json response, making it hard to parse.
I know if I suppress the warnings in php, it would work fine, but this is not the answer I want. I want to know, how do I check the string format, without giving out warning. Even in developing environment. I think it's better to solve problem from the root.
Then I used this,
$xml = simplexml_load_string($result,'SimpleXmlElement', LIBXML_NOERROR+LIBXML_ERR_FATAL+LIBXML_ERR_NONE);
if($xml == false){ // json }
This enters the block no matter if the string is xml or json. $xml == false
is always evaluated as true.
I think if you can explain to me, the difference between $xml == false
and !$xml
that would be great. Thanks in advance.