PHP: json_last_error_msg - Manual (original) (raw)
(PHP 5 >= 5.5.0, PHP 7, PHP 8)
json_last_error_msg — Returns the error string of the last json_validate(), json_encode() or json_decode() call
Description
function json_last_error_msg(): string
Parameters
This function has no parameters.
Return Values
Returns the error message on success, or "No error" if no error has occurred.
Found A Problem?
11 years ago
Here's an updated version of the function:
<?php
if (!function_exists('json_last_error_msg')) {
function json_last_error_msg() {
static $ERRORS = array(
JSON_ERROR_NONE => 'No error',
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
);
$error = json_last_error();
return isset($ERRORS[$error]) ? <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>E</mi><mi>R</mi><mi>R</mi><mi>O</mi><mi>R</mi><mi>S</mi><mo stretchy="false">[</mo></mrow><annotation encoding="application/x-tex">ERRORS[</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.05764em;">ERRORS</span><span class="mopen">[</span></span></span></span>error] : 'Unknown error';
}
}
?>