728x90
SMALL
#PHP
#PHP/operators/arithmetic operators
Arithmetic operators in PHP include +, -, *, /, %, **.
- $a
Reverse the sign of $a.
<?php
$a = 10;
echo - $a;
?>
will print -10.
$a + $b
Add $a and $b.
<?php
$a = 10;
$b = 4;
echo $a + $b;
?>
will output 14.
$a - $b
Subtract $b from $a.
<?php
$a = 10;
$b = 4;
echo $a - $b;
?>
will output 6.
$a * $b
Multiply $a and $b.
<?php
$a = 10;
$b = 4;
echo $a * $b;
?>
will print 40.
$a / $b
Divide $a by $b.
<?php
$a = 10;
$b = 4;
echo $a / $b;
?>
will output 2.5.
$a % $b
The remainder of dividing $a by $b.
$a = 10;
$b = 4;
echo $a % $b;
?>
will output 2.
$a ** $b
$b squared of $a.
<?php
$a = 4;
$b = 3;
echo $a ** $b;
?>
will output 64.
operator precedence
- If there are parentheses, calculate from within the parentheses.
- *, /, % are calculated in order.
- +, - are calculated in order.
- *, /, % are calculated before +, -.
<?php
$a = 4;
$b = 3;
$c = 2;
echo ( $a + $b ) * $c; // 14
echo $a * $b / $c; // 6
echo $a + $b - $c; // 5
echo $a + $b * $c; // 10
?>
728x90
LIST
'PHP' 카테고리의 다른 글
[PHP] PHP/operators/increase operator, decrease operator (0) | 2022.08.13 |
---|---|
[PHP] PHP/operators/comparison operators (0) | 2022.08.08 |
[PHP] PHP/operator/assignment operator (0) | 2022.08.04 |
[PHP] How to create PHP/variables/flow variables (variables within variables) (0) | 2022.08.04 |
[PHP] PHP / Grammar (0) | 2022.08.02 |