json_encode() Function in PHP

Q

Where to get the detailed description of the json_encode() Function in PHP?

✍: FYIcenter.com

A

Here is the detailed description of the json_encode() Function in PHP.

Description - The json_encode() function generates a JSON text string that represents the given PHP value.

Syntax -

string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

Parameters -

  • $value - Required. The value being encoded. Can be any type except a resource.
  • $options - Optional. Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
  • $depth - Optional. Set the maximum depth. Must be greater than zero.

Return value - A JSON text string on success or FALSE on failure.

Examples -

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);

# The above example will output:
# 
# {"a":1,"b":2,"c":3,"d":4,"e":5}
?>

 

json_encode() - PHP to JSON Data Type Mapping

json_last_error_msg() - PHP JSON Error Message

Using JSON in PHP

⇑⇑ JSON Tutorials

2022-06-29, 2916🔥, 3💬