Skip to content

Instantly share code, notes, and snippets.

@diefferson
Created April 11, 2017 19:26
Show Gist options
  • Save diefferson/fb45ac3cf998af55e7c2286ad479ffa8 to your computer and use it in GitHub Desktop.
Save diefferson/fb45ac3cf998af55e7c2286ad479ffa8 to your computer and use it in GitHub Desktop.
Returns what error occurred while executing the json_decode or json_encode functions
<?php
//After executing json_encode or json_decode call this function
function jsonError(){
$errors = "";
switch (json_last_error()) {
case JSON_ERROR_NONE:
$errors .= ' - No errors';
break;
case JSON_ERROR_DEPTH:
$errors .= ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$errors .= ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$errors .= ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$errors .= ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$errors .= ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$errors .= ' - Unknown error';
break;
}
return $errors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment