HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PHPVariablesData TypesOperatorsStringsArraysAssociative ArraysConditions (if / else)Loops (for / foreach / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
PHP›Operators

Operators

Math (+ - * / % **), comparison (== === < >), logical (&& ||), and concatenation (.) operators.

Simple enough for a 5-year-old to understand

Math and concatenation

Note: In PHP, string concatenation uses a DOT (.) instead of +.

Example code
1
<?php
Start.
DefinitionStart.
2
echo 7 % 2;
Remainder: 1
CodeRemainder: 1
3
echo "\n";
New line.
Output / InputNew line.
4
echo "Alex" . " " . "Lovelace";
Concatenation: Alex Lovelace
Output / InputConcatenation: Alex Lovelace
DefinitionCodeOutput / Input
💡 Hint: "5" + 3 gives 8 in PHP (converts to number) but use . for string concatenation.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of the division of 23 by 4.

🎯 Expected output: 3

Loading editor…

About this topic

PHP operators come from the C family, but have two important differences. First: string concatenation is done with a DOT, not a plus. This is a PHP-specific choice that separates numeric addition from string concatenation — a very useful decision considering PHP's flexible type system.

Second: there are two equality operators. The double equals compares by converting types, while the triple equals also checks the type. In modern PHP code, triple equals is almost always used.

Modern PHP also introduced a few practical operators: the null coalescing operator provides a default if a value doesn't exist, the spaceship operator does a three-way comparison for sorting, and the null-safe arrow operator returns null without throwing an error when an object is null.

What you will learn

  • Arithmetic operators and exponentiation
  • String concatenation with the dot
  • The difference between double and triple equals
  • Logical operators and short-circuit behavior
  • Null coalescing and null-safe operators
  • The ternary conditional operator

When to use?

The null coalescing operator is standard when reading form data: it takes the value if it exists, or the default if it doesn't, and does not produce an undefined key warning. This reduces an isset check to a single line.

The spaceship operator is used in custom sorting functions; it gives the comparison result directly in the format expected by the sorting function.

Common mistakes

Wrong

$first = "Ada";
$last = "Lovelace";
echo $first + " " + $last;

In PHP, string concatenation is done with a dot, not a plus. Plus attempts numeric addition; in PHP 8, since strings cannot be converted to numbers, it throws an "Unsupported operand types: string + string" error.

Correct

$first = "Ada";
$last = "Lovelace";
echo $first . " " . $last;

Wrong

$input = $_GET["page"];
echo $input;

If the key does not exist, you get a "Warning: Undefined array key" warning and the value becomes null. For any incoming data, it is necessary to provide a default using the null coalescing operator.

Correct

$input = $_GET["page"] ?? "1";
echo $input;

Wrong

$a = 1;
$b = "1";
if ($a == $b) {
    echo "same";
}

Double equals converts types, so the number 1 and the string "1" are considered equal. This is often an undesired behavior and silently bypasses validation logic — creating a serious flaw in security checks.

Correct

$a = 1;
$b = "1";
if ($a === $b) {
    echo "same";
}

Frequently asked questions

Why is a dot used for string concatenation?

Because PHP is dynamically typed and the plus sign would be ambiguous: should "5" and "3" be added or concatenated? Dot concatenation removes this ambiguity. JavaScript doesn't make this distinction, which is why addition and concatenation get mixed up there.

Is the null coalescing operator different from isset?

It does the same logic in a single line: if the left side is undefined or null, it returns the right-hand value and produces no warnings at all. It also works chained, so you can try multiple sources in sequence. It is the standard way to read form and configuration data.

What is the difference between and and the double ampersand?

Both perform a logical AND operation, but their PRECEDENCE is different: and has lower precedence than the assignment operator, so when used inside an assignment, it gives unexpected results. Always use the symbol version.

Summary

In PHP, strings are concatenated with a dot, and comparisons always use triple equals. The null coalescing operator is the standard way to provide a default for incoming data.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code