PHP json_last_error_msg() Function
The PHP json_last_error_msg() function returns the error string of the last json_encode() or json_decode() call.
Syntax
json_last_error_msg()
Parameters
No parameter is required.
Return Value
Returns the error message on success, or "No error" if no error has occurred.
Example: json_last_error_msg() example
The example below shows the usage of json_last_error_msg() function.
<?php //a valid json string $jsonObj[] = '{"a":10}'; //an invalid json string - //using ' instead of " for quotation //this will cause syntax error $jsonObj[] = "{'b':20}"; foreach ($jsonObj as $string) { echo 'Decoding: '.$string; json_decode($string); if (json_last_error() !== JSON_ERROR_NONE) { echo " - ".json_last_error_msg(); } echo PHP_EOL; } ?>
The output of the above code will be:
Decoding: {"a":10} Decoding: {'b':20} - Syntax error
❮ PHP JSON Reference