Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 06

Operators in PHP (All Types with Output)

In this lesson: PHP operators with examples and output — arithmetic, assignment, comparison (== vs ===), logical, string and spaceship.

What is an operator

An operator is a symbol that performs an action on values — like + to add, > to compare, or . to join text. The values an operator works on are called operands. PHP groups operators into arithmetic, assignment, comparison, logical and string operators.

💡 In $total = 5 + 3, the + and = are operators, while 5 and 3 are operands.

Arithmetic operators

These do maths. The one beginners forget is % (remainder).

<?php
  echo 10 + 3;   // 13
  echo 10 - 3;   // 7
  echo 10 * 3;   // 30
  echo 10 / 3;   // 3.333...
  echo 10 % 3;   // 1  (remainder)
?>
13 7 30 3.3333333333333 1

To join text, PHP uses the dot: "Hello " . "World" gives "Hello World". Do not use + for strings in PHP.

Assignment operators

= stores a value. The shortcuts combine maths with assignment.

<?php
  $x = 10;
  $x += 5;   // same as $x = $x + 5  -> 15
  $x -= 3;   // 12
  $x *= 2;   // 24
  echo $x;
?>
24

These shortcuts (+=, -=, *=, /=) make code shorter and are used a lot inside loops.

Comparison operators

OperatorMeaningExample (true?)
>greater than10 > 3 → true
<less than3 < 10 → true
>=greater or equal5 >= 5 → true
!=not equal5 != 3 → true
==equal (value)"5" == 5 → true
===equal (value + type)"5" === 5 → false

Comparison operators return true or false, which is exactly what conditions need.

Logical operators

These combine true/false conditions.

<?php
  $age = 20;
  $hasID = true;
  if ($age >= 18 && $hasID) {
      echo "Allowed";
  }
?>
Allowed

&& (AND) needs both true; || (OR) needs at least one true; ! (NOT) flips true to false.

== vs === (important)

This is the most important operator detail in PHP. == checks value only; === checks value and type.

<?php
  var_dump("5" == 5);    // true  (types ignored)
  var_dump("5" === 5);   // false (string vs int)
?>
bool(true)
bool(false)

Because == can give surprising results with mixed types, prefer === whenever you can, especially when comparing form input.

Common mistakes

  • Using + to join strings. PHP joins with .; + tries to add numbers.
  • Using == when you need ===. Loose comparison can make "0" or "" behave unexpectedly.
  • Confusing = and ==. One assigns, the other compares.
  • Forgetting operator precedence. Use brackets to make the order clear: ($a + $b) * $c.

Summary

  • Operators act on values: arithmetic, assignment, comparison, logical, string.
  • Join strings with . and get remainder with %.
  • Comparisons return true/false for use in conditions.
  • &&, ||, ! combine conditions.
  • Prefer === (value + type) over == to avoid surprises.

Next, use these operators inside if-else decisions, or repeat with loops in PHP.

Frequently Asked Questions

What is the difference between == and === in PHP?
== compares only the values and ignores type, so "5" == 5 is true. === compares value AND type, so "5" === 5 is false because one is text and the other is a number. Prefer === to avoid surprises.
How do I join two strings in PHP?
Use the dot operator . — for example "Hello " . "World" gives "Hello World". PHP uses a dot for joining strings, not the plus sign like some other languages.
What does the % operator do?
The modulus operator % gives the remainder after division. 10 % 3 is 1. It is very useful to check even/odd ($n % 2 == 0 means even) or to repeat things in cycles.
What is the difference between ++ before and after a variable?
$i++ (post) uses the current value first, then adds 1. ++$i (pre) adds 1 first, then uses it. The final value of $i is the same; only the value used in that expression differs.
What do && and || mean?
&& is AND — true only if both sides are true. || is OR — true if at least one side is true. They combine conditions, like $age >= 18 && $hasID.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicIf-Else and Switch Your progress: 6 / 50 (12%) · Lesson 7 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free