Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Created December 9, 2010 06:39
Show Gist options
  • Save yuya-takeyama/734408 to your computer and use it in GitHub Desktop.
Save yuya-takeyama/734408 to your computer and use it in GitHub Desktop.
My PHP has some bugs on json_decode()? Can't decode String or Number literals not covered with Array or Object.
<?php
$testCases = array(
'[1,2,3]',
'{"one":1,"two":2,"three":3}',
'"1"',
'1'
);
foreach ($testCases as $testCase) {
echo "INPUT:", PHP_EOL;
echo $testCase, PHP_EOL;
echo "OUTPUT:", PHP_EOL;
var_dump(json_decode($testCase));
echo PHP_EOL;
}
$ php -v
PHP 5.2.0-8+etch16 (cli) (built: Nov 24 2009 06:58:21)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies
$ php -i | grep json
json
json support => enabled
json version => 1.2.1
$ php json.php
INPUT:
[1,2,3]
OUTPUT:
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
INPUT:
{"one":1,"two":2,"three":3}
OUTPUT:
object(stdClass)#1 (3) {
["one"]=>
int(1)
["two"]=>
int(2)
["three"]=>
int(3)
}
INPUT:
"1"
OUTPUT:
NULL
INPUT:
1
OUTPUT:
NULL
$ php -v
PHP 5.3.2-1ubuntu4.5 with Suhosin-Patch (cli) (built: Sep 17 2010 13:41:55)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
$ php -i | grep json
json
json support => enabled
json version => 1.2.1
$ php json_test.php
INPUT:
[1,2,3]
OUTPUT:
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
INPUT:
{"one":1,"two":2,"three":3}
OUTPUT:
object(stdClass)#1 (3) {
["one"]=>
int(1)
["two"]=>
int(2)
["three"]=>
int(3)
}
INPUT:
"1"
OUTPUT:
string(1) "1"
INPUT:
1
OUTPUT:
int(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment