json_encode
Description
The json_encode of JSON for PHP returns the JSON representation of a value.
Syntax
json_encode( mixed $value, int $flags = 0, int $depth = 512 ): string|false
Parameters
value
The value being encoded. Can be any type except a resource.
All string data must be UTF-8 encoded.
NOTE: PHP implements a superset of JSON as specified in the original RFC 7159.
flags
Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_HEX_QUOT, 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_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_THROW_ON_ERROR.
depth
Set the maximum depth. Must be greater than zero.
Return
Returns a JSON encoded string on success or false on failure.
Examples
1 · value
<? $value = [ "a" => 1, "b" => 2, "c" => 3 ]; $return = json_encode($value); echo $return;
{"a":1,"b":2,"c":3}
2 · flags · 0
<? $value = [ 1, 2, 3 ]; $flags = 0; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
[1,2,3] [1,2,3]
3 · flags · JSON_FORCE_OBJECT
<? $value = [ 1, 2, 3 ]; $flags = JSON_FORCE_OBJECT; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
[1,2,3] {"0":1,"1":2,"2":3}
4 · flags · JSON_HEX_TAG
<? $value = "<>"; $flags = JSON_HEX_TAG; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
"<>" "\u003C\u003E"
5 · flags · JSON_HEX_AMP
<? $value = "&"; $flags = JSON_HEX_AMP; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
"&" "\u0026"
6 · flags · JSON_HEX_APOS
<? $value = "'"; $flags = JSON_HEX_APOS; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
"'" "\u0027"
7 · flags · JSON_HEX_QUOT
<? $value = '"'; $flags = JSON_HEX_QUOT; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
"\"" "\u0022"
8 · flags · JSON_NUMERIC_CHECK
<? $value = [ "123", "-123", "+123", "12e3", "0.0000123" ]; $flags = JSON_NUMERIC_CHECK; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
["123","-123","+123","12e3","0.0000123"] [123,-123,123,12000,1.23e-5]
9 · flags · JSON_PARTIAL_OUTPUT_ON_ERROR
<? $value = [ "partial", "output", "\xaa" ]; $flags = JSON_PARTIAL_OUTPUT_ON_ERROR; $return1 = json_encode($value); $return2 = json_encode($value, $flags); var_export($return1); echo PHP_EOL . $return2;
false ["partial","output",null]
10 · flags · JSON_PRESERVE_ZERO_FRACTION
<? $value = 1.0; $flags = JSON_PRESERVE_ZERO_FRACTION; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
1 1.0
11 · flags · JSON_PRETTY_PRINT
<? $value = [ 1, 2, 3 ]; $flags = JSON_PRETTY_PRINT; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
[1,2,3] [ 1, 2, 3 ]
12 · flags · JSON_UNESCAPED_LINE_TERMINATORS
<? $value = "\xe2\x80\xa8\xe2\x80\xa9"; $flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
"\u2028\u2029" " "
13 · flags · JSON_UNESCAPED_SLASHES
<? $value = "/"; $flags = JSON_UNESCAPED_SLASHES; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
"\/" "/"
14 · flags · JSON_UNESCAPED_UNICODE
<? $value = "·"; $flags = JSON_UNESCAPED_UNICODE; $return1 = json_encode($value); $return2 = json_encode($value, $flags); echo $return1 . PHP_EOL . $return2;
"\u00b7" "·"
15 · flags · JSON_INVALID_UTF8_IGNORE
<? $value = "\xaa"; $flags = JSON_INVALID_UTF8_IGNORE; $return1 = json_encode($value); $return2 = json_encode($value, $flags); var_export($return1); echo PHP_EOL . $return2;
false ""
16 · flags · JSON_INVALID_UTF8_SUBSTITUTE
<? $value = "\xaa"; $flags = JSON_INVALID_UTF8_SUBSTITUTE; $return1 = json_encode($value); $return2 = json_encode($value, $flags); var_export($return1); echo PHP_EOL . $return2;
false "\ufffd"
17 · depth
<? $value = [ 1, 2, [ 1, 2, [ 1, 2 ] ] ]; $flags = 0; $depth = 3; $return = json_encode($value, $flags, $depth); echo $return;
[1,2,[1,2,[1,2]]]