Home » Programming » PHP » Php Pass Variable By Reference

Passing Variables by Reference in PHP, with Examples

This article will explain how to pass a PHP variable by reference and why you might want to do it – with some examples.

What is a Variable?

You probably already know this one. A variable stores a value – it has a name, and you can use that name to refer to the variable and the value it contains.

In PHP, variable names are prepended with a $ (dollar symbol) and must contain only numbers, letters, dashes, and underscores:

$variableName = "Variable Value";

Variable Behaviour in PHP

In PHP, when you pass a variable to a function, a copy is made. Any changes to the variable passed to the function inside the function are not reflected outside of the function.

What is ‘Passing By Reference’?

Passing By Reference‘ means that a reference to the original variable can be passed to a function rather than the variable being copied.

Rather than a new (copy of the original) variable being created, a reference to the original is created so that any changes made inside the function to the variable are reflected outside of it – the original will change too.

PHP Syntax

To pass a PHP variable by reference, simply add the & (ampersand) character to the beginning of the variable name. See the below examples for how this looks in action.

The result of a function can also be passed by reference by prepending the function name with &.

Examples

See below for examples of passing variables and the result of functions by reference – the comments will explain what’s going on.

<?php

    // Define a function which increments a number passed to it normally
    function myIncrementFunction($var) {
        $var++;
        return $var;
    }

    // Define a function which increments a number passed to it by reference
    function myIncrementByRefFunction(&$var) {
        $var++;
        return $var;
    }

    // Define a variable to test the above functions with
    $testVariable = 3;

    // Print testVariable to confirm its value
    echo $testVariable; // Will output 3

    // Print the result of running myIncrementFunction with testVariable
    echo myIncrementFunction($testVariable); // Will output 4

    // Confirm that the value of $testVariable has not changed as it was NOT passed by reference
    echo $testVariable; // Will output 3

    // Print the result of running myIncrementByRefFunction with testVariable
    echo myIncrementByRefFunction($testVariable); // Will output 4

    // Confirm that the value of $testVariable has now changed because the variable WAS passed by reference
    echo $testVariable; // Will output 4 - as the original variable has been changed as it was passed by reference

?>

For more information on passing PHP variables by reference, check out the official PHP documentation.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment