A common use for json_last_error is processing data without rebuilding the operation manually. Returns the error code for the last JSON operation that did not use JSON_THROW_ON_ERROR.
What Is json_last_error in PHP?
Returns the error code for the last JSON operation that did not use JSON_THROW_ON_ERROR.
Syntax:
json_last_error() : int
Parameters and Return Value
This operation takes no arguments.
Declared return type: int. Distinguish false, an empty result, and exceptions where the API permits them.
Example 1: Basic Usage
This example demonstrates the main operation with a small, controlled input.
<?php
json_decode("{bad}");
echo json_last_error();
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Example 2: Another Input
This example changes the input or surrounding setup to illustrate another behavior.
<?php
json_decode("[]");
var_dump(json_last_error() === JSON_ERROR_NONE);
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Practical Example
This application-style example shows a self-contained usage pattern and checks its result.
<?php
$decoded = json_decode("[1,2,3]", true);
if (json_last_error() !== JSON_ERROR_NONE) { throw new RuntimeException(json_last_error_msg()); }
print_r($decoded);
The example focuses on the value produced or the state changed by this API. Review the return value and validate input before using this pattern in production.
Important Behavior and Edge Cases
- JSON_THROW_ON_ERROR uses exceptions instead of relying on the global last-error state.
- Use the intended input type and handle empty or malformed values explicitly.
- Where this API requires an extension, version, or file, document that dependency before using it in a real application.
Comparison With Related Functions
| Function or Method | When to Use It |
|---|---|
json_last_error | Returns the error code for the last JSON operation that did not use JSON_THROW_ON_ERROR. |
json_last_error_msg | Returns a readable message for the previous JSON encoding or decoding error. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using this API without checking the required input or extension | JSON_THROW_ON_ERROR uses exceptions instead of relying on the global last-error state. |
| Treating valid JSON as valid business data | Check required keys, types and allowed values after decoding. |
| Ignoring UTF-8 or encoding failures | Use JSON_THROW_ON_ERROR and handle JsonException explicitly. |
Key Takeaways
- Returns the error code for the last JSON operation that did not use JSON_THROW_ON_ERROR.
- JSON_THROW_ON_ERROR uses exceptions instead of relying on the global last-error state.
- Test both the expected case and at least one failure or empty-input case before deployment.
Returns the error code for the last JSON operation that did not use JSON_THROW_ON_ERROR. Use it when the operation fits your input, output and validation requirements.