Operators in PHP with Examples

In this article we are going to cover Operators in PHP with Examples,Types of Operators in PHP, Arithmetic Operators in PHP, Logical Operators in PHP, Comparison Operators in PHP, Comparison Operators in PHP.

What is Operators in PHP?

PHP operator is a symbol.

PHP Operator is used to perform some operation on operands. Operators are used to perform operations on variables or values. For example, “5 – 2 = 3”  in this example ‘-’ is an operator. It takes two values 5 and 2 , performs subtraction operation on them to give 5.

PHP supports various types of operations like arithmetic operations (addition, subtraction, etc) , logical operations (AND, OR, etc), etc.

Types of Operators in PHP :

  • Arithmetic Operators
  • Logical Operators
  • Comparison Operators
  • Assignment Operators
  • Increment/Decrement Operator
  • Array operator
  • String Operators

Arithmetic Operators in PHP:

The arithmetic operators are use to perform mathematical operations like addition, subtraction, multiplication etc. with numeric values.

OperatorDescriptionSyntax
+Addition : add two operands$a + $b
Subtraction : subtracts two operands$a – $b
*Multiplication: multiplies two operands$a * $b
/Division: divides the first operand by the second$a / $b
%Modulus : divides first operand by second operand and returns remainder$a % $b
**Exponentiation (power):  returns first raised to power second$a ** $b

Note : The exponentiation has been introduced in PHP 5.6.

Example of arithmetic operator in PHP:

<?php
$a = 15;
$b = 9;

echo($a + $b), "<br>";
echo($a - $b), "<br>";
echo($a * $b), "<br>";
echo($a / $b), "<br>";
echo($a % $b);
  
?>

Output:

24
6
135
1.6666666666667
6

Logical Operators in PHP :

Logical operators allow a program to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value. Then the value of the conditions is used to determine the overall value of the operand1 operator operand2.

OperatorDescriptionSyntax
andLogical AND: If both the operands are true then result become true. If anyone operand is false then result become false$a and $b
orLogical OR: If one operand true or another is false then result become true$a or $b
!Logical NOT: If both operands are true then result become true. otherwise anyone operand is false then result become false.!$a
xorLogical XOR: If either of the operand is true then become true. And if both are same then result become false.$a xor $b
&&Logical AND: If the both operands are true then result become true otherwise false.$a && $b
||Logical OR: If either of the operand is true then result become true otherwise false.$a || $b

Example of logical Operator in PHP:

<?php
  
$a = 50;
$b = 30;
  	
if ($a == 50 and $b == 30)
    echo "and Success <br>";
  
if ($a == 50 or $b == 20)
    echo "or Success <br>";
  
if ($a == 50 xor $b == 20)
    echo "xor Success <br>";
  
if ($a == 50 && $b == 30)
    echo "&& Success <br>";
  
if ($a == 50 || $b == 20)
    echo "|| Success <br>";
  
if (!$c)
    echo "! Success ";
  
?>

Output:

and Success
or Success
xor Success
&& Success
|| Success
! Success

Comparison Operators in PHP:

Comparison Operators used to compare two elements and outputs the result in boolean form.

OperatorDescriptionSyntax
Greater than: Result become True when left operand is greater than the right.$a > $b
Less than: If left operand is less than right then result become True.$a < $b
==Equal to: If both operands are equal then result become True.a$ == $b
>=Greater than or equal to: If left operands is greater than or equal to the right then result become True.a$ >= $b
<=Less than or equal to: If left operands is less than or equal to the right then result become True.a$ <= $b
!=Not equal to: If both operands are not equal then result become Truea$ != $b
<> Not equal to : if the both operands are unequal then result become True.$a <> $b
===Identical : if both operands are equal and are of the same type then result become True.$a === $b
!==Not Identical : If both operands are unequal and are of difference types then result become True.$a == $b

Example of Comparison Operators in PHP:

<?php
  
$a = 40;
$b = 20;
$c = "40";

var_dump($a == $c) + "<br>";
var_dump($a != $b) + "<br>";
var_dump($a <> $b) + "<br>";
var_dump($a === $c) + "<br>";
var_dump($a !== $c) + "<br>";
var_dump($a < $b) + "<br>";
var_dump($a > $b) + "<br>";
var_dump($a <= $b) + "<br>";
var_dump($a >= $b);
  
?>

Output:

bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)

Assignment Operators in PHP:

In PHP an assignment operators are used to assign value to variables.

OperatorDescriptionSyntax
=Assign value on the left side of the expression.$a = $b
+=Add then Assign: Add right operand with left operand and assign Result to left side operand$a + = $b $a = $a + $b
-=Subtract then Assign :Subtract right operand from left operand and assign result to the left operand$a – = $b $a = $a – $b
*=Multiply then Assign: Multiply right operand with left operand and then assign result to the left operand$a * = $b $a = $a * $b
/=Divide then Assign: Divide left operand with right operand and then assign result  to the left operand$a / = $b $a = $a / $b
%=Divide then Assign: Using left and right operands and assign result to left operand$a % = $b $a = $a %$ b

Example of Assignment Operators in PHP:

<?php
  
// simple assign operator
$a = 99;
echo $a, "<br>";
  
// add then assign operator
$a = 200;
$a += 100;
echo $a, "<br>";
  
// subtract then assign operator
$a = 99;
$a -= 77;
echo $a, "<br>";
  
// multiply then assign operator
$a = 60;
$a *= 20;
echo $a, "<br>";
  
// Divide then assign(quotient) operator
$a = 200;
$a /= 50;
echo $a, "<br>";
  
// Divide then assign(remainder) operator
$a = 40;
$a %= 2;
echo $a;
  
?>

Output:

99
300
22
1200
4
0

Increment/Decrement Operators in PHP:

The increment and decrement operators are used to increase or decrease values of a variable.

OperatorDescriptionSyntax
++Pre-Increment : Increment the value and then return++$a
Pre-Decrement: increment the value and then return–$a  
++Post-Increment: First return and then increment the value$a++
Post-Decrement: First return and then decrement the value$a–

Example of Increment/Decrement Operators in PHP:

<?php
  
$x = 5;
echo ++$x, " First increments then prints <br>";
echo $x, "<br>";
  
$x = 5;
echo $x++, " First prints then increments <br>";
echo $x, "<br>";
  
$x = 5;
echo --$x, " First decrements then prints <br>";
echo $x, "<br>";
  
$x = 5;
echo $x--, " First prints then decrements <br>";
echo $x;
  
?>

Output:

6 First increments then prints
6
5 First prints then increments
6
4 First decrements then prints
4
5 First prints then decrements
4

Array Operators in PHP:

In PHP, array operator are used in case of array. These operators are used to compare the values of arrays.

OperatorDescriptionSyntax
+Union : Union of both$a + $b
==Equality: If both has same key-value pair it returns True$a == $b  
++Post-Increment: First return and then increment the value$a++
!-Inequality: If both are equal it return True$a != $b
===Identity: If both has same key-value pair in the same order and of same type it return True.$a === $b
!==Non-Identity: If both are not identical to each other.$a !== $b
<> Inequality: If both are unequal then return True.$a <> $b

Example of Array Operators in PHP:

<?php
  
$a = array("a" => "Apple", "b" => "banana");
$b = array("a" => "Orange", "b" => "Cherry");
 
var_dump($a + $b);
var_dump($a == $b) + "<br>";
var_dump($a != $b) + "<br>";
var_dump($a <> $b) + "<br>";
var_dump($a === $b) + "<br>";
var_dump($a !== $b) + "<br>";
  
?>

Output:

array(4) {
 ["a"]=>
 string(5) "Apple" 
["b"]=> 
string(6) "Banana"
 ["c"]=> 
string(6) "Orange" 
["d"]=> 
string(6) "Cherry"
 }
bool(false)
bool(true)
bool(true)
bool(false)
bool(true)

String Operators in PHP:

In PHP, the string operator are used to perform the operation on strings.

OperatorDescriptionSyntax
.Concatenation : Concatenated both$a . $b
=Concatenation and assignment: Concatenate first and then assign$a = $a.$b  

Example of PHP string Operator in PHP:

<?php
  
$a = "FOSS";
$b = "Tech";
$c = "Nix";
echo $a . $b . $c, "<br>";
  	
$a .= $b . $c;
echo $a;
  
?>

Output:

FOSSTechNix
FOSSTechNix

Conclusion:

We have covered Operators in PHP with Examples,Types of Operators in PHP, Arithmetic Operators in PHP, Logical Operators in PHP, Comparison Operators in PHP, Comparison Operators in PHP.

Related Articles:

Reference:

PHP Operators official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap