json_last_error_msg
Returns the error description of the last `json_decode()` or `json_encode()` call.
Syntax
json_last_error_msg(): stringReturn value
stringThe error text, or `No error`.
Examples
The reason parsing failed:
<?php
$data = json_decode('{"name": "Anna",}');
var_dump($data);
echo json_last_error_msg();Output:
NULL
Syntax errorNotes
json_decode() returns null both on an error and for the string 'null'. It is simpler to pass JSON_THROW_ON_ERROR and catch JsonException.
See also
- json_decode — Parses a JSON string.
- json_validate — Checks whether a string is valid JSON.
- json_encode — Converts a value to a JSON string.
In the tutorial
- The JSON format — JSON in PHP — json_encode and json_decode, non-ASCII text, error handling and storing data in a file.