JSON and Data Shapes
Encode Object
json_encode turns a PHP data shape into a JSON string.
Encode Object
encode_object.php
<?php
$name = ;
$json = json_encode(["name" => $name, "role" => "admin"]);
$length = strlen($json);
echo "name=" . $name . "\n";
echo "json=" . $json . "\n";
echo "length=" . $length . "\n";
<?php
$name = ;
$json = json_encode(["name" => $name, "role" => "admin"]);
$length = strlen($json);
echo "name=" . $name . "\n";
echo "json=" . $json . "\n";
echo "length=" . $length . "\n";
<?php
$name = ;
$json = json_encode(["name" => $name, "role" => "admin"]);
$length = strlen($json);
echo "name=" . $name . "\n";
echo "json=" . $json . "\n";
echo "length=" . $length . "\n";
encoded shape
Build the data shape at the call site when the lesson only needs the JSON string.