🎯 By the end of this lesson you can…
- Create variables with $
- Recognise PHP’s types with var_dump and gettype
- Convert types with casts
🐘 PHP
variables.php
👆 Tap a line to explain it
1<?php2$name = "Asha";3$age = 14;4$height = 1.52;5$isStudent = true;6$nickname = null;7var_dump($name, $age, $height, $isStudent, $nickname);8$age = $age + 1;9echo "Next year $name will be $age\n";
Converting types
🐘 PHP
casting.php
👆 Tap a line to explain it
1<?php2echo (int) "42 apples", "\n";3echo (float) "3.5", "\n";4echo (string) 99 . "!", "\n";5var_dump((bool) 0, (bool) "0", (bool) "hi");6echo intdiv(7, 2), " ", 7 / 2, "\n";
🔮 What will this display?
What does this print?
1<?php2$a = "5";3$b = 3;4echo $a + $b, " ", $a . $b;
🛠️ Swap
Swap $a and $b and print them separated by a space. (PHP can swap in one line: [$a, $b] = [$b, $a];.)
📌 Key takeaways
$name = "Asha";— variables start with$and are created on first assignment.var_dump($x)shows type and value — great for debugging.- Cast with
(int),(float),(string),(bool).
Finished reading & practising?
Saved in this browser. Create a free account to keep it forever.