Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is there an automatic-global setting?

Status
Not open for further replies.

miahmiah900

Programmer
Sep 19, 2003
38
US
i want to change a list of variables passed to a function through an array that holds their names - do i have to somehow declare all of these variables global, or is there a simple "make everything global in this function" setting?
 
why not pass the pointer to the original variables to the function???

Known is handfull, Unknown is worldfull
 
sorry, i dont know what you mean? this seems a really basic question, and i feel silly for asking it ;)

 
Let me illustrate.

Code:
function refExample($a, &$b) {

  $a = "hello";
  $b = "world";

  echo &quot;$a $b<br>&quot;;
}

$greeting = &quot;goodbye&quot;;
$recipient= &quot;mike&quot;;

echo $greeting.' '.$recipient.'<br>';
refExample($greeting, $recipient);
echo $greeting.' '.$recipient.'<br>';


If a function receives a variable as &$var instead of $var, it will work on the memory address of the variable, instead of a copy of the variable... in other words, it will work in the scope of the caller instead of the callee.

-Rob
 
i am not at home right now so i cant paste my code, but i was having problems because i passed the names of the variables to the function in an array, and then referenced the variables by using an eval statement to build the actual code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top