Exercise 2 of 10 · Variables
Greet By Name
What you will make
A personalized greeting built by combining text with a variable, instead of typing the name straight into the string.
The one new idea: a variable stores a value under a name starting with $
A variable is how a program holds on to a value so it can use it more than once, or use it somewhere it didn't know the value in advance. Every exercise from here on starts by putting something into a variable first.
Go straight to the code ↓What a variable is
A variable is a name that holds a value so you can use it again later without retyping it. In PHP every variable name starts with a dollar sign — $name, $price, $total — and you give it a value with =, the assignment operator. Once a variable holds a value, writing its name anywhere afterward stands in for that value, wherever it appears.
Inside a double-quoted string, PHP goes a step further: it looks for variable names and substitutes their current value automatically. That's called interpolation, and it only works inside double quotes ("...") — inside single quotes ('...'), $name prints literally, dollar sign and all, with no substitution.
A worked example
Here's a different program using a different variable, so you can see the idea without today's answer:
<?php
$city = "Lagos";
$message = "The next stop is $city.\n";
echo $message;Running it prints:
The next stop is Lagos.$city is created on the first line, then reused on the second: it's written inside a double-quoted string, so PHP replaces $city with the value it currently holds, "Lagos", before the string is ever printed.
Your turn
The starter program creates a variable called $name, then tries to build a greeting out of it — but the greeting comes out wrong, or missing the name entirely, because the string doesn't actually refer to $name.
Find the variable that was created, note its exact spelling, and check that spelling against every place that variable is used later in the program. Fix the one that doesn't match, keeping the double quotes exactly where they are, then press Run.
If something goes wrong
If your greeting is missing the name entirely, or PHP warns about an undefined variable, the two spellings almost certainly don't match — PHP treats $name and $nam as two completely unrelated variables, with no correction and no warning that you probably meant the other one. If the greeting prints the variable's name itself instead of its value — something like Hello, $name! word for word — check whether the string is wrapped in single quotes instead of double quotes; only double quotes trigger interpolation, and single quotes print everything exactly as typed.
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
- Typing a variable's name slightly differently in two places
- PHP doesn't require you to declare a variable before using it, so a typo like $nam doesn't cause an error — it quietly creates a brand-new, empty variable, and the value you expected just doesn't show up.
- Wrapping the greeting in single quotes instead of double quotes
- Inside single quotes ('...'), PHP prints $name literally — dollar sign, letters and all — instead of substituting its value. Interpolation only happens inside double-quoted strings.
- Dropping the $ when reusing a variable
- The dollar sign is part of the variable's name, not punctuation next to it. name and $name refer to completely different things in PHP.
Want a blank editor instead? Open the PHP playground.