json_last_error_msg

Retrieves the error string of the last json_encode() or json_decode() call.

Description

(bool|string) json_last_error_msg(); 

Returns (bool|string)

Returns the error message on success, "No Error" if no error has occurred, or false on failure.


Usage

  1. if ( !function_exists( 'json_last_error_msg' ) ) { 
  2. require_once ABSPATH . WPINC . '/compat.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = json_last_error_msg(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/compat.php  
  1. function json_last_error_msg() { 
  2. // See https://core.trac.wordpress.org/ticket/27799. 
  3. if ( ! function_exists( 'json_last_error' ) ) { 
  4. return false; 
  5.  
  6. $last_error_code = json_last_error(); 
  7.  
  8. // Just in case JSON_ERROR_NONE is not defined. 
  9. $error_code_none = defined( 'JSON_ERROR_NONE' ) ? JSON_ERROR_NONE : 0; 
  10.  
  11. switch ( true ) { 
  12. case $last_error_code === $error_code_none: 
  13. return 'Noerror; 
  14.  
  15. case defined( 'JSON_ERROR_DEPTH' ) && JSON_ERROR_DEPTH === $last_error_code: 
  16. return 'Maximum stack depth exceeded'; 
  17.  
  18. case defined( 'JSON_ERROR_STATE_MISMATCH' ) && JSON_ERROR_STATE_MISMATCH === $last_error_code: 
  19. return 'State mismatch (invalid or malformed JSON)'; 
  20.  
  21. case defined( 'JSON_ERROR_CTRL_CHAR' ) && JSON_ERROR_CTRL_CHAR === $last_error_code: 
  22. return 'Control character error, possibly incorrectly encoded'; 
  23.  
  24. case defined( 'JSON_ERROR_SYNTAX' ) && JSON_ERROR_SYNTAX === $last_error_code: 
  25. return 'Syntaxerror; 
  26.  
  27. case defined( 'JSON_ERROR_UTF8' ) && JSON_ERROR_UTF8 === $last_error_code: 
  28. return 'Malformed UTF-8 characters, possibly incorrectly encoded'; 
  29.  
  30. case defined( 'JSON_ERROR_RECURSION' ) && JSON_ERROR_RECURSION === $last_error_code: 
  31. return 'Recursion detected'; 
  32.  
  33. case defined( 'JSON_ERROR_INF_OR_NAN' ) && JSON_ERROR_INF_OR_NAN === $last_error_code: 
  34. return 'Inf and NaN cannot be JSON encoded'; 
  35.  
  36. case defined( 'JSON_ERROR_UNSUPPORTED_TYPE' ) && JSON_ERROR_UNSUPPORTED_TYPE === $last_error_code: 
  37. return 'Type is not supported'; 
  38.  
  39. default: 
  40. return 'An unknownerroroccurred';