LESSON 04 / 05

Operators & Maths

Arithmetic, remainders, powers, comparisons (=== vs ==), logical operators and the handy ?? and <=> operators.

๐ŸŽ“Beginnerlevel
โฑ๏ธ25 minto finish
โœ๏ธ1activities

๐Ÿ“‚ PHP Basics๐Ÿท๏ธ PHP basics

๐ŸŽฏ By the end of this lesson you canโ€ฆ

  • Use arithmetic and assignment operators
  • Compare strictly with = and !
  • Use ??, ?: and <=>
๐Ÿ˜ PHP operators.php ๐Ÿ‘† Tap a line to explain it
1<?php2echo 17 % 5, " ", 2 ** 8, " ", intdiv(17, 5), "\n";3$score = 10;4$score += 5;5$score++;6echo $score, "\n";7var_dump(5 == "5", 5 === "5");8$user = null;9echo $user ?? "Guest", "\n";10echo 3 <=> 7, " ", 7 <=> 7, " ", 9 <=> 7, "\n";11var_dump($score > 10 && $score < 20);
๐Ÿ”ฎ What will this display?
What does this print?
1<?php2$a = null;3$b = 0;4echo ($a ?? "A") . ($b ?? "B");

๐Ÿ“Œ Key takeaways

  • % remainder, ** power, intdiv() whole division.
  • Use === (value AND type) instead of ==.
  • $x ?? "default" uses a default when $x is missing or null.

Finished reading & practising?

Saved in this browser. Create a free account to keep it forever.