Jump to content

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


Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...