Some PHP question about &

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Sometimes, when I look at other questions, I see that some variables in the function line have a '&' in front of it... what does that mean?
Code: [ Select ]
function ali_baba($var, &$another_var)
Thanks in advance
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6247
  • Loc: Seattle, WA

Post 3+ Months Ago

It means pass-by-reference.

Basically, when you normally pass an parameter into a function, a copy of that variable is made and the function manipulates the copy. When the function returns, the original variable remains unchanged.

When you pass by reference, you're passing in a reference to the variable so that the function can directly manipulate it without making a copy.

Pass-by-value (default in PHP):
Code: [ Select ]
function passByVal( $var ) {
    $var = 10;
}

$my_variable = 5;
passByVar($my_variable);
echo $my_variable; // outputs '5'
  1. function passByVal( $var ) {
  2.     $var = 10;
  3. }
  4. $my_variable = 5;
  5. passByVar($my_variable);
  6. echo $my_variable; // outputs '5'


Pass-by-reference ('&' in front of argument name):
Code: [ Select ]
function passByRef( &$var ) {
    $var = 10;
}

$my_variable = 5;
passByRef($my_variable);
echo $my_variable; // outputs '10'
  1. function passByRef( &$var ) {
  2.     $var = 10;
  3. }
  4. $my_variable = 5;
  5. passByRef($my_variable);
  6. echo $my_variable; // outputs '10'


http://us2.php.net/language.references.pass

http://www.google.com/search?hl=en&q=ph ... +reference
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Thanks. I tried googling '&' but I didn't get the definition or what it is... I didn't know what it meant either so, I couldn't google that. Thanks for the solution Spork?
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6247
  • Loc: Seattle, WA

Post 3+ Months Ago

You're welcome?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

spork wrote:
You're welcome?

lol I accidentally pressed the question mark rather than the dot. I didn't mean to put that as a question
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6247
  • Loc: Seattle, WA

Post 3+ Months Ago

:lol:
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

So, my understanding that if you do the following script...
PHP Code: [ Select ]
<?php
 
function passByRef(&$val)
{
     unset($var);
     echo $var . 'b'<br />; // This will print only the letter 'b'
}
 
$val = 'My Set Variable';
passByRef($val);
echo $val; // This will print "My Set Variable";
?>
  1. <?php
  2.  
  3. function passByRef(&$val)
  4. {
  5.      unset($var);
  6.      echo $var . 'b'<br />; // This will print only the letter 'b'
  7. }
  8.  
  9. $val = 'My Set Variable';
  10. passByRef($val);
  11. echo $val; // This will print "My Set Variable";
  12. ?>

Is my understanding correct? If I unset a variable in a function that is passed as reference, it is still set after the function is called?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright... I went ahead and tested it. It printed b and then My Set Variable so my understanding was correct (I guess I should have tested it and not posting it here).
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6247
  • Loc: Seattle, WA

Post 3+ Months Ago

When you pass a variable by value (default behavior), PHP copies the value from that variable into a new one for the function to use. Anything the function does to that variable will only affect the function's local copy; the original is still preserved.

When you pass by reference, you're literally giving the function direct access to the original variable itself, so whatever the function does to the variable will affect the original variable. This includes unsetting, changing the value, etc.

So, yes.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

http://www.php.net/manual/ini.core.php#ini.allow-call-time-pass-reference

Code: [ Select ]
function declaration(&$var) {}

Code: [ Select ]
function call_time($var){}

$var = 'variable';
call_time(&$var);
  1. function call_time($var){}
  2. $var = 'variable';
  3. call_time(&$var);

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 54 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.