Re: function by reference

From: Date: Tue, 11 Apr 2006 23:18:09 +0000
Subject: Re: function by reference
References: 1 2 3 4 5 6  Groups: php.general 
Request: Send a blank email to php-general+get-233811@lists.php.net to get a copy of this message
The ampersand before the function name indicates that the function returns a reference instead of a copy of the variable, for example: <?php function &max(&$var1, &$var2) {
    if ($var1 > $var2) return $var1;
    else return $var2;
} $global1 = 10; $global2 = 9; $maxglobal =& max($global1, $global2); $maxglobal++; echo $global1; //this will print 11 since $maxglobal is a reference to $global1 ?>
It's going to hurt thinking about that. :-) But that produces the same results as: <?php function max($var1, $var2) {
    if ($var1 > $var2) return $var1;
    else return $var2;
} $global1 = 10; $global2 = 9; $maxglobal =& max($global1, $global2); $maxglobal++; echo $global1; //this will print 11 since $maxglobal is a reference to $global1 ?> Note the absence ampersands in the function, but an ampersand remains in the assignment: Thanks. tedd -- -------------------------------------------------------------------------------- http://sperling.com

« previous php.general (#233811) next »