PHP, a recursive acronym for PHP: Hypertext Preprocessor, is a very popular programming language that is the core of up to 80 percent of the Internet and has its own JSON functions. The question for developers is how to take advantage of what PHP offers for JSON and how best to use it with MySQL. PHP’s JSON functions require UTF8MB4-encoded strings.
The JSON_DECODE
function is used to convert a JSON string into a PHP variable. The MySQL JSON functions have no analog.
The first argument is the JSON-formatted string to be decoded. The second is a Boolean value (true or false) to set the returned data into an associative array. The third argument is a recursive depth limit set to 1 or greater. The fourth and final argument has two settable options: JSON_OBJECT_AS_ARRAY
, which has the same effect as setting $assoc
to true
, and JSON_BIGINT_AS_STRING
, which casts big integers to strings instead of the default floats.
Example 9-1 Simple PHP program to explore the PHP function JSON_DECODE
Example 9-2 Output of sample PHP program from Example 9-1
The JSON_ENCODE
function turns values of variables into JSON strings:
3.14.131.212