php · level 1 · unit 2 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a box with a name on it.
A variable is a box with a name on it. You put a in the box, such as a string, and from then on you use the name to get it back.
In PHP every variable name starts with a dollar sign, like $name. Read = as put into: $name = "Sam"; puts "Sam" into the box called $name. Then echo $name; shows what is inside.
Spell the name differently and PHP finds no box with that name. It stops with an that says Undefined variable.
<?php
$name = "Sam";
echo $name; Say $name = "Sam"; out loud as: put Sam into the box called name.
guess first
Box or string? What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<?php
$pet = "Biscuit";
echo "pet";
your turn
Your turn. Name a pet and open the box.
Make up a pet name and type it in place of the blank. Then press run and watch echo $pet; show what is in the box.
fix it
This one is broken. Can you fix it?
- Read the error:
Undefined variable $nmae. Is there a box with that exact name? - Compare the name on line 3 with the box on line 2. Two letters have swapped places.
- Change
$nmaeto$nameon line 3, then press run.
nailed it
you made the computer remember. :D
- A variable is a box with a name on it.
=puts a value into the box.- Spell the name the same way every time.
new words in your collection
- variable
- value
- ErrorException
This lesson isn’t saved. A free account remembers every lesson you finish.