PHP Operators
PHP Operator is a symbol i.e used to perform arithmetic or logical operations on operands. In simple words, operators are used to perform operations on variables or values.
PHP divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
PHP Arithmetic operators
An operator that performs arithmetic operations on groups and numbers. Binary plus and minus symbols are used between groups and numbers to indicate addition and subtraction. etc
Operator | Name | Example |
---|---|---|
+ | Addition | $a + $b |
- | Subtraction | $a - $b |
* | Multiplication | $a * $b |
/ | Division | $a / $b |
% | Modulus | $a % $b |
** | Exponentiation | $a ** $b |
PHP Assignment Operators
The PHP basic assignment operator is "=". and used with numeric values to write a value to a variable.
Operator | Description | Example |
---|---|---|
= | The left operand gets set to the value of the expression on the right | $a = $b |
+= | Addition | $a += $b |
-= | Subtraction | $a -= $b |
*= | Multiplication | $a *= $b |
/= | Division | $a /= $b |
%= | Modulus | $a %= $b |
PHP Comparison Operators
The PHP Comparison operators — operators that compare values and return true or false .
Operator | Name | Return | Example |
---|---|---|---|
== | Equal (bool) | true if $a is equal to $b after type juggling. | $a == $b |
=== | Identical (bool) | true if $a is equal to $b, and they are of the same | $a === $b |
!= | Not equal (bool) | true if $a is not equal to $b after type juggling. | $a != $b |
<> | Not equal (bool) | true if $a is not equal to $b after type juggling. | $a <> $b |
!== | Not identical (bool) | true if $a is not equal to $b, or they are not of the same type. | $a !== $b |
< | Less than (bool) | true if $a is strictly less than $b. | $a < $b |
> | Greater than (bool) | true if $a is strictly greater than $b. | $a > $b |
<= | Less than or equal to (bool) | true if $a is less than or equal to $b. | $a <= $b |
>= | Greater than or equal to (bool) | true if $a is greater than or equal to $b. | $a >= $b |
<=> | Spaceship (bool) | An int less than, equal to, or greater than zero when $a is less than, equal to, or greater than $b, respectively. | $a <=> $b |
PHP Increment/Decrement operators
The PHP Increment and decrement operators are unary operators that add or subtract one, to or from their operand
Increment and decrement operators are two different category
- Pre-increment and decrement operators.
- Post-increment and decrement operators.
Operator | Name | Description |
---|---|---|
++$a | Pre-increment | Increments $a by one, then returns $a. |
$a++ | Post-increment | Returns $a, then increments $a by one. |
--$a | Pre-decrement | Decrements $a by one, then returns $a. |
$a-- | Post-decrement | Returns $a, then decrements $a by one. |
PHP Logical Operators
The PHP logical operators reason for the two different variations of "and" and "or" operators is that they operate at different precedence's.
Operator | Name | Description | Example |
---|---|---|---|
and | And | true if both $a and $b are true. | $a and $b |
or | Or | true if either $a or $b is true. | $a or $b |
xor | Xor | true if either $a or $b is true, but not both. | $a xor $b |
! | Not | true if $a is not true. | !$a |
&& | And | true if both $a and $b are true. | $a && $b |
|| | Or | true if either $a or $b is true. | $a || $b |
PHP String Operators
There are two PHP string operators. that are specially designed for strings. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.=')
Operator | Name | Description |
---|---|---|
. | Concatenation | This operator use to concatenation tow string |
.= | Concatenation assignment | This operator use to append string |
PHP Array Operators
Some operators are used to manage two or more arrays. like add two array or compared tow array.
Operator | Name | Description | Example |
---|---|---|---|
= | Union | Union of $a and $b. | $a + $b |
== | Equality | true if $a and $b have the same key/value pairs. | $a == $b |
=== | Identity | true if $a and $b have the same key/value pairs in the same order and of the same types. | $a === $b |
! = | Inequality | true if $a is not equal to $b. | $a != $b |
<> | Inequality | true if $a is not equal to $b. | $a <> $b |
! == | Non-identity | true if $a is not identical to $b. | $a !== $b |
PHP Conditional Assignment Operators
The PHP conditional assignment operators are used to set a value depending on conditions like if else :
Operator | Name | Example |
---|---|---|
?: | Ternary | $a == $b ? $a : $b |
?? | Null coalescing | $a= $a ?? $b |