PHP Operators

PHP has following types of operators:

  • Assignment Operators
  • Comparison Operators
  • Increment/Decrement Operators
  • Logical Operators
  • Arithmetic Operators

PHP Assignment Operators

The basic assignment operator is “=” in PHP. It means that the left operand gets set to the value of the expression on the right. That is, the value of “$var = 3” is 3.

AssignmentSame OperatorDescription
$x = $y$x = $yLeft operand gets set to the value of the expression on the right
$x += $y$x = $x + $yAddition
$x -= $y$x = $x – $ySubtraction
$x *= $y$x = $x * $yMultiplication
$x /= $y$x = $x / $yDivision
$x %= $y$x = $x % $yModulus
$a .= $b$a = $a.$bConcatenate two strings

PHP Increment/Decrement Operators

OperatorNameDescription
++$xPre-incrementIncrements $x by one then returns $x
$x++Post-incrementReturns $x, then increments $x by one
–$xPre-decrementDecrements $x by one then returns $x
$x–Post-decrementReturns $x, then decrements $x by one

PHP Arithmetic Operators

OperatorNameDescription
$x + $yAdditionSum of $x and $y
$x – $ySubtractionDifference of $x and $y
$x * $yMultiplicationProduct of $x and $y
$x / $yDivisionQuotient of $x and $y
$x % $yModulusReminder of $x divided by $y
-$xNegationOpposite of $x
$a.$bConcatenationConcatenate two strings

Comparison Operators in PHP

Comparison operators allows you to compare two values in PHP programming.

OperatorNameDescriptionExample
$x == $yEqualTrue if $x is equal to $y3==5 returns false
$x === $yIdenticalTrue if $x is equal to $y, and they are of the same type5===”5″ returns false
$x != $yNot equalTrue if $x is not equal to $y3!=5 returns true
$x <> $yNot equalTrue if $x is not equal to $y3<>5 returns true
$x !== $yNot IdenticalTrue if $x is not equal to $y, or they are not of same type8!==”8″ returns true
$x > $yGreater thanTrue if $x is greater than $y5>3 returns true
$x < $yLess thanTrue if $x is less than $y3<5 returns true
$x >= $yGreater than or equal toTrue if $x is greater than or equal to $y3>=5 returns false
$x <= $yLess than or equal toTrue if $x less than or equal to $y3<=5 returns true

Logical Operators in PHP

OperatorNameDescriptionExample
$x and $yAndTrue if both $x and $y are true$x=5
$y=3
($x <10 and $y > 1) returns true
$x or $yOrTrue if either or both $x and $y are true$x=5
$y=3
($x==5 or $y==6) returns true
$x xor $yXorTrue if either $x or $y is true, but not both$x=5
$y=3
($x==5 xor $y==3) returns false
$x && $yAndTrue if both $x and $y are true$x=5
$y=3
($x< 8 && $y >1) returns true
$x || $yOrTrue if either or both $x and $y are true$x=3
$y=5
($x==4 || $y==4) returns false
!$xNotTrue if $x is not true$x=3
$y=6
!($x==$y) returns true

PHP is a popular open-source general-purpose scripting language. You can learn PHP programming with examples by following our PHP articles sequentially.

 231 total views,  2 views today

Leave a Comment

Your email address will not be published. Required fields are marked *

wpChatIcon