json_encode

FunctionParams and return types changed in PHP 8.0

Returns the JSON representation of a value.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

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:

$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: $options to $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

PHP VersionAvailability
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