An operator is a symbol that manipulates one or more values, usually producing a new value in the process. Meanwhile, an expression in PHP is anything that evaluates to a value; this can be any combination of values, variables, operators, and functions. In the preceding example, $x + $y is an expression. Here are some more examples of expressions:
$x + $y + $z
gettype( $test_var ) The values and variables that are used with an operator are known as operands .
Operators in PHP can be grouped into ten types, as follows:
|Arithmetic||Perform common arithmetical operations such as addition and subtraction|
|Assignment||Assign values to variables|
|Bitwise||Perform operations on individual bits in an integer|
|Comparison||Compare values in a Boolean fashion ( true or false is returned)|
|Error||Control Affect error handling|
|Execution||Cause execution of commands as though they were shell commands|
|Incrementing/Decrementing||Increment or decrement a variable ’ s value|
|Logical||Boolean operators such as and or and not that can be used to include or exclude|
|String||Concatenates (joins together) strings (there ’ s only one string operator)|
|Array||Perform operations on arrays|
The Assignment Operator
You have met the assignment operator each time we have initialized a variable. It consists of the single character =. The assignment operator takes the value of its right-hand operand and assigns it to its left-hand operand:
The variable $name now contains the string “matt”. Interestingly, this construct is an expression. It might look at first glance that the assignment operator simply changes the variable $name without producing a value, but in fact, a statement that uses the assignment operator always resolves to a copy of the value of the right operand. Thus print ( $name = “matt” );
prints the string “matt” to the browser in addition to assigning “matt” to $name.
$x = 4;
$x += 4; // $x now equals 8 is equivalent to $x = 4;
$x = $x + 4; // $x now equals 8
There is an assignment operator for each of the arithmetic operators and one for the concatenation operator.
|+=||$x += 5||$x = $x + 5|
|− =||$x − = 5||$x = $x − 5|
|/=||$x /= 5||$x = $x / 5|
|*=||$x *= 5||$x = $x *5|
|%=||$x%=5||$x = $x % 5|
|.=||$x .= “test”||$x = $x” test”|
The arithmetic operators do exactly what you would expect. The addition operator adds the right operand to the left operand. The subtraction operator subtracts the right-hand operand from the left. The division operator divides the left-hand operand by the right. The multiplication operator multiplies the left-hand operand by the right. The modulus operator returns the remainder of the left operand divided by the right.
The Concatenation Operator
The concatenation operator is a single dot. Treating both operands as strings, it appends the right-hand operand to the left. So
Regardless of the data types of the operands, they are treated as strings, and the result always is a string.
Comparison operators perform tests on their operands. They return the boolean value true if the test is successful, or false otherwise. This type of expression is useful in control structures, such as if and while statements.
To test whether the value contained in $x is smaller than 5, for example, you would use the less than operator:
$x < 5
If $x contained 3, this expression would be equivalent to the value true. If $x contained 7, the expression would resolve to false.
|==||Equivalence||Left is equivalent to right||$x == 5||false|
|!=||Non-equivalence||Left is not equivalent to right||$x != 5||true|
|===||Identical||Left is equivalent to right and they are the same type||$x === 5||false|
|gt;||Greater||Left is than greater than right||$x gt;4||false|
|gt;=||Greater than or equal to||Left is greater than or equal to right||$x gt;= 4||true|
|lt;||Less than||Left is less than right||x lt; 4||false|
|lt;=||Less than or equal to||Left is less than or equal to right||$x lt;= 4||true|
These operators are most commonly used with integers or doubles, although the equivalence operator is also used to compare strings.