update page now
Laravel Live Japan

json_last_error_msg

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

json_last_error_msg直近の json_validate()、json_encode()、json_decode() の呼び出しのエラー文字列を返す

説明

json_last_error_msg(): string

呼び出しで JSON_THROW_ON_ERROR を指定しなかった場合、 直近の json_validate()json_encode()、 または json_decode() 呼び出しの エラー文字列を返します。

パラメータ

この関数にはパラメータはありません。

戻り値

成功した場合にエラーメッセージ、エラーが発生しなかった場合は "No error"

参考

add a note

User Contributed Notes 1 note

up
23
Anonymous
10 years ago
Here's an updated version of the function:

<?php
    if (!function_exists('json_last_error_msg')) {
        function json_last_error_msg() {
            static $ERRORS = array(
                JSON_ERROR_NONE => 'No error',
                JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
                JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
                JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
                JSON_ERROR_SYNTAX => 'Syntax error',
                JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
            );

            $error = json_last_error();
            return isset($ERRORS[$error]) ? $ERRORS[$error] : 'Unknown error';
        }
    }
?>
To Top