====== Mathematical Operators ======
Mathematical Operators are important in programming as alot of things are based on numbers, and of course you also work with numbers, so having a good idea of the different operators is nice.
===== Identity =====
$a = 27;
// Switches $a over to a float of 27.00
+$a
===== Negation =====
$b = 27;
// Switches $b over to a negative int of -27
-$b
===== Addition =====
$a = 10;
$b = 6;
// Results in 16
$a + $b
===== Subtraction =====
$a = 10;
$b = 6;
// Results in 4
$a - $b
===== Multiplication =====
$a = 10;
$b = 6;
// Results in 60
$a * $b
===== Division =====
$a = 12;
$c = 10;
$b = 6;
$d = 3;
// Results in an int of 2
$a / $b
// Results in a float of 3.333..
$c / $d
===== Exponentiation =====
$a = 3;
$b = 3;
// Results in 27
$a ** $b
===== Modulo =====
Modulo is very interesting, as it gives you the left over int of a division (you can also use floats with [[https://www.php.net/manual/en/function.fmod.php|fmod()]]. This can be used to for example, access every {n}th key of an array.
$a = 4;
$b = 3;
// Results in 1
$a % $b
// Can be used for arrays like so:
$evilArray = [
'evil',
'evil',
'good',
'evil',
'evil',
'good',
'evil',
'evil',
'good',
];
foreach ($evilArray as $key => $value) {
if ($key % 3 === 0) {
$goodArray[] = $value;
}
}
// Good array looks like this after
$goodArray = [
'good',
'good',
'good',
];