Skip to content

Instantly share code, notes, and snippets.

@hnq90
Created September 19, 2014 08:25
Show Gist options
  • Save hnq90/31c53e58eaa8686e1f17 to your computer and use it in GitHub Desktop.
Save hnq90/31c53e58eaa8686e1f17 to your computer and use it in GitHub Desktop.
Detect Json ecode error
$json = json_encode($output);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment