Operators indicate compiler which operation to perform. Operators help to perform various calculations on the given data.
Index
PHP provides different types of operators, similar to other programming languages. They are divided into the following categories.
They are used to perform different arithmetic operations between two or more variable
Operator | Description | Example:(here m = 8, n = 2 ) |
---|---|---|
+ | Addition | $x = $m + $n // Ans: $x=10 |
– | Subtraction | $x = $m – $n // Ans: $x=6 |
* | Multiplication | $x = $m * $n // Ans: $x=16 |
/ | Division | $x = $m / $n // Ans: $x=4 |
% | Modulus | $x = $m % $n // Ans: $x=0 |
They are used to perform logical operations like AND Operation, OR Operation, and NOT operations between two or more variables.
Operator | Description | Example:(here m = 8, n = 2 ) |
---|---|---|
&& | AND | $x = ($m > 0) && ($n < 5) // Ans: true |
|| | OR | $x = ($m == 8) || ($n < 0) // Ans: true |
! | NOT | $x = !($n == $m – 6) // Ans: false |
xor | XOR | $x = ($m == 8) xor ($n < 0) // Ans: true |
NOTE:
They are created to compare two or more variables. They are mostly used with logical operators.
Operator | Description | Example:(here m = 8, n = 2 , int) |
---|---|---|
> | greater than | $x = $m > $n // Ans: true |
< | less than | $x = $m < 5 // Ans: false |
>= | greater than Or equal to | $x = $m >= $n // Ans: true |
<= | less than Or equal to | $x = $n <= 2 // Ans: true |
== | equal to | $x = ($m == “8”) // Ans: true |
=== | equal to with Equal type | $x = ($m === 8) // Ans: true |
!= | not equal to | $x = $m != $n //Ans: true |
<> | not equal to | $x = $m <> $n //Ans: true |
!== | not equal to with Not Equal type | $x = $m !== 5 // Ans: true |
<=> | spaceship | $x = ($m <=> $n) |
Note: The spaceship is a new operator introduced in PHP 7.
They are used to increment and decrement the values.
Operator | Description |
---|---|
++ | Increment |
— | Decrement |
Note: Based on the position they are used, we can divide them into two types:
Pre-increment / Post-increment
Example: (m =8 , n = 2)
Pre increment
$x = $m++; // Ans: x = 8 , m = 9
Post increment
$x = ++$n // Ans: x = 3 , n = 3
Pre decrement / Post decrement
Example: (m =8 , n = 2)
Pre decrement
$x = $m–; // Ans: x = 8 , m = 7
Post decrement
$x = –$n // Ans: x = 1 , n = 1
Read Also: What Is GLOBALS in PHP, and How to Use $GLOBALS in PHP
In conditional operators, According to the condition, the result value is assigned to a variable.
They are of two types:
1) Ternary Operators
Syntax:
Variable name = ( condition ) ? val1 : val2
Explanation:
If the condition is true, then val1 is assigned to the variable; otherwise, val2 is assigned to the variable.
Example: (m =8 , n = 2)
$x = ($m > 5) ? 7 : 3 // Ans: x = 7
$x = ($n == 5) ? 7 : 3 // Ans: x = 3
2) Null Coalescing Operators
Syntax:
Variable name = expr1 ?? val2;
Explanation:
The value of the variable from expr1 is assigned, If the variable exists in code, otherwise val2 is assigned.
Example: (m = 8 , n = 2)
$x = $m ?? 7 // Ans: x = 8
Explanation:
Here value of $m variable already exist in the code so that it is assigned to $x.
$x = $p ?? 3 // Ans: x = 3
Explanation:
Here value of $p variable doesn’t exist in the code so that 3 is assigned to $x.
It is used to assign value to the PHP variable.
It is denoted by ‘ = ’ sign.
Example:
$m = “Errorsea”;
$n = 5;
Note: PHP also supports shorthand notations with assignment operators.
Operations | Description | Same as (here m = 8, n = 2 ) |
---|---|---|
$m = $n | assignment | $m = $n // Ans: $m=2 |
$m += $n | Addition | $m = $m + $n // Ans: $m=10 |
$m -= $n | Subtraction | $m = $m – $n // Ans: $m=6 |
$m *= $n | Multiplication | $m = $m * $n // Ans: $m=16 |
$m /= $n | Division | $m = $m / $n // Ans: $m=4 |
$m %= $n | Modulus | $m = $m % $n // Ans: $m=0 |
PHP supports different operators that are used to compare arrays.
Operator | Name | Example (Here $m = [1,2] , $n = [3,4]) |
---|---|---|
+ | union | $x = ($m + $n) // Ans: [1,2,3,4] |
== | equality | $x = ($m == $) // Ans: false |
=== | identity | $x = ($m === $n) // Ans: false |
!= | inequality | $x = ($m != $n) // Ans: true |
<> | inequality | $x = ($m <> $n) // Ans: true |
!== | Non-identity | $x = ($m !== $n) // Ans: true |
Explanation:
They are used to concatenate two or more strings in PHP.
Example 1:
$e1 = “I am”;
$e2 = “ Errorsea”;
$e3 = $e1.$e2; // Ans: “I am Errorsea”
Example 2:
$e1 = “I am”;
$e2 = “ Errorsea”;
4e1 .= $e2; // Ans: “I am Errorsea”
These are the different types of operators used in PHP according to the requirements for providing users the better facilities. Users can take actions according to the result of different operators.
Enjoy Coding 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…