Re: function by reference

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux