29
Php Operator
Arithmetic Operators
Arithmetic operators work with numeric values to perform common arithmetical operations.
<?php
$num1=8;
$num2=6;
//Addition
echo $num1+$num2; //14
//Substraction
echo $num1-$num2; //2
//Multiplication
echo $num1*$num2; //48
//Division
echo $num1/$num2 ; //1.33333333
?>
Modulus
The modulus operator, represented by the % sign, returns the remainder of the division of the first operand by the second operand:
<?php
$x=14;
$y=3;
echo $x%$y //2
?>
Increment & Decrement
The increment operators are used to increment a variable's value.
The decrement operators are used to decrement a variable's value.
<?php
$x++; // equivalent to $x = $x+1;
$x--; // equivalent to $x = $x-1;
?>
Increment and decrement operators either precede or follow a variable.
<?php
$x++; // post-increment
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement
?>
The difference is that the post-increment returns the original value before it changes the variable, while the pre-increment changes the variable first and then returns the value.
Example:
<?php
$a = 2; $b = $a++; // $a=3, $b=2
$a = 2; $b = ++$a; // $a=3, $b=3
?>
29