Codeskill

Learn to code, step by step

Understanding PHP Operators: Logic in Action

PHP operators. The symbols that compare values, do maths, and drive your if-statements and logic.

What are operators in PHP?

Operators are symbols that tell PHP what to do with variables and values – add them, compare them, combine conditions, and so on.

Types of operators in PHP

PHP has a fair few. Here are the main groups:

Arithmetic operators

Standard maths:

  • Addition (+): adds two values.
  • Subtraction (-): subtracts one value from another.
  • Multiplication (*): multiplies two values.
  • Division (/): divides one value by another.
  • Modulus (%): returns the remainder after division.
<?php
echo 5 + 3; // Outputs 8
echo 5 - 3; // Outputs 2
echo 5 * 3; // Outputs 15
echo 5 / 3; // Outputs 1.6667
echo 5 % 3; // Outputs 2
?>

Assignment operators

Put values into variables:

  • Basic assignment (=): assigns a value to a variable.
  • Combined operators (+=, -=, *=, /=, %=): do an operation and assign the result.
<?php
$x = 10;
$x += 5; // $x is now 15
?>

Comparison operators

Compare two values:

  • Equal (==): true if values are equal.
  • Identical (===): true if values and types are equal.
  • Not equal (!= or <>): true if values are not equal.
  • Not identical (!==): true if values or types are not equal.
  • Greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=).
<?php
var_dump(5 == "5"); // bool(true)
var_dump(5 === "5"); // bool(false)
?>

Increment/decrement operators

Bump a variable up or down by one:

  • Increment (++): increases a variable’s value by 1.
  • Decrement (--): decreases a variable’s value by 1.
<?php
$x = 10;
$x++;
echo $x; // Outputs 11
?>

Logical operators

Combine conditional statements:

  • And (&& or and): true if both operands are true.
  • Or (|| or or): true if either operand is true.
  • Not (!): true if the operand is false.
<?php
$age = 20;
$hasPermission = true;

if ($age >= 18 && $hasPermission) {
    echo "Access granted.";
} else {
    echo "Access denied.";
}
?>

String operators

Work with strings:

  • Concatenation (.): appends one string to another.
  • Concatenation assignment (.=): appends and assigns the result.
<?php
$text = "Hello, ";
$text .= "world!";
echo $text; // Outputs 'Hello, world!'
?>

Array operators

Compare and combine arrays:

  • Union (+): unites two arrays.
  • Equality (==), inequality (!= or <>), identity (===), non-identity (!==).
<?php
$array1 = array("a" => "apple", "b" => "banana");
$array2 = array("b" => "pear", "a" => "apple");
$result = $array1 + $array2;
print_r($result); // Union of $array1 and $array2
?>

Ternary and null coalescing operators

  • Ternary (?:): shorthand for a simple if-else.
  • Null coalescing (??): returns the first operand if it exists and is not null; otherwise the second.
<?php
$user = $_GET['user'] ?? 'nobody';
echo $user;
?>

Practical example: a simple calculator

A small function that puts a few operators to work:

<?php
function calculate($num1, $num2, $operation) {
    switch ($operation) {
        case '+':
            return $num1 + $num2;
        case '-':
            return $num1 - $num2;
        case '*':
            return $num1 * $num2;
        case '/':
            return $num2 != 0 ? $num1 / $num2 : 'Division by zero error';
        default:
            return "Invalid operation";
    }
}

echo calculate(10, 5, '+'); // Outputs 15
echo calculate(10, 5, '/'); // Outputs 2
?>

Operators turn up everywhere – calculations, comparisons, string building, permission checks. Worth knowing the groups above so you’re not guessing at syntax mid-script.

PreviousWorking with Strings in PHP: Manipulation and Functions