0Pricing
PHP Academy · Lesson

Passing Arguments to Functions

Learn how to pass data into functions.

Passing Arguments to Functions is a free PHP Academy lesson on CoddyKit — lesson 4 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Passing Arguments to Functions

Welcome to the next lesson! In this lesson, you’ll learn the different ways to pass arguments to functions in PHP. This flexibility allows you to control how data is handled within your functions. Let’s get started!

Passing Arguments to Functions — illustration 1

2

What Are Function Arguments?

Arguments are values passed to a function when it is called. They allow functions to perform tasks dynamically. Example:

Tip: Specify arguments in parentheses when defining and calling functions.

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

  greet("Alice"); // Outputs: Hello, Alice!
?>

3

Passing by Value

By default, PHP passes arguments to functions by value. This means a copy of the variable is sent, and changes inside the function do not affect the original variable. Example:

Key Point: Changes inside the function do not affect the original variable.

<?php
  function addTen($number) {
    $number += 10;
  }

  $value = 5;
  addTen($value);
  echo $value; // Outputs: 5
?>

4

Passing by Reference

When you pass arguments by reference, the function can modify the original variable. Use an ampersand (&) before the parameter name to enable this. Example:

Key Point: Passing by reference is useful when you want the function to update the original variable.

<?php
  function addTen(&$number) {
    $number += 10;
  }

  $value = 5;
  addTen($value);
  echo $value; // Outputs: 15
?>

5

Default Values for Parameters

You can assign default values to function parameters. If no value is passed when calling the function, the default is used. Example:

Tip: Default values make your functions more versatile and reduce the need for additional logic.

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

  greet();        // Outputs: Hello, Guest!
  greet("Alice"); // Outputs: Hello, Alice!
?>

6

Type Hinting

Type hinting ensures that arguments passed to a function are of the correct type. Example:

Tip: Use type hinting to avoid unexpected behavior in your functions.

<?php
  function multiply(int $a, int $b): int {
    return $a * $b;
  }

  echo multiply(3, 4); // Outputs: 12
?>

7

Handling Variable-Length Arguments

Use the ...$args syntax to pass a variable number of arguments to a function. Example:

Key Point: Variable-length arguments are useful when the number of inputs is unknown.

<?php
  function sum(...$numbers) {
    return array_sum($numbers);
  }

  echo sum(1, 2, 3, 4); // Outputs: 10
?>

8

Using Named Parameters (PHP 8+)

Named parameters allow you to pass arguments to a function by their parameter name. Example:

Tip: Named parameters improve readability and make it easier to call functions with many arguments.

<?php
  function introduce($name, $age, $city) {
    echo "$name, $age years old, from $city.";
  }

  introduce(age: 25, name: "Alice", city: "New York");
// Outputs: Alice, 25 years old, from New York.
?>

9

<?php
  function addFive(&$number) {
    $number += 5;
  }

  $value = 10;
  addFive($value);
  echo $value;
?>

10

Great Job!

Congratulations! You’ve learned how to pass arguments to functions in PHP, including by value, by reference, and with default values. These techniques allow you to write flexible and powerful functions. In the next lesson, we’ll explore lambda and anonymous functions. Let’s keep coding!

Passing Arguments to Functions — illustration 10

Frequently asked questions

Is the “Passing Arguments to Functions” lesson free?

Yes — the full text of “Passing Arguments to Functions” is free to read here on the web, and the PHP Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Passing Arguments to Functions”?

Learn how to pass data into functions. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Passing Arguments to Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Are Functions?
  2. Built-in Functions
  3. User-Defined Functions
  4. Passing Arguments to Functions
  5. Return Values
← Back to PHP Academy