Formats and protocols · Question 10 of 12

The response of an API should be processed with json_decode(), and invalid JSON should not go unnoticed. Which call fits?

1 answer is correct. Medium

Show answer

Answer

  • B json_decode($body, true, 512, JSON_THROW_ON_ERROR)

Explanation

Without JSON_THROW_ON_ERROR, json_decode() returns null for invalid JSON, which cannot be distinguished from a valid "null", and the reason would have to be requested with json_last_error(). With the flag PHP throws a JsonException with an error message. json_decode() never returns false on errors. The second parameter is a bool for associative arrays, not flags.

Certifications & exams

Which exam comes next?

You can already practise for 19 exams, and 21 are up for a vote. See all certifications with their official information, cast your vote or suggest your exam.

See all certifications