php · level 1 · unit 3 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Quotes turn a number into text.
Every value has a . Text in quotes is a string. A whole number with no quotes, like 12, is an . Put quotes round it, "12", and it becomes a string that happens to hold digits.
The dot always joins, so "5" . "3" gives 53. The plus sign always does maths, so 5 + 3 gives 8. PHP even does maths with "5" + "3", as long as the text looks like a number.
gettype is a : a ready-made command that comes with PHP. Put a value in its brackets and it hands back the name of its type.
<?php
$eggs = 12;
$label = "12";
echo gettype($eggs) . "\n";
echo gettype($label) . "\n"; Text that does not look like a number, like "cat" + 1, stops PHP with a TypeError.
guess first
One number is in quotes. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<?php
$cookies = "4";
$baked = 2;
$total = $cookies + $baked;
echo $total . "\n";
your turn
Your turn. Count the fruit in your bowl.
Type how many apples and how many pears you have, as integers with no quotes. Run it and check that gettype says integer.
fix it
These cookies will not add up. Can you fix them?
- Run it. Four cookies and two more makes 42? Look at the sign on line 4.
- The dot joins, so 4 and 2 get stuck together. You want maths instead.
- Change the
.on line 4 to+, then press run.
nailed it
you can tell text and numbers apart. :D
- Quotes make a string. A whole number without quotes is an integer.
.always joins, and+always adds.gettypetells you the type of any value.
new words in your collection
- type
- integer
- built-in function
This lesson isn’t saved. A free account remembers every lesson you finish.