JSON and Data Shapes
JSON Numbers
JSON numbers decode into numeric values that can be used in calculations.
numeric field
After decoding, calculate with the selected numeric field and print scalar results.
JSON Numbers
json_numbers.php
Replay: real traced execution (multi-file project)
<?php
$qty = 3;
$json = '{"item":"pen","qty":' . $qty . '}';
$count = json_decode($json, true)["qty"];
$total = $count * 2;
echo "qty=" . $qty . "\n";
echo "total=" . $total . "\n";
<?php
$qty = 1;
$json = '{"item":"pen","qty":' . $qty . '}';
$count = json_decode($json, true)["qty"];
$total = $count * 2;
echo "qty=" . $qty . "\n";
echo "total=" . $total . "\n";
<?php
$qty = 8;
$json = '{"item":"pen","qty":' . $qty . '}';
$count = json_decode($json, true)["qty"];
$total = $count * 2;
echo "qty=" . $qty . "\n";
echo "total=" . $total . "\n";
$qty ← 3, $json ← {"item":"pen","qty":3}, $count ← 3, $total ← 6
1<?php2$qty→ 3 = 3; //@qty=1, 83$json→ {"item":"pen","qty":3} = '{"item":"pen","qty":' . $qty3 . '}';4$count→ 3 = json_decode($json, true)["qty"]3;5$total→ 6 = $count3 * 2;67echo "qty=" . $qty3 . "\n";8echo "total=" . $total6 . "\n";outputqty=3 total=6
$qty ← 1, $json ← {"item":"pen","qty":1}, $count ← 1, $total ← 2
1<?php2$qty→ 1 = 1;3$json→ {"item":"pen","qty":1} = '{"item":"pen","qty":' . $qty1 . '}';4$count→ 1 = json_decode($json, true)["qty"]1;5$total→ 2 = $count1 * 2;67echo "qty=" . $qty1 . "\n";8echo "total=" . $total2 . "\n";outputqty=1 total=2
$qty ← 8, $json ← {"item":"pen","qty":8}, $count ← 8, $total ← 16
1<?php2$qty→ 8 = 8;3$json→ {"item":"pen","qty":8} = '{"item":"pen","qty":' . $qty8 . '}';4$count→ 8 = json_decode($json, true)["qty"]8;5$total→ 16 = $count8 * 2;67echo "qty=" . $qty8 . "\n";8echo "total=" . $total16 . "\n";outputqty=8 total=16