json_encode()

json_encode() encodes a data structure into a JSON string.

In case an error arise during parsing, a null value is returned, as the default value. This error may be confused with the decoding of the 'null' string, which is a valid JSON value. It may also raise a ValueError, when the function is configured by JSON_THROW_ON_ERROR.

<?php

$data = [1,2,3];
try {
    $json = json_encode($data);
} catch(ValueError $e) {
    print "JSON encoding error";
}
// $json === '[1,2,3]';

?>

Documentation

See also Mastering PHP json_encode: A Complete Guide.

Related : JavaScript Object Notation (JSON), JSON Lines (JSONC), json_decode(), get_object_vars()