Articles → PHP → Operators In PHP

Operators In PHP






Purpose





Types Of Operators


  1. Arithmetic operators
  2. Assignment operators
  3. Incrementing/Decrementing operators
  4. Comparison operators
  5. Logical operators

Arithmetic Operators


OperatorNameDescription
+AddThe sum of 2 operands (left operand + right operand)
-SubtractDifference of 2 operands (left operand – right operand)
*MultiplyProduct of 2 operands (left operand * right operand)
/DivisionThe quotient of 2 operands (left operand / right operand)
%ModulusThe remainder of division when one operand is divided by other
- OperandNegationOpposite of operand
.ConcatenationCombine 2 strings into one.


	<?PHP
echo "Addition", "\n", 1 + 2, "<br>";
echo "Subtract", "\n", 2 - 1, "<br>";
echo "Multiplication", "\n", 2 * 3, "<br>";
echo "Division", "\n", 4 * 2, "<br>";
echo "Modulus", "\n", 5 % 3, "<br>";
echo "Negation", "\n", -3, "<br>";
echo "Concatenation", "\n", "wel" . "come", "<br>";
?>



















Assignment Operators


OperatorsHow they are used
a = ba = b
a += ba = a + b
a -= ba = a + b
a *= ba = a * b
a /= ba = a / b
a %= ba = a % b


<?php
$a = 1;
$b = 2;

$a = $b;
echo $a, "\n";

$a += $b;
echo $a, "\n";

$a -= $b;
echo $a, "\n";

$a *= $b;
echo $a, "\n";

$a /= $b;
echo $a, "\n";

$a %= $b;
echo $a, "\n";
?>





Incrementing/Decrementing Operators


OperatorsDescription
++xThis operator increments the value of the variable by one and then returns it.
x++This operator returns the variable and then increments it by one.
--yThis operator decrements the value of the variable by one and then returns it.
y--This operator returns the variable and then decrements it by one.


<?php
$var = 4;
echo ++$var;
echo $var++;
echo --$var;

echo $var--;
?>






  1. Value of the variable "$var" is initialized with 4
  2. In the first echo statement, the value of the variable is incremented by one (value becomes 5) and then displays the value
  3. In the second echo statement the value of the variable is displayed first then increments by one. At the time of display, the value of the variable is 5 and after that, it has been incremented to 6
  4. In the third echo statement, the value of the variable is first decremented by one and then displays on the screen. After the last operation the value was incremented to 6 and now after the value is decremented the value of the variable becomes 5 again. So, the third echo statement also displays the value as 5
  5. In the last echo statement, the value is first displayed and then decrements. So, the last echo statement also displays 5

Comparison Operators


OperatorsDescription
a == bChecks if the value of a is equal to the value of b even if the data type is different than this expression returns true else returns false
a === bChecks if the value of a is equal to the value of b and the data type of both the operands are the same then this expression returns true else false
a != b Or a <> bIf the value of a is not equal to the value of b, then this expression returns true else false
a !== bIf the value of a is not equal to the value of b or the data type of a is not equal to the data type of b then this expression returns true else false
a > bIf the value of a is greater than the value of b then this expression returns true else false
a < bIf the value of a is less than the value of b then this expression returns true else false
a >= bIf the value of a is greater than or equal to the value of b then this expression returns true else false
a <= bIf the value of a is less than or equal to the value of b then this expression returns true else false





Logical Operators


OperatorsNameDescription
x and y / x && yAndTrue of both x and y are true
x or y / x || yOrTrue if either one or both is true
x xor yXorTrue if either one is true but not both
!xNotTrue if x is not true





Posted By  -  Karan Gupta
 
Posted On  -  Sunday, May 19, 2013

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250