Menu
Coddy logo textTech

Function Parameters

Part of the Fundamentals section of Coddy's PHP journey — lesson 62 of 71.

The sayHello function from the previous lesson always does the same thing. But what if you want to greet different people? Parameters let you pass data into a function, making it flexible and dynamic.

Parameters are variables listed inside the parentheses when you declare a function. When you call the function, you provide values (called arguments) that get assigned to those parameters:

<?php
function greet($name) {
    echo "Hello, $name!\n";
}

greet("Alice");
greet("Bob");
?>

This outputs:

Hello, Alice!
Hello, Bob!

The parameter $name acts like a placeholder. Each time you call greet(), the argument you pass replaces that placeholder inside the function.

Functions can accept multiple parameters, separated by commas:

<?php
function introduce($name, $age) {
    echo "$name is $age years old.\n";
}

introduce("Alice", 25);
introduce("Bob", 30);
?>

This outputs:

Alice is 25 years old.
Bob is 30 years old.

When calling a function with multiple parameters, the order matters—the first argument goes to the first parameter, the second to the second, and so on.

challenge icon

Challenge

Easy

Read three lines of input:

  1. A person's name (e.g., Alice)
  2. A city name (e.g., Paris)
  3. A year (e.g., 2020)

Create a function called describePerson that accepts three parameters: $name, $city, and $year. The function should print a message in the following format:

[name] moved to [city] in [year].

Call the function with the input values.

Example 1:

If the inputs are Alice, Paris, and 2020, the output should be:

Alice moved to Paris in 2020.

Example 2:

If the inputs are Bob, Tokyo, and 2018, the output should be:

Bob moved to Tokyo in 2018.

Example 3:

If the inputs are Emma, New York, and 2015, the output should be:

Emma moved to New York in 2015.

Cheat sheet

Parameters are variables listed inside the parentheses when declaring a function. When calling the function, you provide values (arguments) that get assigned to those parameters:

<?php
function greet($name) {
    echo "Hello, $name!\n";
}

greet("Alice");
?>

Functions can accept multiple parameters, separated by commas:

<?php
function introduce($name, $age) {
    echo "$name is $age years old.\n";
}

introduce("Alice", 25);
?>

When calling a function with multiple parameters, the order matters—the first argument goes to the first parameter, the second to the second, and so on.

Try it yourself

<?php
// Read input
$name = trim(fgets(STDIN));
$city = trim(fgets(STDIN));
$year = trim(fgets(STDIN));

// TODO: Create a function called describePerson that accepts three parameters
// ($name, $city, $year) and prints the message in the required format


// Call the function with the input values

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals