1

I have recently learned of the function error_get_last() and json_last_error(). Going through tutorials, I rarely found information on the functions above.

Will all errors caused by json_encode() or json_decode() not be in error_get_last() already?

Is there a better way to handle errors (and memory leaks) during runtime (just before output)?

I use a buffer to hold all output, use the functions above to check for errors, and output the data if no errors are found.

1
  • preg_last_error() Commented Feb 16, 2019 at 15:00

1 Answer 1

1

error_get_last returns PHP runtime type of errors, for example division by zero is such an error.

The errors returned by the functions json_decode and json_encode are not runtime errors.

Consider the following example to get better understanding:

json_decode('Hello world');
echo error_get_last() . PHP_EOL; // No error here
echo json_last_error() . PHP_EOL;  // Error #4 - JSON_ERROR_SYNTAX
Sign up to request clarification or add additional context in comments.

2 Comments

Are there more error functions like the two above? (Not considering mysql_error() in favor of PDO)
On looking through sites with php error function definitions, not one mentions json_last_error(). I am just wondering if i missed another function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.