...

View Full Version : scope



subhailc
09-09-2005, 11:12 AM
hi - php's scope specifically in regard to function wrappers is throwing me. basic recurser to echo out files and call itself on subdirs, it works fine. eg
function getfiles($a){
echo $a;
if($b=opendir($a))
while($c=readdir($b))
if(is_dir($a."/".$c)) getfiles($a."/".$c);
else echo $a."/".$c;
closedir($b);}
getfiles('.');

but if i try to leave the curlies, i get nothing - like if i were change the echo to $somestring.= and define somestring anywhere but inside the function, i get bupkiss - same holds true using array_push and and array. i tried doing that and then passing them as arguments like getfiles($a,$b) where $b would be a pointer to the array defined earlier, but still no good.

any tips?

subhailc
09-09-2005, 01:13 PM
this works
function getfiles($a){
echo $a;
if($b=opendir($a))
while($c=readdir($b))
if(is_dir($a."/".$c)) getfiles($a."/".$c);
else echo $a."/".$c;
closedir($b);}
getfiles('.');
this doesnt
$somestring="";
function getfiles($a){
$somestring.=$a;
if($b=opendir($a))
while($c=readdir($b))
if(is_dir($a."/".$c)) getfiles($a."/".$c);
else $somestring.=$a."/".$c;
closedir($b);}
getfiles('.');
echo $somestring;
neither does this
$somearray=array();
function getfiles($a){
array_push($somearray,$a);
if($b=opendir($a))
while($c=readdir($b))
if(is_dir($a."/".$c)) getfiles($a."/".$c);
else array_push($somearray, $a."/".$c);
closedir($b);}
getfiles('.');
foreach($somearray as $i) echo $i;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum