PHP - Operator Arithmetic operators

Introduction

In PHP, the arithmetic operators (plus, minus, and so on) can create expressions.

For example, $c = $a + $b adds $a and $b and assigns the result to $c.

Here's a full list of PHP's arithmetic operators:

Operator Meaning Example Equation
+ addition 6 + 3 = 9
-subtraction 6 - 3 = 3
*multiplication 6 * 3 = 18
/division6 / 3 = 2
%modulus 6 % 3 = 0 (the remainder of 6/3)

Arithmetic operators are easy to use.

Addition, subtraction, multiplication, and division (+, -, *, and /) do as their names say.

Modulus (%) gives the remainder of the division of two operands.

Exponentiation (**) raises the first operand to the power of the second.

Finally, negation - negates the operand.

This last one is the only arithmetic operator that takes just one operand.

Demo

<?php
    $a = 10; //from w w w.  j a v  a  2s .  co m
    $b = 3; 
    var_dump($a + $b); // 13 
    var_dump($a - $b); // 7 
    var_dump($a * $b); // 30 
    var_dump($a / $b); // 3.333333... 
    var_dump($a % $b); // 1 
    var_dump($a ** $b); // 1000 
    var_dump(-$a); // -10 
?>

Result