Created
April 11, 2017 19:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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