Exercise 3 of 10 · Arithmetic
Ticket Total
What you will make
A small receipt that multiplies a ticket price by a quantity and prints the total cost.
The one new idea: arithmetic operators (+ - * /) and joining text with .
Almost every useful program computes something before it has anything worth printing. Combining numbers, variables and text in one line is the next building block after storing a single value.
Go straight to the code ↓Arithmetic and joining text
PHP does ordinary arithmetic with four operators: + for addition, - for subtraction, * for multiplication, and / for division. They work the way you'd expect on numbers stored in variables, not just on numbers typed directly into the code, and the result of a calculation can be stored in a variable of its own before it's used anywhere.
Text is different. To join a piece of text to something else — including a number — PHP uses the dot operator, ., not +. "Total: " . $total builds one string out of two pieces; "Total: " + $total is a type error, because + in PHP is reserved strictly for arithmetic.
A worked example
Here's a different calculation, so you can see the shape of it before touching today's exercise:
<?php
$length = 5;
$width = 4;
$area = $length * $width;
echo "The room is " . $area . " square meters.\n";Running it prints:
The room is 20 square meters.$area is computed first, on its own line, and only then joined into a sentence with .. Keeping the calculation and the printing as separate steps makes both of them easier to read — and easier to fix, if one of them turns out to be wrong.
Your turn
The starter program is supposed to multiply a ticket price by a quantity to get a total, then print it. Right now the total comes out too small, because the line that computes it uses the wrong operator.
Read the comment above the calculation — it says what the total is supposed to represent. Compare that to the operator actually used in the line below it, pick the one that matches "multiply," and press Run.
If something goes wrong
If the total is way off, check which operator is doing the calculating — + and * are easy to swap without noticing, especially when you're moving quickly, but PHP has no idea which one you meant; it only does exactly what's written. If PHP reports a type error mentioning "unsupported operand types," look for a + where a . was needed to join a piece of text and a number together instead of adding them.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Reaching for + when the operation is really multiplication
- In everyday language 'times' and 'plus' can blur together when you're moving fast, but PHP has no idea what you meant — it just adds the two numbers with +. Read what the total is supposed to represent, then pick the matching operator.
- Trying to join text and a number with +
- In PHP, + is only for arithmetic. Joining text together uses the dot, . — "Total: " + $total causes a TypeError, while "Total: " . $total works.
- Forgetting that / can return a decimal
- 10 / 3 in PHP gives 3.3333333333333, not a whole number. Plain / doesn't round anything — getting a whole number back needs a separate step.
Want a blank editor instead? Open the PHP playground.