Abstract
An operator is a symbol that makes the script perform a specific mathematical or logical manipulation. The operators in PHP can be grouped into five types: arithmetic, assignment, comparison, logical, and bitwise operators.
Access provided by Autonomous University of Puebla. Download chapter PDF
An operator is a symbol that makes the script perform a specific mathematical or logical manipulation. The operators in PHP can be grouped into five types: arithmetic, assignment, comparison, logical, and bitwise operators.
Arithmetic Operators
The arithmetic operators include the four basic arithmetic operations, as well as the modulus operator (%), which is used to obtain the division remainder.
$x = 4 + 2; // 6 // addition $x = 4 - 2; // 2 // subtraction $x = 4 * 2; // 8 // multiplication $x = 4 / 2; // 2 // division $x = 4 % 2; // 0 // modulus (division remainder)
An exponentiation operator (**) was introduced in PHP 5.6. It raises the left-side operand to the power of the right-side operand.
$x = 4 ** 2; // 16 // exponentiation
Assignment Operators
The second group is the assignment operators. Most importantly, the assignment operator (=) itself, which assigns a value to a variable.
$x = 1; // assignment
Combined Assignment Operators
A common use of the assignment and arithmetic operators is to operate on a variable and then to save the result back into that same variable. These operations can be shortened with the combined assignment operators.
$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;
The exponentiation operator added in PHP 5.6 also received a shorthand assignment operator.
$x **= 5; // $x = $x**5;
Increment and Decrement Operators
Another common operation is to increment or decrement a variable by one. This can be simplified with the increment (++) and decrement (--) operators.
$x++; // $x += 1; $x--; // $x -= 1;
Both of these operators can be used either before or after a variable.
$x++; // post-increment $x--; // post-decrement ++$x; // pre-increment --$x; // pre-decrement
The result on the variable is the same whichever is used. The difference is that the post-operator returns the original value before it changes the variable, whereas the pre-operator changes the variable first and then returns the value.
$x = 5; $y = $x++; // $x=6, $y=5 $x = 5; $y = ++$x; // $x=6, $y=6
Comparison Operators
The comparison operators compare two values and return either true or false. They are mainly used to specify conditions, which are expressions that evaluate to either true or false.
$x = (2 == 3); // false // equal to $x = (2 != 3); // true // not equal to $x = (2 <> 3); // true // not equal to (alternative) $x = (2 === 3); // false // identical $x = (2 !== 3); // true // not identical $x = (2 > 3); // false // greater than $x = (2 < 3); // true // less than $x = (2 >= 3); // false // greater than or equal to $x = (2 <= 3); // true // less than or equal to
The strict equality operators, === and !==, are used for comparing both type and value. These are necessary because the regular “equal to” (==) and “not equal to” (!=) operators automatically perform a type conversion before they compare the operands. It is considered good practice to use strict comparison when the type conversion feature of the “equal to” operation is not needed.
$x = (1 == "1"); // true (same value) $x = (1 === "1"); // false (different types)
PHP 7 added a new comparison operator called the spaceship operator (<=>). It compares two values and returns 0 if both values are equal; 1 if the value on the left side is greater; and –1 if the value on the right side is greater.
$x = 1 <=> 1; // 0 (1 == 1) $x = 1 <=> 2; //-1 (1 < 2) $x = 3 <=> 2; // 1 (3 > 2)
Logical Operators
The logical operators are often used together with the comparison operators. Logical and (&&) evaluates to true if both the left and right side are true, and logical or (||) evaluates to true if either the left or right side is true. For inverting a Boolean result, there is the logical not (!) operator. Note that for both “logical and” and the “logical or”, the right side of the operator is not evaluated if the result is already determined by the left side.
$x = (true && false); // false // logical and $x = (true || false); // true // logical or $x = !(true); // false // logical not
Bitwise Operators
The bitwise operators can manipulate binary digits of numbers. For example, the xor operator (^) turn on the bits that are set on one side of the operator, but not on both sides.
$x = 5 & 4; // 101 & 100 = 100 (4) // and $x = 5 | 4; // 101 | 100 = 101 (5) // or $x = 5 ^ 4; // 101 ^ 100 = 001 (1) // xor (exclusive or) $x = 4 << 1; // 100 << 1 =1000 (8) // left shift $x = 4 >> 1; // 100 >> 1 = 10 (2) // right shift $x = ∼4; // ∼00000100 = 11111011 (-5) // invert
These bitwise operators have shorthand assignment operators, just like the arithmetic operators.
$x=5; $x &= 4; // 101 & 100 = 100 (4) // and $x=5; $x |= 4; // 101 | 100 = 101 (5) // or $x=5; $x ^= 4; // 101 ^ 100 = 001 (1) // xor $x=5; $x <<= 1; // 101 << 1 =1010 (10)// left shift $x=5; $x >>= 1; // 101 >> 1 = 10 (2) // right shift
Note that decimal numbers used together with binary operators are automatically converted to binary. The binary notation may also be used to specify binary numbers for the operation.
$x = 0b101 & 0b100; // 0b100 (4)
Operator Precedence
When an expression contains multiple operators, the precedence of those operators decides the order in which they are evaluated. The order of precedence can be seen in Table 3-1.
To give an example, multiplication has greater precedence than addition, and therefore it is evaluated first in the following line of code.
$x = 4 + 3 * 2; // 10
Parentheses can be used to force precedence. An expression placed within parentheses is evaluated before other expressions in that statement.
$x = (4 + 3) * 2; // 14
Additional Logical Operators
In the precedence table, make special note of the last three operators: and, or, and xor . The and and or operators work in the same way as the logical && and || operators. The only difference is their lower level of precedence.
// Same as: $a = (true && false); $x = true && false; // $x is false // Same as: ($a = true) and false; $x = true and false; // $x is true
The xor operator is a Boolean version of the bitwise ^ operator. It evaluates to true if only one of the operands are true.
$x = (true xor true); // false
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2016 Mikael Olsson
About this chapter
Cite this chapter
Olsson, M. (2016). Operators. In: PHP 7 Quick Scripting Reference. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-1922-5_3
Download citation
DOI: https://doi.org/10.1007/978-1-4842-1922-5_3
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-1921-8
Online ISBN: 978-1-4842-1922-5
eBook Packages: Professional and Applied ComputingProfessional and Applied Computing (R0)Apress Access Books