json_encode
Returns the JSON representation of a value.
json_encode Function synopsis
json_encode(
mixed $value,
int $flags = 0,
int $depth = 512
): string|false
Parameters
$value
Typemixed
The $value being encoded. Can be any type except
a resource.
$flags
OptionalTypeintDefault value0
Bitmask consisting of
JSON_FORCE_OBJECT,
JSON_HEX_QUOT,
JSON_HEX_TAG,
JSON_HEX_AMP,
JSON_HEX_APOS,
JSON_INVALID_UTF8_IGNORE,
JSON_INVALID_UTF8_SUBSTITUTE,
JSON_NUMERIC_CHECK,
JSON_PARTIAL_OUTPUT_ON_ERROR,
JSON_PRESERVE_ZERO_FRACTION,
JSON_PRETTY_PRINT,
JSON_UNESCAPED_LINE_TERMINATORS,
JSON_UNESCAPED_SLASHES,
JSON_UNESCAPED_UNICODE,
JSON_THROW_ON_ERROR.
The behaviour of these constants is described on the
JSON constants page.
$flags accepts one or a bitmask of the following constant values:
JSON_HEX_QUOTJSON_HEX_TAGJSON_HEX_AMPJSON_HEX_APOSJSON_NUMERIC_CHECKJSON_PRETTY_PRINTJSON_UNESCAPED_SLASHESJSON_FORCE_OBJECTJSON_PRESERVE_ZERO_FRACTIONJSON_UNESCAPED_UNICODEJSON_PARTIAL_OUTPUT_ON_ERRORJSON_UNESCAPED_LINE_TERMINATORSJSON_THROW_ON_ERROR
$depth
OptionalTypeintDefault value512
Set the maximum depth. Must be greater than zero.
Return value
Typestring|false
Returns a JSON encoded string on success or false on failure.
Changes to the json_encode Function
PHP 8.0
- Return type added:
string|false - Parameter type added for parameter #1 (
$value):mixed - Parameter name of parameter #2 changed:
$optionsto$flags - Parameter type added for parameter #2 (
$flags):int - Parameter default value added for position #2 (
$flags):0 - Parameter type added for parameter #3 (
$depth):int - Parameter default value added for position #3 (
$depth):512
json_encode(
- $value,
+ mixed $value,
- $options,
+ int $flags = 0,
- $depth
+ int $depth = 512
- )
+ ): string|false PHP 5.5
- Optional parameter added - #3:
$depth
-json_encode($value, $options)
+json_encode(
+ $value,
+ $options,
+ $depth
+ ) json_encode Function Availability
json_encode Function Availability| PHP Version | Availability |
|---|---|
| PHP 8.6Upcoming Release | Yes |
| PHP 8.5Supported (Latest) | Yes |
| PHP 8.4Supported | Yes |
| PHP 8.3Security-Fixes Only | Yes |
| PHP 8.2Security-Fixes Only | Yes |
| PHP 8.1Unsupported | Yes |
| PHP 8.0Unsupported | Yes |
| PHP 7.4Unsupported | Yes |
| PHP 7.3Unsupported | Yes |
| PHP 7.2Unsupported | Yes |
| PHP 7.1Unsupported | Yes |
| PHP 7.0Unsupported | Yes |
| PHP 5.6Unsupported | Yes |
| PHP 5.5Unsupported | Yes |
| PHP 5.4Unsupported | Yes |
| PHP 5.3Unsupported | Yes |