PHP Operators
HP operators are the essential tools you use to perform calculations, comparisons, and manipulations on data within your code. They act as the glue that connects variables, values, and expressions to achieve the desired outcome.
PHP divides the operators in the following groups:
• Arithmetic operators : Examples: +, -, *, /, %
• Assignment operators : Examples: =, +=, -=, *=, /=, %=
• Comparison operators : Examples: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to)
• Increment/Decrement operators : Pre-increment (++) and post-increment (++) or pre-decrement (--) and post-decrement (--) variations exist, affecting the order of operations.
• Logical operators : Examples: && (AND), || (OR), ! (NOT)
• String operators : Examples: . (concatenation), === (identical string comparison)
• Array operators : Examples: + (concatenation), == (loose comparison)
• Conditional assignment operators : Example: $result = ($age >= 18) ? "adult" : "minor";
• PHP Arithmetic Operators
Example
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds together two values | $a + $b |
- | Subtraction | Subtracts one value from another | $a - $b |
* | Multiplications | Multiplies two values | $a * $b |
* | Division | Quotient of operands | $a / $b |
% | Modulus | Remainder of operands | $a % $b |
** | Exponentiation | $a raised to the power $b | $a ** $b |
• PHP Assignment Operators
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
Example
Assignment | Same as... | Description |
---|---|---|
x = y | x = y | The left operand gets set to the value of the expression on the right |
x += y | x = x + y | Addition |
x -= y | x = x - y | Subtraction |
x *= y | x = x * y | Multiplication |
x /= y | x = x / y | Division |
x %= y | x = x % y | Modulus |
• PHP Comparison Operators
Example
Operator | Name | Description | Example |
---|---|---|---|
== | Equal | Returns true if $x is equal to $y | $x == $y |
=== | Identical | Returns true if $x is equal to $y, and they are of the same type | $x === $y |
!= | Not equal | Returns true if $x is not equal to $y | $x != $y |
<> | Not equal | Returns true if $x is not equal to $y | $x <> $y |
!== | Not identical | Returns true if $x is not equal to $y, or they are not of the same type | $x !== $y |
> | Greater than | Returns true if $x is greater than $y | $x > $y |
!== | Not identical | Returns true if $x is not equal to $y, or they are not of the same type | $x !== $y |
> | Greater than | Returns true if $x is greater than $y | $x > $y |
!== | Not identical | Returns true if $x is not equal to $y, or they are not of the same type | $x !== $y |
> | Greater than | Returns true if $x is greater than $y | $x > $y |
< | Less than | Returns true if $x is less than $y | $x < $y |
>= | Greater than or equal to | Returns true if $x is greater than or equal to $y | $x >= $y |
<= | Less than or equal to | Returns true if $x is less than or equal to $y | $x <= $y |
<=> | Spaceship | Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. | $x <=> $y |
• PHP Increment / Decrement Operators
The PHP decrement operators are used to decrement a variable's value.
Example
Assignment | Same as... | Description |
---|---|---|
++$x | Pre-increment | Increments $x by one, then returns $x |
$x++ | Post-increment | Returns $x, then increments $x by one |
--$x | Pre-decrement | Decrements $x by one, then returns $x |
$x-- | Post-decrement | Returns $x, then decrements $x by one |
x /= y | x = x / y | Division |
x %= y | x = x % y | Modulus |
• PHP Logical Operators
Example
Operator | Name | Description | Example |
---|---|---|---|
and | And | True if both $x and $y are true | $x and $y |
or | Or | True if either $x or $y is true | $x or $y |
xor | Xor | True if either $x or $y is true, but not both | $x xor $y |
&& | And | True if both $x and $y are true | $x && $y |
|| | Or | True if either $x or $y is true | $x || $y |
! | Not | True if $x is not true | !$x |
• PHP String Operators
Example
Operator | Name | Description | Example |
---|---|---|---|
. | Concatenation | Concatenation of $txt1 and $txt2 | $txt1 . $txt2 |
.= | Concatenation assignment | Appends $txt2 to $txt1 | $txt1 .= $txt2 |
• PHP Array Operators
Example
Operator | Name | Description | Example |
---|---|---|---|
+ | Union | Union of $x and $y | $x + $y |
== | Equality | Returns true if $x and $y have the same key/value pairs | $x == $y |
=== | Identity | Returns true if $x and $y have the same key/value pairs in the same order and of the same types | $x === $y |
!= | Inequality | Returns true if $x is not equal to $y | $x != $y |
<> | Inequality | Returns true if $x is not equal to $y | $x <> $y |
!== | Non-identity | Returns true if $x is not identical to $y | $x !== $y |