what is passing parameters by reference php code example

Example 1: php pass by reference

<?php
function appendToString(&$string)
{
    $string .= 'Cool Im appended';
}

$str = 'I am a start and - ';
appendToString($str);
echo $str;    // outputs 'I am a start and - Cool Im appended'
?>

Example 2: php & before variable

It passes a reference to the variable so when any variable assigned the reference 
is edited, the original variable is changed. They are really useful when making 
functions which update an existing variable. Instead of hard coding which variable 
is updated, you can simply pass a reference to the function instead.

Example

<?php
    $number = 3;
    $pointer = &$number;  // Sets $pointer to a reference to $number
    echo $number."<br/>"; // Outputs  '3' and a line break
    $pointer = 24;        // Sets $number to 24
    echo $number;         // Outputs '24'
?>

Tags:

Php Example