▶ TrainingGet Pro
All tools & lessonsAll toolsAllGet the appApp
Learning departments Music, Math, Cookery, Programming and Art are open – explore →

PHP lesson 7: Comparisons: == and ===

US version. US grades and US spelling. · UK version of this lesson

A comparison gives a bool: true or false. 5 > 3 is true. echo is a poor way to look at a bool: true prints 1 and false prints NOTHING. Use var_dump(), which shows bool(true) or bool(false). A complete 40-minute PHP lesson (Grades 9–12), from Unit 3: Decisions. It runs right here in your browser – nothing to install.

Time
40 minutes + 20 min homework
Grade
Grades 9–12 (ages 14–18)
You need
A computer or tablet, paper and a pencil
Graded
Instantly, with explanations

For parents: how to use this lesson

You don't need to know PHP yourself. The lesson explains each step on screen, the way Mr Babbage, our Head of Computing, likes it: a worked example first, then a go for your child. Your child predicts what code prints, traces it and puts it in order – graded straight away – and then writes and fixes real PHP in the practice test, where it runs in the browser.

  1. Read what your child will learn together.
  2. Press Start the lesson below. Sit with your child for the teaching and the worked example (about 16 minutes): ask them to say what each line does.
  3. Let them do the practice on their own, then the write-it and fix-it questions in the practice test. When code goes wrong, PHP's message is a clue – let them read it out and try again before you help.
  4. Finish with the talk question: When is == risky? Give an example where === is safer.

Watch out for

  • echo of false prints 0. It prints nothing; var_dump() shows bool(false).
  • == and === always agree. Try var_dump("5" == 5, "5" === 5);.

Miss Lovelace's tip for home: ask your child to explain one line of their program to you in their own words. If they can explain it, they understand it – and every message from PHP is a clue, not a failure.

What your child will learn

Learning goal: compare values with == != < > <= >=.

Success looks like

  • I can compare values with == != < > <= >=.
  • I can explain the difference between == and ===.
  • I can see true and false with var_dump().

Key words

comparison · boolean · loose comparison · strict comparison · type

What to have ready

  • A device with a web browser (PHP runs in the browser – nothing to install)
  • Paper for tracing
  • A keyboard helps for typing code (a tablet or phone works too)

The big ideas (for the adult)

A comparison gives a bool: true or false. 5 > 3 is true. echo is a poor way to look at a bool: true prints 1 and false prints NOTHING. Use var_dump(), which shows bool(true) or bool(false).

== compares VALUES after converting: "5" == 5 is true. === also checks the TYPE: "5" === 5 is false, because one is a string and one is an int. Many programmers use === by default to avoid surprises.

Text from a web form is ALWAYS a string, even when someone types a number. So $_GET["age"] === 15 is false even for 15 – a classic PHP bug.

Worked example – see it

<?php
var_dump(2 == 2);

It prints:

bool(true)

The lesson, step by step

40 minutes in 5 parts
PartTimeHow it is checked
Starter5 minGraded by Classfern
Teaching input8 minRead, watch and talk
See it, then predict8 minGraded by Classfern
Put it in order, trace it, check it14 minGraded by Classfern
Closure and exit ticket5 minGraded by Classfern + self-check

The lesson

It runs here on the page. No sign-up and no names – progress is kept only in this browser. Each visit picks a new mix of questions.

Homework

20 minutes: 5 questions – predict what code prints, trace it or put it in order, and check the key ideas – then a short reflection. New questions every time, all graded instantly.

Write it and fix it: practice tests

The practice tests run real PHP in your browser: write and fix short programs, graded against hidden tests.

All PHP practice tests · All 24 PHP lessons

Curriculum

CSTA K–12 Computer Science Standards (2017): 3A-AP-15 – Justify choice of control structures (implementation, readability, performance); 2-AP-12 – Design and iteratively develop programs with control structures incl. nested loops and compound conditionals.

The standard codes are CSTA's; the descriptions are our own words.

Our lessons and questions are our own. They are not written or endorsed by any government department, testing agency or exam board. Classfern is not affiliated with or endorsed by The PHP Group. PHP runs here as php-wasm, under its open-source licences.