Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: scope

  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post

    scope

    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
    Code:
    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?

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    this works
    Code:
    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
    Code:
    $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
    Code:
    $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;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •