Skip to content

Instantly share code, notes, and snippets.

@blockjon
Created February 5, 2014 18:36
Show Gist options
  • Save blockjon/8830229 to your computer and use it in GitHub Desktop.
Save blockjon/8830229 to your computer and use it in GitHub Desktop.
Apparent evidence PHP json_encode not returning FALSE on error
/**
* Evidence that PHP json_encode is not returning FALSE on error.
* (Reference: http://us1.php.net/json_encode)
*
* Env:
*
* PHP 5.3.10-1ubuntu3.9 with Suhosin-Patch (cli) (built: Dec 12 2013 04:27:25)
* Copyright (c) 1997-2012 The PHP Group
* Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
* with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
*/
public function testApparentPHPJsonEncodeBug()
{
// json_encode cannot handle this input. It usually causes this error:
// "json_encode(): Invalid UTF-8 sequence in argument" to appear in
// error logs. I will instead silence the error in order to observe
// the result value.
$result = @json_encode(pack("H*" ,'c32e'));
// Unfortunately I am getting a string back.
$this->assertTrue(is_string($result));
// The contents of the string are "null"
$this->assertEquals("null", $result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment