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

PHP lesson 4: Arithmetic: / % ** and intdiv()

UK version. UK school years and UK spelling. · US version of this lesson

PHP does maths like you do: *, / and % before + and -, and brackets first. ** means "to the power of": 2 ** 3 is 8. A complete 40-minute PHP lesson (Years 10–11, KS4), from Unit 2: Numbers and strings. It runs right here in your browser – nothing to install.

Time
40 minutes + 20 min homework
Year
Years 10–11, KS4 (ages 14–18)
You need
A computer or tablet, paper and a pencil
Marked
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 – marked 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: How could you use % to test whether a number is even?

Watch out for

  • 7 / 2 is 3. In PHP / gives 3.5 – use intdiv() for 3.
  • % means percent. % gives the remainder after dividing.

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

We are learning to use + - * / % ** and know which goes first.

Success looks like

  • I can use + - * / % ** and know which goes first.
  • I can predict when / gives a decimal.
  • I can use intdiv() and % for whole-number division.

Key words

operator · order of operations · remainder · modulus · intdiv · power

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)

PHP does maths like you do: *, / and % before + and -, and brackets first. ** means "to the power of": 2 ** 3 is 8.

Division / gives the exact answer: 7 / 2 is 3.5. When it divides exactly, you get a whole number: 8 / 2 is 4.

intdiv(7, 2) gives the whole-number part, 3, and 7 % 2 gives the remainder, 1. Together they answer "how many groups, and how many left over?" Dividing by zero is a Fatal error (DivisionByZeroError).

Worked example – see it

<?php
echo (4 + 5) * 5;

It prints:

45

The lesson, step by step

40 minutes in 5 parts
PartTimeHow it is checked
Starter5 minMarked by Classfern
Teaching input8 minRead, watch and talk
See it, then predict8 minMarked by Classfern
Put it in order, trace it, check it14 minMarked by Classfern
Plenary and exit ticket5 minMarked 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 marked instantly.

Write it and fix it: practice tests

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

All PHP practice tests · All 24 PHP lessons

Curriculum

National curriculum in England, computing, key stage 4: pupils should be taught to develop and apply their analytic, problem-solving, design, and computational thinking skills.

Contains public sector information licensed under the Open Government Licence v3.0.

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.