PHP

[PHP] PHP/operators/arithmetic operators

OOQ 2022. 8. 8. 09:25
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