Math in PHP


$num = 20;
echo -$num // output is -20

/*
PEMDAS
1. Parentheses
2. Exponents
3. Multiplication/Division
4. Addition/Subtraction
*/

// 5 squared
echo 5**2;

//Moddulus Operator
$a % 2 == 0  // is even
$a % 2 !== 0 // is odd
echo -15 % 2;


// if you short hand increment or decrement anywhere in php script, including in echo statements, the variable will still change the values

// There are pre and post increment actions.

// post increment
$a = 5;
echo 'the vale of $a is ' . $a++ . '<br/>'; // value of a is 5
echo 'now the value is' . $a . '<br/>'; // value of a is 6

// pre increment
$a = 5;
echo 'the vale of $a is ' . ++$a . '<br/>'; // value of a is 6
echo 'now the value is' . $a . '<br/>'; // value of a is 6

$a += 5;
Previous
Next