php · level 1 · unit 7 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a slot where your function catches information.
Right now your function does the same thing every time. Cut a slot in the front, and you can feed in something new on each go. That slot is a parameter, like $name in function greet($name).
The value you drop in when you call is the . Call greet("Mia") and $name holds "Mia". Call greet("Leo") and the same machine greets Leo instead.
A function cannot see the boxes outside it. If it needs a value, hand it over as an argument. Inside, the parameter works like any other variable.
<?php
function greet($name) {
echo "Hello, $name!\n";
}
greet("Mia");
greet("Leo"); For two slots, separate the parameters with commas, and the arguments too.
guess first
Two slots this time. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<?php
function order($food, $drink) {
echo "I would like $food with $drink.\n";
}
order("lemonade", "chips");
your turn
Your turn. Run the postcard machine.
Type a place and the weather there in each pair of blanks. Then run it and watch one machine write two postcards.
fix it
Rex is right there on line 2, but this crashes. Fix it.
- Read the error:
Undefined variable $peton line 4. Line 2 fills that box, so why can the function not see it? - A function cannot see the boxes outside it. Hand the value over as an argument.
- Give the function a parameter,
function feed($pet), and call it withfeed($pet);, then press run.
nailed it
your functions take orders now. :D
- A parameter is a slot in the function's brackets.
- An argument is the value you drop in when you call.
- A function only sees what you hand it.
new words in your collection
- parameter
- argument
This lesson isn’t saved. A free account remembers every lesson you finish.