json_last_error
Description
The json_last_error of JSON for PHP returns the last error occurred.
Syntax
json_last_error(): int
Return
Returns an integer, the value can be one of the following constants:
Value | Constant | Description |
---|---|---|
0 | JSON_ERROR_NONE | No error |
1 | JSON_ERROR_DEPTH | Maximum stack depth exceeded |
2 | JSON_ERROR_STATE_MISMATCH | State mismatch (invalid or malformed JSON) |
3 | JSON_ERROR_CTRL_CHAR | Control character error, possibly incorrectly encoded |
4 | JSON_ERROR_SYNTAX | Syntax error |
5 | JSON_ERROR_UTF8 | Malformed UTF-8 characters, possibly incorrectly encoded |
6 | JSON_ERROR_RECURSION | Recursion detected |
7 | JSON_ERROR_INF_OR_NAN | Inf and NaN cannot be JSON encoded |
8 | JSON_ERROR_UNSUPPORTED_TYPE | Type is not supported |
9 | JSON_ERROR_INVALID_PROPERTY_NAME | The decoded property name is invalid |
10 | JSON_ERROR_UTF16 | Single unpaired UTF-16 surrogate in unicode escape |
Examples
1 · JSON_ERROR_NONE
<? $return = json_last_error(); echo $return;
0
2 · JSON_ERROR_DEPTH
<? $value = [ 1, 2, [ 1, 2, [ 1, 2 ] ] ]; $flags = 0; $depth = 2; json_encode($value, $flags, $depth); $return = json_last_error(); echo $return;
1
3 · JSON_ERROR_STATE_MISMATCH
<? $json = '{"a":1]}'; json_decode($json); $return = json_last_error(); echo $return;
2
4 · JSON_ERROR_CTRL_CHAR
<? $json = "\""; json_decode($json); $return = json_last_error(); echo $return;
3
5 · JSON_ERROR_SYNTAX
<? $json = "{'a':1}"; json_decode($json); $return = json_last_error(); echo $return;
4
6 · JSON_ERROR_UTF8
<? $value = "\xaa"; json_encode($value); $return = json_last_error(); echo $return;
5
7 · JSON_ERROR_RECURSION
<? $value[0] = &$value; json_encode($value); $return = json_last_error(); echo $return;
6
8 · JSON_ERROR_INF_OR_NAN
<? $value = [ INF, NAN ]; json_encode($value); $return = json_last_error(); echo $return;
7
9 · JSON_ERROR_UNSUPPORTED_TYPE
<? $filename = $_SERVER["DOCUMENT_ROOT"] . "/assets/txt/1.txt"; $mode = "r"; $value = fopen($filename, $mode); fclose($value); json_encode($value); $return = json_last_error(); echo $return;
8
10 · JSON_ERROR_INVALID_PROPERTY_NAME
<? $json = '{"\u0000":1}'; json_decode($json); $return = json_last_error(); echo $return;
9
11 · JSON_ERROR_UTF16
<? $json = '{"\udaaa":1}'; json_decode($json); $return = json_last_error(); echo $return;
10