Summary - Data types
Combine string, int, float, bool and null types to build an inventory record.
Summary: five core types
In this group you learned five scalar types:
string— text, in quotes.integer— whole number, no decimal.float— fractional number, with a dot.boolean— true or false.null— no value.
Use gettype() to inspect a type and var_dump() for detailed output.
Now you will combine these types into a single inventory record.
Exercise Print the inventory record
Define five variables for an inventory record: $productName = "Pen", $quantity = 50, $price = 12.5, $inStock = true, $note = null. Print five lines: the product name, quantity, price, active status (shows 1) and the note inside [note] brackets.
Show example solution
$productName, $quantity, $price, $inStock and "[" . $note . "]"; add "\n" to each line.
Example solution:
<?php
$productName = "Pen";
$quantity = 50;
$price = 12.5;
$inStock = true;
$note = null;
echo $productName . "\n";
echo $quantity . "\n";
echo $price . "\n";
echo $inStock . "\n";
echo "[" . $note . "]\n";
Test yourself
-
Which one creates a bool variable?
- $ok = true;
- $ok = "true";
- $ok = 1;
- $ok = 1.0;
Show answer
$ok = true; — Correct! The unquoted true keyword is a bool.
-
Define a float price for the inventory record
$price = ____;Show answer
$price = 12.5; -
Match each type to an example value
- string
- "Hello"
- integer
- 42
- float
- 3.14
- boolean
- true
- NULL
- null