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
+AddSum of 2 operand (left operand + right operand)
-SubtractDifference of 2 operands (left operand – right operand)
*MultiplyProduct of 2 operands (left operand * right operand)
/DivisionQuotient of 2 operands (left operand / right operand)
%ModulusRemainder 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 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 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 variable is incremented by one (value becomes 5) and then displays the value
  3. In the second echo statement the value of variable is displayed first then increments it by one. At the time of display, the value of variable is 5 and after that it has been incremented to 6
  4. In the third echo statement, the value of 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 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 value of a is equal to value of b even if data type is different then this expression returns true else returns false.
a === bChecks if value of a is equal to value of b and data type of both the operands are same then this expression returns true else false
a != b Or a <> bIf value of a is not equal to value of b then this expression returns true else false.
a !== bIf value of a is not equal to value of b or data type of a is not equal to data type of b then this expression returns true else false.
a > bIf value of a is greater than value of b then this expression returns true else false.
a < bIf value of a is greater than or equal to value of b then this expression returns true else false.
a >= bIf value of a is less than or equal to value of b then this expression returns true else false.
a <= bThis operator decrements the value of variable by one and then returns it.





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