PHP Tutorial


PHP 7 Operators With Examples

Author:- w3alert.com
Updated Date:-01-02-2020

PHP Operators

PHP Operator is many types of symbols that are used to operation variables and values.

PHP supports many types of operators:

  • Arithmetic or Math Operators
  • Assignment Operators
  • Relational or Comparison Operators
  • Bitwise Operators
  • Logical Operators
  • Conditional or Ternary Operators
  • Array Operators
  • Increment or Decrement Operators
  • String Operators
  • Spaceship Operators (Introduced in PHP 7)

PHP Arithmetic/Math Operators

PHP Arithmetic Operators are used in arithmetic operations such as addition (+), subtraction (-), multiplication (*), division (/) and modulus (%).

The following Symbols is used in PHP Arithmetic or Math Operators: 

OPERATOR NAME SYNTAX
+ Addition $a + $b
Subtraction $a – $b
* Multiplication $a * $b
/ Division $a / $b
** Exponentiation $a ** $b
% Modulus $a % $b

The following Example for understanding  PHP Arithmetic Operator:

<!DOCTYPE html>
<html>
<body>

<?php

$num1 =  12;  
$num2 = 4;
// Arithmetic/Math Operators:
echo $num1 + $num2 ."<br/>"; // Addition Operator
echo $num1 - $num2 ."<br/>"; // Substraction Operator
echo $num1 * $num2 ."<br/>"; // Multiplication Operator
echo $num1 / $num2 ."<br/>"; // Division Operator
echo $num1 % $num2 ."<br/>"; // Modulus Operator
echo $num1 ** $num2 ."<br/>"; // Exponentation Operator

?>  

</body>
</html>

PHP Assignment Operators

PHP Assignment Operator is used to assign the value of the variable or operand and the use of the symbol (=).

Some arithmetic operators are also used with assignment operators and are called combined assignment operators. These operators are used to solve mathematical equations.

The following Symbols is used in PHP Assignment Operators: 

OPERATOR NAME SYNTAX
= Assign $a = $b
+= Add then Assign $a += $b
-= Subtract then Assign $a -= $b
*= Multiply then Assign $a *= $b
/= Divide then Assign (quotient) $a /= $b
%= Divide then Assign (remainder) $a %= $b

The following Example for understanding PHP Assignment Operator:

<!DOCTYPE html>
<html>
<body>

<?php 

// Simple Assign Operator 
$num1 = 15; 
echo $num1, "<br>"; 

// Combined Addition Assign Operator 
$num2 = 20; 
$num2 += 30; 
echo $num2, "<br>"; 

// Combined Subtraction Assign Operator 
$num3 = 40; 
$num3 -= 35; 
echo $num3, "<br>"; 

// Combined Multiplication Assign Operator 
$num4 = 50; 
$num4 *= 45; 
echo $num4, "<br>"; 

// Combined Division Assign Operator 
$num5 = 60; 
$num5 /= 10; 
echo $num5, "<br>"; 

// Combined modulus Assign Operator 
$num6 = 70; 
$num6 %= 5; 
echo $num6; 

?> 

</body>
</html>

PHP Relational or Comparison Operators

PHP Relational operators are used to compare the relationship between the values of any two variables or operands.and it will return the result as boolean (true or false) form.

The following Symbols is used in PHP Relational or Comparison Operators: 

OPERATOR NAME SYNTAX
== Equal To $a == $b
!= Not Equal To $a != $b
<> Not Equal To $a <> $b
=== Identical $a === $b
!== Not Identical $a == $b
< Less Than $a < $b
> Greater Than $a > $b
<= Less Than or Equal To $a <= $b
>= Greater Than or Equal To $a >= $b

The following Example for understanding PHP Relational or Comparison Operator:

<!DOCTYPE html>
<html>
<body>

<?php

$num1 = 60;  
$num2 = 40;
$num3 = "40";

var_dump($num1 > $num2); echo "<br/>"; // returns true because values are $num1 greater than $num2 
var_dump($num1 < $num2); echo "<br/>"; // returns false because values are $num1 less than $num2
var_dump($num1 >= $num2); echo "<br/>"; // returns true because values are $num1 greater than $num2 or $num1 equal $num2
var_dump($num1 <= $num2); echo "<br/>"; // returns false because values are $num1 less than $num2 or $num1 equal $num2
var_dump($num1 <> $num2); echo "<br/>"; // returns true because values are $num1 is not equal to $num2
var_dump($num2 == $num3); echo "<br/>"; // returns true because values are $num2 is equal to $num3
var_dump($num2 === $num3); echo "<br/>"; //returns false because values are $num2 is not equal to $num3 (not same datatypes)
var_dump($num2 != $num3); echo "<br/>"; //returns false because values are $num2 is not equal to $num3 
var_dump($num2 !== $num3); echo "<br/>"; //returns true because values are $num2 is not equal to $num3 (not same datatypes)

?> 

</body>
</html>

PHP Bitwise Operators

The Bitwise Operators are commonly used to perform bit-level (binary 0s and 1s) operations on the variables or operands in PHP.

Note:- The PHP Bitwise Operators perform an operation on the binary digits of numbers, which is more control than many programmers need.

The following Symbols is used in PHP Bitwise Operators: 

OPERATOR NAME SYNTAX
& And $a & $b
| Or (inclusive or) $a | $b
^ Xor (exclusive or) $a ^ $b
~ Not ~ $a
<< Shift left $a << $b
>> Shift right $a >> $b

The following Example for understanding Bitwise Operators:

<!DOCTYPE html>
<html>
<body>

<?php

// The PHP Bitwise operators.

// Bitwise AND
$num1 = 10;
$num2 = 5;
$ans = $num1 & $num2;

echo("Bitwise & of 10 and 5 is $ans");

echo("<br>");

// Bitwise OR
$ans = $num1 | $num2;
echo("Bitwise | of 10 and 5 is $ans");

echo("<br>");

// Bitwise XOR
$ans = $num1 ^ $num2;
echo("Bitwise ^ of 10 and 5 is $ans");

echo("<br>");

// Bitwise NOT
$ans = ~$num1;
echo("Bitwise ~ of 10 is $ans");

echo("<br>");

// Bitwise Left shift
$num2 = 1;
$ans = $num1 << $num2;
echo("10 << 1 will be $ans");

echo("<br>");

// Bitwise Right shift
$ans = $num1 >> $num2;
echo("10 >> 1 will be $ans");

echo("<br>");
?>

</body>
</html>

PHP Logical Operators

The Logical Operators are used with conditional statements. It always returns the result as boolean value (TRUE OR FALSE). It works on condition-based means to test more than one condition at a time.

Note: If you want to test more than one condition at a time. For example, you could check to see whether two conditions at a time like the email id and password are correct from the same If Statement. So, a condition can either be met or cannot be met so the result of a conditional statement can either be true or false.

PHP supports the logical operators: AND, OR, NOT, XOR. In PHP There are two types of priority in logical operators like: High Priority (&&,||)and Low Priority (AND, OR) 

The following Symbols is used in PHP Logical Operators:

OPERATOR NAME SYNTAX
and Logical AND $a and $b
or Logical OR $a or $b
xor Logical XOR $a xor $b
&& Logical AND $a && $b
|| Logical OR $a || $b
! Logical NOT !$a

The following Example for understanding PHP Logical Operator:

<!DOCTYPE html>
<html>
<body>

<?php 

$num1 = 45; 
$num2 = 30; 

if ($num1 == 45 && $num2 == 30) // AND Logical Operator with High Priority
    echo "&& true"."<br/>"; 

if ($num1 == 45 || $num2 == 30) // OR Logical Operator with High Priority
    echo "|| ture"."<br/>"; 

if ($num2 != 243)                    // NOT Logical Operator
    echo "! true"."<br/>"; 

if ($num1 == 45 and $num2 == 30) // AND Logical Operator with Low Priority
    echo "and True \n"."<br/>"; 

if ($num1 == 45 or $num2 == 30) // OR Logical Operator with Low Priority
    echo "or ture"."<br/>"; 

if ($num1 == 45 xor $num2 == 30) // XOR Logical Operator
    echo "xor true"."<br/>"; 

?> 
  
</body>
</html>

PHP Conditional or Ternary Operators

PHP Conditional or Ternary Operator depends on the conditional statement. These operators similar work as if .... else condition statement.

PHP Conditional or Ternary Operator is a conditional operator that decreases the length of code while performing comparisons and conditionals

Syntax: 

Condition ? Expression1 : Expression2

The following Symbols is used in PHP Logical Operators:

OPERATOR NAME SYNTAX
?: Ternary Condition ? Expression1 : Expression2

 

The following Example for understanding PHP Logical Operator: 

<!DOCTYPE html>
<html>
<body>

<?php 

$num1 = 10; 

echo ($num1 >= 5) ? 'Condition is True' : 'Condition is False'; // Ternary Operators

?> 
  
</body>
</html>

PHP Array Operators

in PHP, The Array Operators used to perform some operations on two or more operands and array variables. The array operators operations like Union, Equality, Identity, Inequality, and Non-identity.

The following Symbols is used in PHP Array Operators:

OPERATOR NAME SYNTAX
+ Union $a + $b
== Equality $a == $b
!= Inequality $a != $b
=== Identity $a === $b
!== Non-Identity $a !== $b
<> Inequality $a <> $b

The following Example for understanding PHP Array Operator:

<!DOCTYPE html>
<html>
<body>

<?php 

$array1 = array("a"=> "PHP", "b"=> "Python");
$array2 = array("c"=> "Angular", "d"=> "React"); 

var_dump($array1 + $array2); echo "<br/>";
var_dump($array1 == $array2); echo "<br/>";
var_dump($array1 != $array2); echo "<br/>";
var_dump($array1 <> $array2); echo "<br/>";
var_dump($array1 === $array2); echo "<br/>";
var_dump($array1 !== $array2); 

?> 

</body>
</html>

PHP Increment or Decrement Operators

In PHP The Increment/Decrement Operators are used to perform increment/decrement on operands or variables value.

The following Symbols is used in PHP Increment or Decrement Operators:

OPERATOR NAME SYNTAX
++ Pre-Increment ++$a
Pre-Decrement –$a
++ Post-Increment $a++
Post-Decrement $a–

The following Example for understand PHP Increment or Decrement Operator:

<!DOCTYPE html>
<html>
<body>

<?php
$num1 = 50;
$num2 = 50;
echo ++$num1; // Increment then Print
echo "<br>";
echo $num1++; // Print then Increment
echo "<br>";
echo --$num2; // Decrement then Print
echo "<br>";
echo $num2--; // Print then Decrement
?>  

</body>
</html>

PHP String Operators

There are two types of PHP string operators Concatenation (.) and Concatenation assignment (.=) which are used for the design of string.

The following Symbols is used in PHP String Operators Operators:

OPERATOR NAME SYNTAX
. Concatenation $a.$b
.= Concatenation assignment $a.=$b

The following Example for understand PHP String Operators Operator:

<!DOCTYPE html>
<html>
<body>

<?php

$text1 = "Welcome to";
$text2 = " w3alert.com!";
echo $text1 . $text2; // Concatenation PHP String Operator
echo "<br/>";
echo $text1 .= $text2; // Concatenation assignment PHP String Operator

?>  

</body>
</html>

PHP 7 Spaceship Operators

In PHP 7 has added new operator name spaceship operator (<=>), that is used to compare or perform operation two operands or variables.

Note:- If the both operands are equal, the spaceship operator returns 0. The spaceship operator return 1 if value on the left is greater. The spaceship operator return -1 if the value on the right is greater.

The following Symbol is used in PHP 7 Spaceship Operator Operators: 

OPERATOR SYNTAX
$a < $b $a <=> $b
$a > $b $a <=> $b
$a <= $b $a <=> $b
$a >= $b $a <=> $b
$a == $b $a <=> $b
$a != $b $a <=> $b

The following Example for understand PHP 7 Spaceship Operator Operator:

<!DOCTYPE html>
<html>
<body>

<?php

//Integer Comparison with Spaceship Operator

$num = 10;
$num2 = 20;
$num3 = 10;

echo ( $num <=> $num2); echo ("<br/>");
echo ( $num2 <=> $num3); echo ("<br/>");
echo ( $num <=> $num3); echo ("<br/>");
echo ("<br/>");

$num = 10.6;
$num2 = 20.6;
$num3 = 10.6;

//Float Comparison with Spaceship Operator
echo ( $num <=> $num2); echo ("<br/>");
echo ( $num2 <=> $num3); echo ("<br/>");
echo ( $num <=> $num3); echo ("<br/>");
echo ("<br/>");

$a = 'PHP';
$b = 'JAVASCRIPT';
$c = 'PHP';

//String Comparison with Spaceship Operator
echo ( "a" <=> "a"); echo ("<br/>");
echo ( "a" <=> "b"); echo ("<br/>");
echo ( "b" <=> "a"); echo ("<br/>");
?>

</body>
</html>