Operators In PHP

What are an Operators in PHP ?

Operators in PHP is a symbol that is used to perform operations. In simple words, operators are used to execute the operations on variables or values

In PHP, there is too many operators are there and they are:  

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

Arithmetic Operators

In PHP, arithmetic operators are used to performing simple mathematical operations like addition, subtraction, multiplication, etc.

OperatorNameExampleResult
+Addition$a + $bSum of $a and $b
Subtraction$a – $bDifference of $a and $b
*Multiplication$a * $bProduct of $a and $b
/Division$a / $bQuotient of $a and $b
%Modulus$a % $bRemainder of $a divided by $b
**Exponentiation$a ** $bIt gives the result of raising $a to the $b’th power

Example

<?php
  $x = 4; // Variable 1
  $y = 4; // Variable 2
 
  // Some arithmetic operations on
  // these two variables
  echo ($x + $y), "\n";
  echo($x - $y), "\n";
  echo($x * $y), "\n";
  echo($x / $y), "\n";
?>
 OUTPUT:
 4
 0
 16
 1

Assignment Operators

In PHP, the assignment operators are used to assign values to different variables. “=” operator is used basically in assignment operator.

Operator NameNameResult
=Assign$ = $bThe value in the right operand is assigned to the left operand.
+=Add then Assign$x += $bAddition same as $x = $x + $b
-=Subtract then Assign$x -= $bSubtraction same as $x = $x – $b
*=Multiply then Assign$x *= $bMultiplication same as $x = $x * $b
/=Divide then Assign
(quotient)
$x /= $bFind quotient same as $x = $x / $b
%=Divide then Assign
(remainder)
$x %= $b Find remainder same as $x = $x % $b

Example:

<?php
// Simple assign operator
$y = 75;
echo $y, "\n";

// Add then assign operator
$y = 100;
$y += 200;
echo $y, "\n";

// Subtract then assign operator
$y = 70;
$y -= 10;
echo $y, "\n";

// Multiply then assign operator
$y = 30;
$y *= 20;
echo $y, "\n";

// Divide then assign(quotient) operator
$y = 100;
$y /= 5;
echo $y, "\n";

// Divide then assign(remainder) operator
$y = 50;
$y %= 5;
echo $y;
?>
 OUTPUT:
 75
 300
 60
 600
 20
 0

Bitwise Operators

In PHP, bitwise operators are used to perform the bit-level operations on operands.

OperatorNameExampleExplanation
&And$x & $bBits that are 1 in both $x and $b are set to 1, otherwise 0.
|Or (Inclusive or)$x | $bBits that are 1 in either $x or $b are set to 1
^Xor (Exclusive or)$x ^ $bBits that are 1 in either $x or $b are set to 0.
~Not~$xBits that are 1 are set to 0 and bits that are 0 are set to 1
<< Shift left$x << $bLeft shift the bits of operand $x $b steps
>> Shift right$x >> $bRight shift the bits of $x operand by $b number of places

Increment / Decrement Operators

In PHP, the increment operators are used to increment a variable’s value.

In PHP, the decrement operators are used to decrement a variable’s value.

Operator NameExampleExplanation
++
$x++
Increment++$x Increment the value of $x by one, then return $x
Return $x, then increment the value of $x by one

$x–
decrement–$xDecrement the value of $x by one, then return $x
Return $x, then decrement the value of $x by one
<!DOCTYPE html>
<html>
<body>

<?php
$x = 34;  
echo ++$x;
?>  

</body>
</html>
 OUTPUT: 35

Logical Operators

In PHP, the logical operators are used to perform bit-level operations on operands.

OperatorNameExampleExplanation
andAnd$x and $bReturn TRUE if both $x and $b are true
OrOr$x or $bReturn TRUE if either $x or $b is true
xorXor$x xor $bReturn TRUE if either $ or $b is true but not both
!Not! $xReturn TRUE if $x is not true
&&And$x && $bReturn TRUE if either $x and $b are true
||Or$x || $bReturn TRUE if either $x or $b is true

Example:

<!DOCTYPE html>
<html>
<body>

<?php
$x = 100;  
$y = 50;

if ($x == 100 or $y == 80) {
    echo "Hello GeektoCode!";
}
?>  

</body>
</html>
 OUTPUT:
 Hello GeektoCode!

String Operators

In PHP, there are two operators that are specially designed for strings and used for the concatenation of 2 or more strings using the concatenation operator (‘.’).

OperatorNameExampleExplanation
.Concatenation$x . $yConcatenate both $x and $y
.=Concatenation and Assignment$a .= $bFirst concatenate $a and $b, then assign the concatenated string to $a, e.g. $a = $a . $b
<!DOCTYPE html>
<html>
<body>

<?php
$txt1 = "Hello";
$txt2 = " GeektoCode!";
echo $txt1 . $txt2;
?>  

</body>
</html>
 OUTPUT: Hello GeektoCode!

Array Operators

The Array operators are used in the case of arrays. The array operators and their syntax and operations, that PHP provides for the array operation. To compare arrays we need the array operators.

OperatorNameExampleExplanation
+Union$a + $yUnion of $a and $b
==Equality$a == $yReturn TRUE if $a and $y have the same key/value pair
!=Inequality$a != $yReturn TRUE if $a is not equal to $y
===Identity$a === $yReturn TRUE if $a and $y have the same key/value pair of the same type in the same order
!==Non-Identity$a !== $yReturn TRUE if $a is not identical to $y
<> Inequality$a <> $yReturn TRUE if $a is not equal to $y

Conditional Assignment Operators

In PHP, the conditional assignment operators are used to fix a value depending on conditions:

OperatorNameExampleResult
?:Ternary$x = expr1 ? expr2 : expr3Returns the value of $a. The value of $a is expr2 if expr1 = TRUE. The value of $a is expr3 if expr1 = FALSE
??Null coalescing$x = expr1 ?? expr2Returns the value of $a. The value of $a is expr1 if expr1 exists, and is not NULL. If expr1 does not exist or is NULL, the value of $a is expr2. Introduced in PHP 7

Example:

<!DOCTYPE html>
<html>
<body>

<?php
   // if empty($user) = TRUE, set $status = "GeektoCode"
   echo $status = (empty($user)) ? "GeektoCode" : "logged in";
   echo("<br>");

   $user = "User";
   // if empty($user) = FALSE, set $status = "logged in"
   echo $status = (empty($user)) ? "GeektoCode" : "logged in";
?>  

</body>
</html>
 OUTPUT:  
 GeektoCode
 logged in

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry