PHP theorytheory 0/50 · 0%
APIs · easy

20. JSON Handling

Encoding and decoding data.

json_encode() converts PHP arrays/objects into JSON strings, while json_decode() parses JSON text back into PHP values, returning associative arrays when the second argument is true. JSON is the standard format for REST API payloads.

Errors during encoding/decoding can be inspected with json_last_error() or, in PHP 7.3+, by passing the JSON_THROW_ON_ERROR flag to throw a JsonException.

Check your understanding

  1. 1. Which function converts a PHP array to a JSON string?

  2. 2. What does json_decode($str, true) return?