Jump to content

What does an & symbol mean in a function's parameters?


music_lp90

Recommended Posts

Hi, I'm trying to modify a component in Joomla, could someone tell me what the & symbol means when it is placed in a function's parameters?Like this: function botMosImage( $published, &$row, &$params, $page=0 )So $row and $params have the & symbol before them.Thanks!

Link to comment
Share on other sites

It passes the variable by reference instead of by value. Normally variables are passed by value, so if you have a variable that contains the number 5, and you pass that to a function, it passes the number 5 (the value). When you pass by reference, it passes the memory address of the variable (or equivalent in PHP, probably a symbol table ID or something) instead, so when the function modifies the variable the changes are visible outside of the function as well.

<?phpfunction testfunc($var1, &$var2){  $var1++;  $var2++;}$a = 0; $b = 0;testfunc($a, $b);echo $a . " " . $b; // "0 1"?>

In that case the echo statement will print 0 and 1. $a is still 0 because it was passed by value, but since $b was passed by reference then incrementing it inside the function increments the variable that was passed to the function.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...