Content · Web Development · PHP

PHP json_last_error_msg() Function: Usage and Examples

Returns a readable message for the previous JSON encoding or decoding error. Learn how to apply it with examples and common mistakes.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

A common use for json_last_error_msg is processing data without rebuilding the operation manually. Returns a readable message for the previous JSON encoding or decoding error.

What Is json_last_error_msg in PHP?


Returns a readable message for the previous JSON encoding or decoding error.

Syntax:

json_last_error_msg() : string

Parameters and Return Value


This operation takes no arguments.

Declared return type: string. 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("{invalid}");
echo json_last_error_msg();

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("[]");
echo json_last_error_msg();

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

json_decode("{\"test\":}");
if (json_last_error() !== JSON_ERROR_NONE) { echo json_last_error_msg(); }

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


  • The last-error message describes the most recent eligible JSON operation, not arbitrary application failures.
  • 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 MethodWhen to Use It
json_last_error_msgReturns a readable message for the previous JSON encoding or decoding error.
json_last_errorReturns the error code for the last JSON operation that did not use JSON_THROW_ON_ERROR.

Common Beginner Mistakes


MistakeCorrect Approach
Using this API without checking the required input or extensionThe last-error message describes the most recent eligible JSON operation, not arbitrary application failures.
Treating valid JSON as valid business dataCheck required keys, types and allowed values after decoding.
Ignoring UTF-8 or encoding failuresUse JSON_THROW_ON_ERROR and handle JsonException explicitly.

Key Takeaways


  • Returns a readable message for the previous JSON encoding or decoding error.
  • The last-error message describes the most recent eligible JSON operation, not arbitrary application failures.
  • Test both the expected case and at least one failure or empty-input case before deployment.

Returns a readable message for the previous JSON encoding or decoding error. Use it when the operation fits your input, output and validation requirements.

Share this resourceSend this page to someone who may find it useful.