Operators in PHP
There are following Operators are used in PHP.
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
1) PHP Arithmetic Operators .
PHP Arithmetic operators are used to mathematical operation uch as addition, subtraction, multiplication etc.
Operator |
Description |
Example |
Result |
+ |
Addition |
$x + $y |
Sum of $x and $y |
- |
Subtraction |
$x - $y |
Difference of $x and $y. |
* |
Multiplication |
$x * $y |
Product of $x and $y. |
/ |
Division |
$x / $y |
Quotient of $x and $y |
% |
Modulus |
$x % $y |
Remainder of $x divided by $y |
Example :-
Output
2) PHP Assignment Operators
The assignment operators are used to assign values to variables..
assignment operator in PHP is "=" .
Example :-
Output
3) PHP Comparison Operators
comparison operators are used to compare Boolean value true or false .
Operator |
Name |
Example |
Result |
== |
Equal |
$x == $y |
True if $x is equal to $y |
=== |
Identical |
$x === $y |
True if $x is equal to $y, and they are of the same type |
!= |
Not equal |
$x != $y |
True if $x is not equal to $y |
<> |
Not equal |
$x <> $y |
True if $x is not equal to $y |
!== |
Not identical |
$x !== $y |
True if $x is not equal to $y, or they are not of the same type |
< |
Less than |
$x < $y |
True if $x is less than $y |
> |
Greater than |
$x > $y |
True if $x is greater than $y |
>= |
Greater than or equal to |
$x >= $y |
True if $x is greater than or equal to $y |
<= |
Less than or equal to |
$x <= $y |
True if $x is less than or equal to $y |
Example :-
Output
bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
4) PHP Incrementing and Decrementing Operators
Increment operators are used to increment a variable's value.
Decrement operators are used to decrement a variable's value
Operator |
Name |
Effect |
++$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 |
Example :-
Output
5) PHP Logical Operators
PHP logical operators are used to conditional statements.
Operator |
Name |
Example |
Result |
and |
And |
$x and $y |
True if both $x and $y are true |
or |
Or |
$x or $y |
True if either $x or $y is true |
xor |
Xor |
$x xor $y |
True if either $x or $y is true, but not both |
&& |
And |
$x && $y |
True if both $x and $y are true |
|| |
Or |
$x || $y |
True if either $$x or $y is true |
! |
Not |
!$x |
True if $x is not true |
Example :-
Output
6) PHP String Operators
PHP has two operators that are designed for strings.
Operator |
Description |
Example |
Result |
. |
Concatenation |
$str1 . $str2 |
Concatenation of $str1 and $str2 |
.= |
Concatenation assignment |
$str1 .= $str2 |
Appends the $str2 to the $str1 |
Example :-
Output
Hello World!
Hello World!
7) PHP Array Operators
Array operators are used to compare arrays:
Operator |
Name |
Example |
Result |
+ |
Union |
$x + $y |
Union of $x and $y |
== |
Equality |
$x == $y |
True if $x and $y have the same key/value pairs |
=== |
Identity |
$x === $y |
True if $x and $y have the same key/value pairs in the same order and of the same types |
!= |
Inequality |
$x != $y |
True if $x is not equal to $y |
<> |
Inequality |
$x <> $y |
True if $x is not equal to $y |
!== |
Non-identity |
$x !== $y |
True if $x is not identical to $y |
Example :-
Output
array(6) {
["a"]=>
string(3) "Red"
["b"]=>
string(5) "Green"
["c"]=>
string(4) "Blue"
["u"]=>
string(6) "Yellow"
["v"]=>
string(6) "Orange"
["w"]=>
string(4) "Pink"
}
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
|