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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scope of variable

Status
Not open for further replies.

Beez

Programmer
Nov 24, 2002
3
CH
I am calling functions within functions and was wondering how you pass a variable from one to another. Declaring the variable as global only seems to work within the first and highest level function called, but not for other functions that are being called within functions. Thanks in advace for any help.
 
Below is a basic example of passing variables with functions. I hope this will explain the ways in which to do it. TRY not to use global variables.

function dosomething($first,&$second,$third=0)
{
echo &quot;first=&quot; . $first . &quot;<br />&quot;; // pass variable
echo &quot;second=&quot; . $second . &quot;<br />&quot;; // pass variable, and alterations change original
echo &quot;third=&quot; . $third . &quot;<br />&quot;; // pass third, default to 0 if not passed
$first =$first + 10;
$second=$second + 10;
$third =$third + 10;

return TRUE; // not necessary but usefull if need to check if function was successful
}

$a = 50;
$b = 60;
echo &quot;a=&quot; . $a . &quot;<br />&quot;;
echo &quot;b=&quot; . $b . &quot;<br />&quot;;
dosomething($a,$b);
echo &quot;a=&quot; . $a . &quot;<br />&quot;;
echo &quot;b=&quot; . $b . &quot;<br />&quot;;

first is called pass by value : a copy of the variable (value) is passed into the function, you can alter the value within the function without it changing the original value.
second is pass by reference : the location of the variable is passed to the function, so any changes made to this change the original as well.
third is a pass by value but it also has a default if it is omitted when called. Usefull for extending the functionality of a function while still keeping the code simple.
e.g.
function welcomemessage($name,$heading=&quot;Welcome&quot;)
{
echo $heading . &quot; &quot; . $name . &quot;<br />&quot;;
}
welcomemessage(&quot;John&quot;);
// gives 'Welcome John'
welcomemessage(&quot;John&quot;,&quot;Greetings);
// gives 'Greetings John'

I try and put the variables with default values at the end of the list, because it is easier to read and understand the call. ie dosomthingelse($a,,,,,&quot;rr&quot;,,,) - yuck!!
 
Ok, that made sence and it worked, thanks. I only have one other problem though. When I 'return $result' from my second function back to the first function there is again a scope problem. (var $result is not being passed) Useing the same logic explaned to me does not seem to work nor does defining the var as global. Does php4 have a rule about returning a result into a function? Again thanks in advance...
 
Don't worry about it, figured it out. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top