HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

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:

ValueConstantDescription
0JSON_ERROR_NONENo error
1JSON_ERROR_DEPTHMaximum stack depth exceeded
2JSON_ERROR_STATE_MISMATCHState mismatch (invalid or malformed JSON)
3JSON_ERROR_CTRL_CHARControl character error, possibly incorrectly encoded
4JSON_ERROR_SYNTAXSyntax error
5JSON_ERROR_UTF8Malformed UTF-8 characters, possibly incorrectly encoded
6JSON_ERROR_RECURSIONRecursion detected
7JSON_ERROR_INF_OR_NANInf and NaN cannot be JSON encoded
8JSON_ERROR_UNSUPPORTED_TYPEType is not supported
9JSON_ERROR_INVALID_PROPERTY_NAMEThe decoded property name is invalid
10JSON_ERROR_UTF16Single 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