View Single Post
Old 01-18-2008, 05:00 PM   #1 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Unhappy Json_encode is crazy

To cut a long story short, I'm wanting to check the return value of the json_encode function - therefore if its false I can throw an exception. What do I find? The lovely json_encode returns false as a string if it fails!

php Code:
$aJson = json_encode($szContent);

if(!$aJson)
{
    throw new Exception('Badly formatted JSON returned');
}

That won't work because it's a string, and I can't typecast it using (bool) because the string will be seen as a true value. Here's the result from var_dump when json_encode fails:

Quote:
string(5) "false"
Does anybody have any idea? I don't want to just do it like the following as that would be very silly indeed.

php Code:
if($aJson == 'false')
{
   
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote