PHP Operators
To carry out operations on variables and values, operators are employed.
In the following groupings PHP splits the operators :
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
PHP Arithmetic Operators
The PHP arithmetic operators are used in conjunction with numeric data to execute standard arithmetic operations like as addition, subtraction, multiplication, and so on.
Example :- Consider, There are two variables called $a = 12 and $b = 5
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | $a + $b | 17 |
- | Subtraction | $a - $b | 7 |
* | Multiplication | $a * $b | 60 |
/ | Division | $a / $b | 2.4 |
% | Modulus | $a % $b | 2 |
** | Exponentiation | $a ** $b | End of $a and $b |
PHP Assignment Operators
With numerical numbers, the PHP assignment operators are used to type a value into a variable.
The "=" assignment operator is the most fundamental assignment operator in PHP. It signifies that the value of the assignment expression on the right is assigned to the left operand.
Assignment | Same As | Description | |
---|---|---|---|
a = b | a = b | The left operand gets set to the value of the expression on the right | |
a+=b | a = a+b | Addition | |
a-=b | a = a-b | Subtraction | |
a*=b | a = a*b | Multiplication | |
a/=b | a = a/b | Division | |
x%=y | x = x%y | Modulus |
PHP Comparison Operators
Two values (number or string) are compared with PHP comparison operators.
Example :- Consider, There are two variables called $a = 12 and $b = 5
Operator | Name | Example | Description | Result |
---|---|---|---|---|
== | Equal | $a == $b | Returns true if $a is equal to $b | FALSE |
=== | Identical | $a === $b | Returns true if $x is equal to $y, and they are of the same type | FALSE |
!= | Not Equal | $a != $b | Returns true if $a is not equal to $b | TRUE |
<> | Not Equal | $a <> $b | Returns true if $a is not equal to $b | TRUE |
!== | Not Identical | $a !== $b | Returns true if $a is not equal to $b, or they are not of the same type | FALSE |
> | Greater Than | $a > $b | Returns true if $a is greater than $b | TRUE |
< | Less Than | $a < $b | Returns true if $a is less than $b | FALSE |
>= | Greater Than or Equal to | $a >= $b | Returns true if $a is greater than or equal to $b | TRUE |
<= | Less Than or Equal to | $a <= $b | Returns true if $a is less than or equal to $b | FALSE |
<=> | Spaceship | $a <=> $b | Returns an integer less than, equal to, or greater than zero, depending on if $a is less than, equal to, or greater than $b. |
PHP Increment / Decrement Operators
The PHP increment functionality is used to increase the value of the variable.
decreasing PHP operators are used to decreasing the value of the variable.
Operator | Name | Description |
---|---|---|
++$a | Pre-Increment | Increments $a by one, then returns $a |
$a++ | Post-Increment | Returns $a, then increments $a by one |
--$a | Post-Decrement | Returns $a, then decrements $a by one |
Related Links
PHP Logical Operators
To match conditional statements, PHP logical operators are utilized.
Operator | Name | Example | Result |
---|---|---|---|
and | And | $a and $b | True if both $a and $b are true |
or | Or | $a or $b | True if either $a or $b is true |
xor | Xor | $a xor $b | True if either $a or $b is true, but not both |
&& | And | $a && $b | True if both $a and $b are true |
|| | Or | $a || $1b | True if either $a or $b is true |
! | Not | !$a | True if $a is not true |
PHP String Operators
String operators are provided by PHP in the form of two operators.
Operator | Name | Example | Result |
---|---|---|---|
. | Concatenation | $msg1 . $msg2 | Concatenation of $msg1 and $msg2 |
.= | Concatenation assignment | $msg1 .= $msg2 | $msg1 .= $msg2 Appends $msg2 to $msg1 |
PHP Array Operators
For the comparison of arrays, PHP array operators are utilized.
Operator | Name | Example | Result |
---|---|---|---|
+ | Union | $a + $b | Union of $a and $b |
== | Equality | $a == $b | Returns true if $x and $y have the same key/value pairs |
=== | Identity | $a === $b | Returns true if $x and $y have the same key/value pairs in the same order and of the same types |
!= | In Equality | $a != $b | Returns true if $a is not equal to $b |
<> | In Equality | $a <> $b | Returns true if $a is not equal to $b |
!== | Not Identity | $a !== $b | Returns true if $x is not identical to $y |
Related Links
PHP Conditional Assignment Operators
In order to set a value dependent of conditions, the PHP conditional assignment operators will :
Operator | Name | Example | Result |
---|---|---|---|
?: | Tenary | $a = expr1 ? expr2 : expr3 | Returns the value of $x. The value of $a expr2 if expr1 = TRUE. The value of $a is expr3 if expr1 = FALSE |
?? | Null coalescing | $a = expr1 ?? expr2 | Returns 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 |