[RESOLVED] it doesn't work in function
ok My code doesn't work when its in a function, yet it does when its not why is that?
This Works:
PHP Code:
$handle = fopen("http://www.runescape.com/lang/en/aff/runescape/serverlist.ws?lores.x=0&plugin=0&order=WPML","r");
if($handle){
while (!feof($handle)) {
$contents .= fread($handle,8192);}
fclose($handle);}
$world = 2;
$lengthA = strpos(stristr($contents, 'World '.$world), 'FULL');
$lengthB = strpos(stristr($contents, 'World '.$world), 'Players');
if ($lengthA < $lengthB){
echo 'Full';
}
else
{
echo strpos(stristr($contents, 'World '.$world), 'Players');
}
And This Doesn't:
PHP Code:
$handle = fopen("http://www.runescape.com/lang/en/aff/runescape/serverlist.ws?lores.x=0&plugin=0&order=WPML","r");
if($handle){
while (!feof($handle)) {
$contents .= fread($handle,8192);}
fclose($handle);}
function Population($world) {
$lengthA = strpos(stristr($contents, 'World '.$world), 'FULL');
$lengthB = strpos(stristr($contents, 'World '.$world), 'Players');
if ($lengthA < $lengthB){
return "Full";
}
else
{
return strpos(stristr($contents, 'World '.$world), 'Players');
}
}
echo Population(2);
Why is that?
Re: it doesn't work in function
Its because of variable scope ($handle is used outside the function). Any variable initialised outside a function must be accessed via the $GLOBALS array or declared in the function using global:
PHP Code:
$handle = ....
function blah()
{
echo($GLOBALS['handle']);
}
function glah()
{
global $handle;
echo($handle);
}
Re: it doesn't work in function
WOW I am dumb! I forgat the
PHP Code:
global $contents;
I have waisted a whole 30 minuts on that crap! and all the problem was a line i forgat.... thank man