0
0
PHPprogramming~10 mins

Parameters and arguments in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function that takes one parameter.

PHP
<?php
function greet([1]) {
    echo "Hello, $name!";
}
?>
Drag options to blanks, or click blank then click option'
Agreet
B$greet
C$name
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the $ sign before the parameter name.
Using the function name as a parameter.
2fill in blank
medium

Complete the code to call the function with the argument 'Alice'.

PHP
<?php
function greet($name) {
    echo "Hello, $name!";
}
greet([1]);
?>
Drag options to blanks, or click blank then click option'
Aname
B"Alice"
C$name
DAlice
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the argument without quotes.
Using variable names instead of string literals.
3fill in blank
hard

Fix the error in the function definition by completing the parameter list.

PHP
<?php
function add([1]) {
    return $a + $b;
}
?>
Drag options to blanks, or click blank then click option'
A$a, $b
B$a $b
Ca, b
D$a; $b
Attempts:
3 left
💡 Hint
Common Mistakes
Missing commas between parameters.
Omitting $ signs before parameter names.
4fill in blank
hard

Fill both blanks to complete the function call with two arguments.

PHP
<?php
function multiply($x, $y) {
    return $x * $y;
}
$result = multiply([1], [2]);
?>
Drag options to blanks, or click blank then click option'
A5
B"5"
C10
D"10"
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numbers unnecessarily.
Passing strings instead of numbers.
5fill in blank
hard

Fill all three blanks to create a function with a default parameter and call it.

PHP
<?php
function greetUser([1] = [2]) {
    echo "Hello, $name!";
}
greetUser([3]);
?>
Drag options to blanks, or click blank then click option'
A$name
B"Guest"
C"Alice"
DGuest
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the $ sign in the parameter.
Not using quotes for string default values.
Passing argument without quotes.