Current Location: Home> Function Categories> json_last_error

json_last_error

Return the last error
Name:json_last_error
Category:JSON
Programming Language:php
One-line Description:Returns the last error that occurred.

Definition and usage

json_last_error() function returns the last error that occurred.

Example

Returns the last error:

 <?php
// Invalid json string
$string = "{'Bill':35,'Elon':37,'Joe':43}" ;

echo "Decoding: " . $string ;
json_decode ( $string ) ;
echo "<br>Error:" ;

switch ( json_last_error ( ) ) {
  case JSON_ERROR_NONE :
    echo "No error" ;
    break ;
  case JSON_ERROR_DEPTH :
    echo "Exceeding maximum stack depth" ;
    break ;
  case JSON_ERROR_STATE_MISMATCH :
    echo "Invalid or malformed JSON" ;
    break ;
  case JSON_ERROR_CTRL_CHAR :
    echo "Control character error" ;
    break ;
  case JSON_ERROR_SYNTAX :
    echo "grammatical error" ;
    break ;
  case JSON_ERROR_UTF8 :
    echo "Misformed UTF-8 characters" ;
    break ;
  default :
    echo "Unknown Error" ;
    break ;
}
?>

Run the instance

grammar

 json_last_error ( )

Parameter value

none.

Similar Functions
Popular Articles