๐Ÿ“ Web Development

PHP Operators: Arithmetic, Comparison and Logical

Aug 16, 20265 min read10 viewsBy Flow

PHP operators are the symbols that do the work. In the last lesson you learned to store data. Now you will learn to use it. Every total, every discount, every "is this true" check runs on PHP operators.

What a PHP operator is

An operator takes one or two values and makes a new one. That is all it does.

<?php
echo $total = 10 + 5;
?>

The + is an operator. The 10 and 5 are its operands. The answer is 15.

Then a second operator, =, stores that answer in $total. So one short line uses two operators.

Output

PHP operators example output showing 15 from adding 10 and 5

Arithmetic PHP operators

These do maths. You know most of them already.

<?php
$a = 17;
$b = 5;

echo $a + $b;
echo "<br>";
echo $a - $b;
echo "<br>";
echo $a * $b;
echo "<br>";
echo $a / $b;
echo "<br>";
echo $a % $b;
echo "<br>";
echo $a ** 2;
?>

Output

Arithmetic PHP operators output: 22, 12, 85, 3.4, 2 and 289

Two of these need a closer look.

Division can give a decimal. 17 / 5 is 3.4. It is not 3. PHP does not round for you. For a whole number, use intdiv(17, 5).

Modulus gives the remainder. The % sign divides, throws the answer away, and keeps what is left. So 17 % 5 is 2. Five goes into 17 three times, and 2 is left over.

That sounds useless. It is not. Here is the trick you will use most.

<?php
$number = 8;

echo $number % 2;
?>

Output

PHP modulus operator output showing 0, meaning the number is even

A zero remainder means the number divided cleanly. So the number is even. You will use this to stripe table rows too.

The dot: joining text together

This one gets its own section, because it confuses people and nobody explains it properly.

In PHP the dot . joins two pieces of text into one. Its proper name is the concatenation operator.

<?php
echo "Hello" . " world";
?>

That prints Hello world. The dot does not add anything up. It sticks things end to end. Think of it as glue.

Look at the space inside " world". PHP joins exactly what you give it, and nothing more. It never adds a space for you. Remove that space and you get Helloworld.

You will use this constantly, because most output is built from parts.

<?php
$firstName = "Ada";
$lastName  = "Lovelace";

echo $firstName . " " . $lastName;
?>

Output

PHP dot operator joining first and last name into Ada Lovelace

Read that as three pieces glued together. First the name. Then a space. Then the surname.

Never use + to join text. In some languages that works. In PHP it does not. The plus sign is only for numbers. Give it two strings and PHP tries to read them as numbers, and you get an error.

The dot is the only way to join. Learn it now and save yourself a confusing afternoon later.

Assignment PHP operators

You already know =. It stores a value.

The rest are shortcuts. Each one does a calculation, then stores the result back into the same variable.

<?php
$price = 100;

$price += 50;
echo $price;
echo "<br>";

$price -= 30;
echo $price;
echo "<br>";

$price *= 2;
echo $price;
?>

Output

PHP assignment operators output showing 150, 120 and 240

Now the important part. These are only shorthand. Each one expands into a longer line you already understand.

<?php
$price += 50;

// means exactly the same as:
$price = $price + 50;
?>

Read the long version from the right. Take what is in $price. Add 50. Put the answer back into $price.

The same idea works with the dot. .= is the concatenation operator joined to an assignment.

<?php
$message = "Hello";
$message .= " world";
$message .= "!";

echo $message;
?>

Output

PHP concatenation assignment operator output showing Hello world!

Those three lines are just this, written the long way:

<?php
$message = "Hello";
$message = $message . " world";
$message = $message . "!";

echo $message;
?>

Both versions print the same thing. Each line takes what is already inside $message, glues something on the end, and stores it back.

So .= means "add this to the end of what is already there". You will meet it everywhere people build HTML inside a loop.

Comparison PHP operators

These ask a question. The answer is always true or false.

<?php
$x = 10;
$y = "10";

var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x !== $y);
var_dump($x > 5);
var_dump($x <= 9);
?>

Output

Comparison PHP operators output showing loose == true and strict === false

Look at the first two lines. Same values, different answers.

$x is the number 10. $y is the text "10". They look alike. They are not the same type.

== is loose. It converts one value first, then compares. It says they are equal.

=== is strict. It checks the value and the type. It says they are not equal.

Use === by default. This single habit will save you hours. Data from forms, URLs and databases always arrives as text, even when it looks like a number. Strict comparison stops PHP guessing for you.

There is more to it than one rule โ€” the spaceship operator, how arrays compare, and a change PHP 8 made that most tutorials still get wrong. It is all in PHP comparison operators: ==, === and the type trap.

Logical PHP operators

These join several true or false answers into one.

<?php
$age = 22;
$hasTicket = true;

var_dump($age >= 18 && $hasTicket);
var_dump($age < 18 || $hasTicket);
var_dump(!$hasTicket);
?>

Output

Logical PHP operators output showing true, true and false

&& needs both sides true. || needs only one. ! flips the answer.

PHP also gives you word forms for two of these โ€” and and or, plus xor, which has no symbol at all. They look interchangeable with && and || and they are not, because they rank below =. See PHP logical operators: and, or, && and || explained.

PHP is lazy here, in a useful way. With &&, if the left side is false, it never checks the right side at all. The answer cannot change, so it does not bother.

Operator precedence

What if one line has several operators? PHP does not work left to right. It follows an order, just like school maths.

<?php
echo 2 + 3 * 4;
echo "<br>";
echo (2 + 3) * 4;
?>

Output

PHP operator precedence output showing 14 and 20

Multiplication runs before addition. Brackets beat everything.

Add brackets whenever the order is not obvious โ€” they cost nothing. When you do want the whole table, and the three rows that cause almost every real bug, read PHP operator precedence: what runs first and why.

Three mistakes with PHP operators

Writing = when you meant ==. One equals sign stores. Two compare. So if ($status = "active") checks nothing. It overwrites $status, and it is always true.

Trusting == with mixed types. Use ===. Then PHP never guesses.

Joining text with +. Use the dot. Plus is only for numbers.

Common questions about PHP operators

The operator questions that send people searching most often.

What is the difference between == and === in PHP?

== is loose: it converts types before comparing, so the number 10 and the text "10" come out equal. === is strict: it checks value and type, so they do not. Use === by default โ€” form and database data always arrives as text.

Why can I not use + to join two strings in PHP?

PHP uses the dot for joining text. The plus sign is only for numbers, so giving it two strings makes PHP try to read them as numbers and fail. Use "Hello" . " world".

What does the % operator do in PHP?

It is modulus โ€” it divides and keeps the remainder. 17 % 5 is 2. A remainder of 0 means the number divided cleanly, which is how you check whether a number is even.

What is the difference between = and == in PHP?

One equals sign assigns a value. Two compare. Writing if ($status = "active") does not test anything โ€” it overwrites $status and is always true.

What is next

Those are the PHP operators you will reach for every day. You can now store data and work with it. Next your program needs to make choices.

Carry on with PHP if else: conditional statements. Need a refresher first? Go back to PHP basics: syntax, variables and data types.

Comments

Loading comments...

Link copied to clipboard