PHP Operators

Operators are the programming language symbols we use to manipulate data, such as the plus sign (+) for numeric addition and the equal sign (=) for assigning a value to a variable. Operands are the values (literals, variables, or complex expressions) that an operator works on.

PHP Operators

PHP has different operators for working with different types of data.

Types of PHP Operators

  1. Assignment operators
  2. Arithmetic operators
  3. Combined operators
  4. Comparison operators
  5. Logical operators
  6. Ternary operators

1. Assignment Operators

The assignment operator assigns a value to a variable. The basic assignment operator in PHP is "=". This means that the operand to the left of "=" gets set to the value to the right of "=".

$x = 1; // assignment

2. Arithmetic Operators

The arithmetic operators include the four basic arithmetic operations - Addition (+), subtraction (-), multiplication (*), and division (/) and the modulus operator (%) that gives the remainder of the division of two operands. These are all binary operators, meaning they require two operands.

$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)

Exponentiation operator (**) raises the first operand to the power of the second. Negation (-) negates the operand.

$x = 4 ** 2; // 16 exponentiation

Order of Precedence

As in mathematics, these operators have an order of precedence that controls the way an expression is evaluated when it contains multiple operations. The arithmetic operators for multiplication, division, and modulo have higher precedence,
while the operators for addition and subtraction have lower precedence.

If other considerations are equal, PHP goes from left to right. You can change the order in which the arithmetic is performed by using parentheses.

3. Combined Arithmetic 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.

For example, you have a $total variable for keeping track of the total cost of the items in an online shopping cart. Every time the user adds or removes an item from the cart, you want to change $total by the cost of that item.

$total = $total + 25;

With combined arithmetic assignment operators, you can accomplish the same task by using more concise syntax:

$total += 25;

You can combine an arithmetic operator with the assignment operator to form a combined operator. Combined operators are:

  1. +=
  2. -=
  3. *=
  4. /=
  5. %=
$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;

Increment and Decrement Operators

Numbers can also be incremented (increased by 1) or decremented (decreased by 1) using special operators: a double plus sign (++) for incrementing and a double minus sign (--) for decrementing.

  • ++: This operator on the left of the variable will increase the variable by 1, and then return the result. On the right, it will return the content of the variable, and after that increase it by 1.
  • --: This operator works the same as ++ but decreases the value by 1 instead of increasing by 1.
$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

4. 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.

There are four comparisons that are very intuitive: < (less than), <= (less or equal to), > (greater than), and >= (greater than or equal to). There is also the special operator <=> (spaceship) that compares both the operands and returns an integer instead of a Boolean.

  1. == (equal to)
  2. != (not equal to)
  3. === (identical)
  4. !== (not identical)
  5. > (greater than)
  6. >= (greater than or equal to)
  7. < (less than)
  8. <= (less than or equal to)
  9. <=> (spaceship)

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. For example,

$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)

5. Logical Operators

Logical operators manipulate or combine Boolean expressions, producing a single true or false value. The logical operators are often used together with the comparison operators.

The and operator returns true if both the operands evaluate to true. The or operator returns true if any or both of the operands are true. The not operator returns the negated value of the operand, that is, true if the operand is false or false if the operand is true.

  1. || (or)
  2. && (and)
  3. ! (not)
  4. xor

For example,

$x = (true && false); // false // logical and
$x = (true || false); // true // logical or
$x = !(true); // false // logical not

6. Ternary Operators

  1. Shorthand for If-Else - ?: