php · level 1 · unit 3 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Double quotes fill in the gaps in your text.
Joining with dots gets fiddly. Here is a shortcut: inside double quotes, PHP spots each variable and drops its value in. So "Hi, $name!" shows Hi, Mia!.
, like 'Hi, $name!', work differently. PHP shows the text exactly as typed, $ and all, and \n stays as two characters too. Use double quotes when you want the gaps filled.
If letters follow straight after a name, PHP cannot tell where the name ends. Wrap it in : "{$pet}s" shows cats.
<?php
$name = "Mia";
$age = 9;
echo "Hello, $name! You are $age.\n";
echo 'Hello, $name!'; Numbers drop into double quotes too, so $age shows 9 with no joining needed.
guess first
Single quotes this time. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<?php
$pet = "cat";
echo 'I love my $pet\n';
your turn
Your turn. Write a birthday card.
Type a name between the quotes and an age, with no quotes, in the other blank. Then run it to fill in the card.
fix it
How many kittens? Can you fix it?
- Read the error:
Undefined variable $pets. Which box did you mean? - PHP thinks the s is part of the name. Show it where the name stops.
- Write
{$pet}sinstead of$pets, then press run.
nailed it
your text fills in its own gaps. :D
- Variables inside double quotes are swapped for their values.
- Single quotes show text exactly as typed.
- Curly brackets mark where a name stops, like
{$pet}s.
new words in your collection
- single quotes
- curly brackets
This lesson isn’t saved. A free account remembers every lesson you finish.