PHP 5 and 7 - json_last_error difference

Viewed 760

I'm trying to upgrade one of my PHP apps to PHP7. Everything is working great, except one. I see that json_last_error() returns a different value in PHP7.

$input = file_get_contents('php://input');
$json = json_decode($input, true);

print_r(json_last_error());

When I make

curl 'http://localhost/test.php'  -H 'Content-Type: application/json' --compressed

PHP 5 returns 0 (JSON_ERROR_NONE)

PHP 7 returns 4 (JSON_ERROR_SYNTAX)

I've looked at the official documentation if there was change, but I could not find any info.

Was there a change for either json_decode() or json_last_error() function?

1 Answers

As per the changelog of json_decode(), any "falsy" string-value (meaning empty strings, null and false) will result in a JSON syntax error. So yes, there was a change in json_decode() between PHP 5 and PHP 7. json_last_error() has however not changed.

Changelog in 7.0.0 of json_encode():

An empty PHP string or value that after casting to string is an empty string (NULL, FALSE) results in JSON syntax error.

Related