Operators in PHP

PHP supports the standard arithmetic operators for addition, subtraction, multiplication, and division using +,-, *, and / respectively. Each of these operators is a binary operator that acts on two operands which can either be literals, variables or expressions and follow the usual rules of arithmetic when it comes to order of precedence (multiplication and division before addition and subtraction).

$a = 10, $b = 20, $c = 30;
$d = $a + 5;
$d = $a + $b;
$d = $a - $b;
$d = $a + $b * $c;
$d = $a * $b; //d becomes a floating point number with a value .5

$x = 1.5, $y = 3.4, $z = 10.5;
$w = $x + 5.0;
$w = $x + $y;
$w = $x - $y;
$w = $x + $y * $z;
$w = $x * $y;
$w = $x / $y;

//mixing integers and floating point numbers is no problem
$w = $a + $x;

PHP also supports the integer remainder operator using the % symbol. This operator gives the remainder of the result of dividing two integers. Examples:

$x = 10 % 5; //x is 0
$x = 10 % 3; //x is 1
$x = 29 % 5; //x is 4

Licenses and Attributions


Speak Your Mind