1

If I run the following, no error is returned and the value of $result is INF. json_last_error() returns 0.

$result = json_decode('547533e683433', true);

As this is a string and not JSON I would expect $result to be NULL and json_last_error() to return 4 or JSON_ERROR_SYNTAX.

Why is this happening?

11
  • Well, what do you expect it to return? Commented Jun 11, 2015 at 21:09
  • 547533e683433 is a huge number. 547533 x10^683433 Commented Jun 11, 2015 at 21:11
  • I would expect json_last_error() to return an error code, in this case it should be 4 (JSON_ERROR_SYNTAX) and the $result to be NULL. I'll update the question. Commented Jun 11, 2015 at 21:11
  • 2
    @greg: According to the docs, [PHP] will also encode and decode scalar types and NULL. So it's trying to decode '547533e683433' as an int, but it's so big that it gives you infinity. Commented Jun 11, 2015 at 21:13
  • 1
    Why the down votes? It seems like a legitimate question to me? @RocketHazmat your answers have been great, add as an answer and I'll accept. Commented Jun 11, 2015 at 21:22

1 Answer 1

3

This is the expected result. While '547533e683433' is not valid JSON, PHP can still "decode" it.

PHP implements a superset of JSON as specified in the original RFC 4627 - it will also encode and decode scalar types and NULL.

According to the docs for json_decode, PHP will decode single scalar values, not just arrays/objects.

In the case of '547533e683433', it's interpreted as an int when decoding (strings need to be in double quotes). 547533e683433 is read as 547533 x 10^683433 (see the docs for is_numeric), which is a huge number. PHP can't represent a number this big, so it gives you INF.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.