Skip to main content

PHP Operators

An operator is a special symbol that carries out an action on variables and values, such as adding two numbers or comparing them.

PHP's operators fall into these groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic Operators

These are the operators you know from math class. They work on numeric values and handle everyday calculations like adding, subtracting and multiplying.

The list below shows each one and what it produces:

  • + Addition - $x and $y added together
  • - Subtraction - $y taken away from $x
  • * Multiplication - $x times $y
  • / Division - $x divided by $y
  • % Modulus - What is left over after dividing $x by $y
  • ** Exponentiation - $x raised to the power of $y

PHP Assignment Operators

An assignment operator writes a value into a variable, and the ones below are used with numeric values.

The simplest is =, which gives the variable on its left the value of the expression on its right. The others are shorthand for doing a calculation and storing the result in one step:

  • x = y means x takes the value of y, with no shorthand involved
  • x += y does the same job as x = x + y
  • x -= y does the same job as x = x - y
  • x *= y does the same job as x = x * y
  • x /= y does the same job as x = x / y
  • x %= y does the same job as x = x % y

PHP Comparison Operators

Comparison operators look at two values, either numbers or strings, and tell you how they relate.

  • == Equal - Gives true when $x and $y have the same value
  • === Identical - Gives true only when $x and $y have the same value and also the same type
  • != Not equal - Gives true when the values of $x and $y differ
  • <> Not equal - Another way of writing !=, with the same result
  • !== Not identical - Gives true when $x and $y differ in value or in type
  • > Greater than - Gives true when $x is larger than $y
  • < Less than - Gives true when $x is smaller than $y
  • >= Greater than or equal to - Gives true when $x is larger than $y or the two are equal
  • <= Less than or equal to - Gives true when $x is smaller than $y or the two are equal
  • <=> Spaceship - Gives an integer: below zero when $x is the smaller value, zero when they are equal, and above zero when $x is the larger value

PHP Increment / Decrement Operators

Increment operators raise a variable's value by one, and decrement operators lower it by one. Where the operator sits matters: in front of the variable, the change happens before the value is returned; behind it, the change happens afterwards.

  • ++$x Pre-increment - Adds one to $x first, then returns the new value
  • $x++ Post-increment - Returns the current value of $x, then adds one
  • --$x Pre-decrement - Subtracts one from $x first, then returns the new value
  • $x-- Post-decrement - Returns the current value of $x, then subtracts one

PHP Logical Operators

Logical operators let you join conditions together into a single test.

  • and - True when both conditions hold
  • or - True when at least one condition holds
  • xor - True when exactly one condition holds, never both
  • && - True when both conditions hold (same as and)
  • || - True when at least one condition holds (same as or)
  • ! - True when the condition is false

PHP String Operators

Two operators exist just for working with strings.

  • . Concatenation - Joins two strings into one
  • .= Concatenation assignment - Adds one string onto the end of another

String Concatenation

Here the . operator joins $txt1 and $txt2, and echo prints the combined text:

Prompt
Write a PHP script that creates two string variables and uses the dot operator to concatenate them, then prints the result.
Output: PHP script

String Concatenation Assignment

Here .= adds more text to the end of $txt1, changing the variable itself:

Prompt
Write a PHP script that creates a string variable, then uses the .= operator to append another string to it, and prints the result.
Output: PHP script

PHP Array Operators

These operators work on whole arrays, mostly to compare one with another.

  • + Union - Combines $x and $y into their union
  • == Equality - Gives true when both arrays hold the same key/value pairs
  • === Identity - Gives true when both arrays hold the same key/value pairs, in the same order and with the same types
  • != Inequality - Gives true when $x and $y aren't equal
  • <> Inequality - Another way of writing !=, with the same result
  • !== Non-identity - Gives true when $x and $y aren't identical

PHP Conditional Assignment Operators

Conditional assignment operators choose a value based on a condition.

  • ?: Ternary - In $x = expr1 ? expr2 : expr3, $x receives expr2 when expr1 is true and expr3 when it is false
  • ?? Null coalescing - In $x = expr1 ?? expr2, $x receives expr1 as long as it exists and isn't NULL; if it is missing or NULL, $x receives expr2

Ternary Operator

The condition here checks whether $x divides evenly by 2. It does, so the ternary operator picks its first value, "Even":

Prompt
Write a PHP script that uses the ternary operator to check if a variable is even or odd, then prints the result.
Output: PHP script

Null Coalescing Operator

?? hands back its first operand when that operand exists and isn't NULL, and its second operand otherwise. Since $color doesn't exist here, the result falls back to "red":

Prompt
Write a PHP script that uses the null coalescing operator to set a default value for a variable that does not exist, then prints the result.
Output: PHP script