PROGRAMMING     CLIMBING

  minte9
reminder




Last update:   20-02-2022

Passed by reference

Function arguments can be passed by reference.
 
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

Default values

PHP allows default values. Even when parameters are declared as by-reference.
 
function test($var1, &$var2 = null) 
{
    $var2 = "c";
}

$var1 = 'a';
$var2 = 'b';

test($var1, $var2);

echo $var2; // Output: c

Questions    
Function variable scope
        A B C D E F
🔔
1/2