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 -$xand$yadded together-Subtraction -$ytaken away from$x*Multiplication -$xtimes$y/Division -$xdivided by$y%Modulus - What is left over after dividing$xby$y**Exponentiation -$xraised 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 = ymeansxtakes the value ofy, with no shorthand involvedx += ydoes the same job asx = x + yx -= ydoes the same job asx = x - yx *= ydoes the same job asx = x * yx /= ydoes the same job asx = x / yx %= ydoes the same job asx = x % y
PHP Comparison Operators
Comparison operators look at two values, either numbers or strings, and tell you how they relate.
==Equal - Givestruewhen$xand$yhave the same value===Identical - Givestrueonly when$xand$yhave the same value and also the same type!=Not equal - Givestruewhen the values of$xand$ydiffer<>Not equal - Another way of writing!=, with the same result!==Not identical - Givestruewhen$xand$ydiffer in value or in type>Greater than - Givestruewhen$xis larger than$y<Less than - Givestruewhen$xis smaller than$y>=Greater than or equal to - Givestruewhen$xis larger than$yor the two are equal<=Less than or equal to - Givestruewhen$xis smaller than$yor the two are equal<=>Spaceship - Gives an integer: below zero when$xis the smaller value, zero when they are equal, and above zero when$xis 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.
++$xPre-increment - Adds one to$xfirst, then returns the new value$x++Post-increment - Returns the current value of$x, then adds one--$xPre-decrement - Subtracts one from$xfirst, 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 holdor- True when at least one condition holdsxor- True when exactly one condition holds, never both&&- True when both conditions hold (same asand)||- True when at least one condition holds (same asor)!- 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:
Output: PHP script
<?php
$txt1 = "Hello";
$txt2 = " World!";
echo $txt1 . $txt2;
?>
String Concatenation Assignment
Here .= adds more text to the end of $txt1, changing the variable itself:
Output: PHP script
<?php
$txt1 = "Hello";
$txt1 .= " World!";
echo $txt1;
?>
PHP Array Operators
These operators work on whole arrays, mostly to compare one with another.
+Union - Combines$xand$yinto their union==Equality - Givestruewhen both arrays hold the same key/value pairs===Identity - Givestruewhen both arrays hold the same key/value pairs, in the same order and with the same types!=Inequality - Givestruewhen$xand$yaren't equal<>Inequality - Another way of writing!=, with the same result!==Non-identity - Givestruewhen$xand$yaren't identical
PHP Conditional Assignment Operators
Conditional assignment operators choose a value based on a condition.
?:Ternary - In$x = expr1 ? expr2 : expr3,$xreceives expr2 when expr1 is true and expr3 when it is false??Null coalescing - In$x = expr1 ?? expr2,$xreceives expr1 as long as it exists and isn'tNULL; if it is missing orNULL,$xreceives expr2
Ternary Operator
The condition here checks whether $x divides evenly by 2. It does, so the ternary operator picks its first value, "Even":
Output: PHP script
<?php
$x = 10;
echo ($x % 2 == 0) ? "Even" : "Odd";
?>
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":
Output: PHP script
<?php
// variable $color does not exist
$color = $color ?? "red";
echo $color;
?>