minte9
LearnRemember / PHP



Reference

Function arguments can be passed by reference.
 
/**
 * Function arguments, passed by reference
 */

function findAndCount($s, $char, &$j) // Look Here
{
    $i = 0;
    while($i < strlen($s)) {
        if ($s[$i] == $char) 
            $j++;
        $i++;
    }
}

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

echo $count; // 2

Default values

PHP allows default values, even when parameters are declared as by-reference.
 
/**
 * Default function arguments
 */

function test($x, &$y = null) 
{
    $y = 100;
}

$x = 1;
$y = 2;

test($x, $y);
echo $y; // 100






Questions and answers




Is this code valid?

  • a) Yes
  • b) No

  • a) test(&$a, $b=0)
  • b) test($a, &$b)


References