Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

json_last_error_msg

Returns the error description of the last `json_decode()` or `json_encode()` call.

Syntax

json_last_error_msg(): string

Return 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 error

Notes

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

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.

Thanks for reading!

Donate to the site — we work hard for you.