PHP Operator are used to perform operations such as addition, division, comparison, etc. with variables. When you use operator with a variable then that variable is called operand.
PHP divides the operators in the following groups:
- Arithmetic Operators
- Assignment Operators
- Logical Operators
- Comparison Operators
- Increment/Decrement Operators
- String Operators
- Array Operators
Arithmetic php operator are used to perform arithmetic operations with variables. A list of some common arithmetic operators is being given below.
|+||Addition||$a + $b||Addition of 2 variables|
|–||Subtraction||$a – $b||Subtraction of 2 variables|
|*||Multiplication||$a * $b||Multiplication of 2 variables|
|/||Division||$a / $b||Division of 2 variables|
|%||Modulus||$a % $b||Remain from division|
|**||Exponentiation||$a ** $b||First variable raise to power to second variable|
Assignment php operator is used to assign a variable to a value. (=) assignment operator. These values can be assigned by 2 types.
- In the first method, you assign the value directly. Assigning Direct Value is quite simple. You create a variable and assign (=) the assignment operator and write the value in front of it.
$name = "Ram";
- In the second way you assign a variable to another variable. By doing so, the value of the variable of the right side gets assigned to the variable of the left side. This is called assigning by reference.
$a = 9; $b = $a //$b contains 9
|$a = $b||The value of the variable of the right side gets assigned to the variable of the left side|
|$a += $b||Addition|
|$a -= $b||Subtraction|
|$a *= $b||Multiplication|
|$a /= $b||Division|
|$a %= $b||Modulus|
The logical php operator are used to combine conditional statements.
|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 || $b||True if either $a or $b is true|
|!||Not||!$a||True if $a is not true|
Comparison operators are used to compare values of two variables. Comparison php operator are used mostly in control statements. The result of Comparison variables is TRUE or FALSE.
|==||Equal||$a == $b||True if $a is equal to $b|
|===||Identical||$a===$b||True if $a is equal to $b, and they are of the same type|
|!=||Not equal||$a != $b||True if $a is not equal to $b|
|<>||Not equal||$a <> $b||True if $a is not equal to $b|
|!==||Not identical||$a !==$b||True if $a is not equal to $b, or they are not of the same type|
|>||Greater than||$a > $b||True if $a is less than $b|
|<||Less than||$a < $b||True if $a is greater than $b|
|>=||Greater than or equal to||$a >= $b||True if $a is greater than or equal to $b|
|<=||Less than or equal to||$a <= $b||True if $a is less than or equal to $b|
Increment and Decrements PHP Operator
With increment and decrements operators, you can increment and decrement the value of a variable. These operators (++) and (–) are. Such variables can be used in 2 ways.
- Post increment / decrements – In this kind of use, operators are placed behind the variable. After printing the variable, it increases or decreases its value from number 1.
- Pre increment / decrements – In this kind of use, php operator are placed before the variable. These increments increase or decrease before printing.
|$a++||Post-increment||Returns $a, then increments $a by one|
|++$a||Pre-increment||Increments $a by one, then returns $a|
|$a–||Post-decrement||Returns $a, then decrements $a by one|
|–$a||Pre-decrement||Decrements $a by one, then returns $a|
There are two operators for strings.
|.||Concatenation||$a . $b||Concatenation of $a and $b|
|.=||Concatenation assignment||$a .= $b||Appends $a to $b|
PHP also provides some array operators that only apply to arrays.
|+||Union||$a + $b||Union of $a and $b|
|==||Equality||$a == $b||Returns true if $a and $b have the same key/value pairs|
|===||Identity||$a===$b||Returns true if $a and $b have the same key/value pairs in the same order and of the same types|
|!=||Inequality||$a != $b||Returns true if $a is not equal to $b|
|<>||Inequality||$a <> $b||Returns true if $a is not equal to $b|
|!==||Non-identity||$a !== $b||Returns true if $a is not identical to $b|