Shoutbox

PHP help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP help (/showthread.php?tid=80411)

PHP help by lordy on 12-25-2007 at 01:15 PM

Just got a quick question... if I'm creating a function in PHP, and I'm creating variables in said function. How do i make those variables available outside the actual function? I know how to make variables created outside the function available inside the function... but how do you do the reverse?

for example:

code:
$blah = 2;
$blah2 = 4;

function yay() {
global $blah, $blah2;

$minus = $blah2 - $blah;
$plus = $blah + $blah2;
}


I know that doesn't do anything but meh its an example...

if what I understand is correct, to make those variables ($plus and $minus) available to other things outside the function, do i also need to add them to the global statement alongisde $blah and $blah2 inside the actual function??
RE: PHP Variables by Eljay on 12-25-2007 at 01:37 PM

You have to define the variable in the global scope first, e.g:

code:
$test = 'foo';

function blah(){
  global $test;
  $test = 'bar';
}

echo $test; // outputs foo
blah();
echo $test; // outputs bar


RE: PHP help by lordy on 12-25-2007 at 01:38 PM

thanks Eljay,

another question, if I set a return value for a function, inside a loop which is in the function once a certain criteria is met, does that then break the loop?


RE: PHP help by Eljay on 12-25-2007 at 01:42 PM

quote:
Originally posted by lordy
thanks Eljay,

another question, if I set a return value for a function, inside a loop which is in the function once a certain criteria is met, does that then break the loop?

yes. :P
RE: PHP help by lordy on 12-25-2007 at 01:44 PM

thankin you muchly master Eljay