Memory App
 




Passed by reference

Function arguments can be passed by reference, by prefixing them with the by-reference operator &

function myFind($str, $char, &$count) 
{
    for($i=0; $i<strlen($str); $i++) {
        if ($str[$i] == $char) $count++;
    }
}

myFind("abca", "a", $count);

echo $count; // output 2
(1/2)

Default values

PHP 5 allows default values to be specified for parameters even when they are declared as by-reference:

function test($var1, &$var2 = null) 
{
    $var2 = "c";
}

$var1 = 'a';
$var2 = 'b';
test($var1, $var2);
echo $var2; // Output: c


Questions