PHP Tutorial Tutorials - PHP Arithmetic Operators

PHP Arithmetic Operators
Operator Name Description Example Result
+$a Identity Conversion of $a to int or float as appropriate. +5 5
-$a Negation Opposite of $a. -5 -5
$a + $b Addition Sum of $a and $b. 5+3 8
$a - $b Subtraction Difference of $a and $b. 5-3 2
$a * $b Multiplication Product of $a and $b. 5*3 15
$a / $b Division Quotient of $a and $b. 5/3 1.6666667
$a % $b Modulo Remainder of $a divided by $b. 5%3 2
$a ** $b Exponentiation Result of raising $a to the $b'th power. 5**3 125
<?php
$a = 5;
$b = 3;
echo "<br>";echo +$a; // 5
echo "<br>";echo -$a; // -5
echo "<br>";echo $a + $b; // 8
echo "<br>";echo $a - $b; // 2
echo "<br>";echo $a * $b; // 15
echo "<br>";echo $a / $b; // 1.6666666666667
echo "<br>";echo $a % $b; // 2
echo "<br>";echo $a ** $b; // 125
?>

Notes:The result of the modulo operator % has the same sign as the dividend

<?php
echo "<br>";echo 5 % 3; // 2
echo "<br>";echo 5 % -3; // 2
echo "<br>";echo -5 % 3; // -2 ,the sign is same as -5
echo "<br>";echo -5 % -3; // -2 ,the sign is same as -5
?>
Date:2019-10-01 01:27:44 From:www.Lautturi.com author:Lautturi