Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON decode on empty string behaves differently with PHP 7

Tags:

json

php

Result on PHP 5.6

json_decode('', true);
echo last_json_error(); // 0

Result on PHP 7.0

json_decode('', true);
echo last_json_error(); // 4

I checked the changelog of PHP 7 (https://github.com/tpunt/PHP7-Reference) and could not find the reason for that. JSON got replaced by JSOND but the BC breaks list does not mention that!?

To clarify: My question is not about valid/invalid JSON. It's about the behavior change of PHP I could not find.

like image 998
messy Avatar asked Feb 11 '16 20:02

messy


People also ask

What does JSON decode do in PHP?

The json_decode() function is used to decode or convert a JSON object to a PHP object.

Why is json_decode returning NULL?

If json_decode returns null is it because the database. json is not valid.

How can you decode JSON string?

You just have to use json_decode() function to convert JSON objects to the appropriate PHP data type. Example: By default the json_decode() function returns an object. You can optionally specify a second parameter that accepts a boolean value. When it is set as “true”, JSON objects are decoded into associative arrays.


1 Answers

It was marked as a bug and was fixed: https://bugs.php.net/bug.php?id=68938

You can find change logs here: http://php.net/ChangeLog-7.php#7.0.0 (search for json_decode)

like image 149
Александр Стуков Avatar answered Nov 15 '22 00:11

Александр Стуков