json_last_error

(PHP 5 >= 5.3.0, PHP 7)

json_last_errorOluşan son hatayı döndürür.

Açıklama

json_last_error ( void ) : int

Son JSON çözümleyicisi tarafından (varsa) oluşan son hatayı döndürür.

Değiştirgeler

Bu işlevin değiştirgesi yoktur.

Dönen Değerler

Tamsayı döndürür, ve değeri aşağıdaki sabitler olabilir:

JSON hata kodları
Sabit Anlamı Geçerlilik
JSON_ERROR_NONE Hata bulunamadı  
JSON_ERROR_DEPTH Azami yığın derinliği aşıldı  
JSON_ERROR_CTRL_CHAR Denetim karakteri hatası, muhtemelen yanlış kodlanmış  
JSON_ERROR_SYNTAX Sözdizimi hatası  
JSON_ERROR_UTF8 UTF-8 karakter kodlama hatası, muhtemelen yanlış kodlanmış PHP 5.3.1

Örnekler

Örnek 1 json_last_error() örneği

<?php
// Geçerli json dizgesi
$json[] = '{"Organization": "PHP Documentation Team"}';

// Geçersiz json dizgesi sözdizimi hatasına sebep
// olur, bu durumda biz tırnak için ' yerine " 
// kullanırız 
$json[] = "{'Organization': 'PHP Documentation Team'}";


foreach(
$json as $string)
{
    echo 
'Decoding: ' $string;
    
json_decode($string);

    switch(
json_last_error())
    {
        case 
JSON_ERROR_DEPTH:
            echo 
' - Azami yığın derinliği aşıldı';
        break;
        case 
JSON_ERROR_CTRL_CHAR:
            echo 
' - Beklenmeyen kontol karakteri bulundu';
        break;
        case 
JSON_ERROR_SYNTAX:
            echo 
' - Sözdizimi hatası, kusurlu JSON';
        break;
        case 
JSON_ERROR_NONE:
            echo 
' - Hatasız';
        break;
    }

    echo 
PHP_EOL;
}
?>

Yukarıdaki örneğin çıktısı:

Decoding: {"Organization": "PHP Documentation Team"} - Hatasız
Decoding: {'Organization': 'PHP Documentation Team'} - Sözdizimi hatası, kusurlu JSON

Ayrıca Bakınız