Menu
Coddy logo textTech

Passing by Reference

Part of the Logic & Flow section of Coddy's PHP journey — lesson 7 of 68.

By default, when you pass a variable to a function in PHP, the function receives a copy of that variable's value. This means any changes made inside the function don't affect the original variable outside the function.

However, sometimes you want a function to modify the original variable directly. This is where passing by reference comes in. By adding an ampersand (&) before the parameter name in your function definition, you tell PHP to pass the actual variable instead of a copy:

<?php
function doubleValue(&$number) {
    $number = $number * 2;
}

$value = 10;
doubleValue($value);
echo $value;  // Outputs: 20
?>

Notice how $value changed from 10 to 20 even though we didn't return anything from the function. The &$number parameter creates a direct link to the original $value variable, allowing the function to modify it directly.

This technique is particularly useful when you need a function to modify multiple variables or when working with large data structures where copying would be inefficient.

quiz iconTest yourself

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

quiz iconTest yourself

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

quiz iconTest yourself

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

challenge icon

Challenge

Easy

Create a function called updateInventory that takes three parameters: a reference to a quantity variable, an amount to add, and an amount to remove. The function should modify the original quantity by adding the first amount and subtracting the second amount.

You will receive three inputs: the initial quantity, the amount to add, and the amount to remove. Read all three inputs, create a variable with the initial quantity, call the updateInventory function passing the quantity by reference along with the add and remove amounts, and print the updated quantity.

Input format: Three lines - first line contains the initial quantity (a number), second line contains the amount to add (a number), third line contains the amount to remove (a number)

Expected output: The updated quantity after adding and removing

Cheat sheet

By default, PHP passes variables to functions by value (a copy). To modify the original variable, use pass by reference by adding an ampersand (&) before the parameter name:

<?php
function doubleValue(&$number) {
    $number = $number * 2;
}

$value = 10;
doubleValue($value);
echo $value;  // Outputs: 20
?>

The &$number parameter creates a direct link to the original variable, allowing the function to modify it without returning a value. This is useful for modifying multiple variables or working with large data structures efficiently.

Try it yourself

<?php
// Read inputs
$initialQuantity = intval(fgets(STDIN));
$amountToAdd = intval(fgets(STDIN));
$amountToRemove = intval(fgets(STDIN));

// Create quantity variable
$quantity = $initialQuantity;

// TODO: Create the updateInventory function here that takes a reference parameter

// TODO: Call the updateInventory function with the quantity by reference

// Output the updated quantity
echo $quantity;
?>
quiz iconTest yourself

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

All lessons in Logic & Flow