Re: Passing by reference...

From: Date: Tue, 01 Jan 2002 02:02:42 +0000
Subject: Re: Passing by reference...
References: 1 2  Groups: php.windows 
Request: Send a blank email to php-windows+get-11245@lists.php.net to get a copy of this message
At 08:51 PM 12/31/2001 -0600, Anthony Ritter wrote:
............................................... <? function tax (&$Salary) { $Salary= $Salary - (($Salary/100)*20); return $Salary; } $Salary=2500; echo (tax($Salary)); // This displays $2000
echo $Salary;         // This also displays $2000
?> ........................................ The question really was about the *ampersand* in the argument line which supposedly changes the value of the variable to 2000 from 2500 because of the ampersand symbol.
Yes, that is true. The "tax" function has the ampersand in the argument list, which means that any variable that you send to it gets passed by reference. This means that any changes to that variable that occur inside the function will actually affect the same variable that is in the scope of the main script. Without the ampersand, the function will only modify it's own local copy of the variable and leave the one in the main script unaltered. If you remove the ampersand from the above script and re-run it, then the output of the first echo statement should not change, but the second one will change to 2500 since the function will no longer be altering the variable in the main script. http://www.php.net/manual/en/language.references.pass.php Sorry if I'm not understanding your question...

« previous php.windows (#11245) next »