Skip to content

Operators and Expressions ​

A calculator has buttons for adding, dividing, and comparing. Operators are PHP's buttons.

An operator is a symbol that does something with values, like + or >. An expression is any piece of code that produces a value: 5, $price, and $price * 2 are all expressions. Operators combine small expressions into bigger ones.

Arithmetic ​

OperatorDoesExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
%remainder7 % 21
**power7 ** 249

The remainder % is more useful than it looks. $n % 2 is 0 for even numbers and 1 for odd ones.

Which goes first? ​

Just like in math class, multiplication and division happen before addition and subtraction. Use parentheses to change the order, or just to make it clearer:

php
<?php
echo 2 + 3 * 4, "\n";
echo (2 + 3) * 4, "\n";
echo 2 ** 3 ** 2, "\n";
text
14
20
512

The last one is a curiosity: ** works from right to left, so it's 2 ** 9, not 8 ** 2. When in doubt, add parentheses.

Comparing values ​

Comparison operators give a boolean, true or false:

OperatorMeans
===equal, and the same type
!==not equal, or not the same type
==equal after converting types
!=not equal after converting types
<, >less than, greater than
<=, >=less than or equal, greater than or equal

Notice there are two kinds of "equal". === (three equal signs) is strict: the values must match and be the same type. == (two) is loose: PHP converts the values to a common type first.

php
<?php
var_dump(5 === 5);
var_dump(5 === "5");
var_dump(5 == "5");
text
bool(true)
bool(false)
bool(true)

The number 5 and the string "5" aren't the same type, so === says false, but == converts and says true. Loose comparison has some real surprises, which you'll see in Type Juggling and Strict Comparisons. Until then, a simple rule: use === and !==.

And don't mix up = and ===. One equal sign stores a value; three compare two values.

The spaceship operator ​

<=> compares two values and gives -1 if the left is smaller, 0 if they're equal, and 1 if the left is bigger. It's called the spaceship operator, because <=> looks a bit like one. You'll use it for sorting in Array Functions.

php
<?php
echo 3 <=> 8, "\n";
echo 8 <=> 8, "\n";
echo 9 <=> 8, "\n";
text
-1
0
1

Logical operators: and, or, not ​

To combine true/false values:

OperatorNameTrue when
&&andboth sides are true
||orat least one side is true
!notthe value is false (it flips it)
php
<?php
$age = 15;
$hasTicket = true;

var_dump($age >= 13 && $hasTicket);
var_dump($age >= 18 || $hasTicket);
var_dump(!$hasTicket);
text
bool(true)
bool(true)
bool(false)

PHP also has the words and and or, but they behave differently from && and || in some situations, which leads to confusing bugs. Stick with && and ||.

The ternary operator ​

The ternary operator picks one of two values based on a condition. It reads: condition ? value if true : value if false.

php
<?php
$score = 72;
$result = $score >= 75 ? "passed" : "try again";
echo "You $result.\n";
text
You try again.

It's perfect for small either/or choices. For anything more complicated, use if, from Making Decisions.

The null coalescing operator ?? ​

Often you want "this value, or a default if it's missing." ?? does exactly that. It gives the left side if that exists and isn't null; otherwise, the right side:

php
<?php
$nickname = null;
echo $nickname ?? "Guest", "\n";

$nickname = "Mimi";
echo $nickname ?? "Guest", "\n";

echo $undefinedThing ?? "No warning here", "\n";
text
Guest
Mimi
No warning here

The last line shows ??'s superpower: it doesn't warn when the variable doesn't exist at all. That makes it ideal for form data, which may or may not have been sent, as you'll see in Handling Forms.

Try it ​

A cinema works out ticket prices. Predict each line.

Running PHP here

Running PHP right in the browser is coming to Syntaxia soon. For now, you can read and edit the code here. If you have PHP installed on your computer (see Setting Up), save it as index.php and run it with php index.php.

Check your prediction
text
Price: 200
Code used: none
Even-numbered seat? no
bool(false)
bool(false)
0

$age < 13 is true, so the discount is 50 and the price is 200. $price === "200" is false because one side is an int and the other a string. !$isStudent is false, so the && is false, and 200 <=> 200 is 0.

Try it yourself ​

  1. Change $age to 20 and $isStudent to false. What's the price now?
  2. Give $promoCode a value, like "SUMMER". What does the Code used line print?
  3. Change === to == in the var_dump($price === "200") line. What changes, and why?

Check your understanding ​

What does 10 - 2 * 3 give?
What does 7 === "7" give?
What does $name ?? "Guest" give when $name is null?

Up next ​

You've combined strings with the dot. PHP has dozens of functions for working with text, too. Explore them in Working with Strings.

← Data Types
Working with Strings →

Answer the quiz above correctly to continue.