how while loop value prints outside loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ditditsasa
    New Member
    • Mar 2012
    • 17

    how while loop value prints outside loop

    Code:
    while (false !== ($sfname = readdir($dh))) {
      if (substr($sfname, 0, 1) == '.') continue;
      if ($sfname == 'CVS'            ) continue;
      $sitedir = "$OE_SITES_BASE/$sfname";
      if (!is_dir($sitedir)               ) continue;
      if (!is_file("$sitedir/sqlconf.php")) continue;
      $siteslist[$sfname] = $sfname;
        }	echo $sfname;//here not prints sfname value
    the value not prints outside the loop. please help me
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    You can't print a variable in the scope of the while loop outside it's scope, outside the while loop.

    Your code iterates through the while loop and prints the last value that was assigned to $sfname.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      @dilite922 I am confused about the information of while loop scope.
      as far I know, while loop, if statement don’t have their own scope.

      but about ditditsasa's problem, if the while condition does not satisfy in the first place in your print you won’t get anything.
      Last edited by Dormilich; Apr 13 '12, 05:12 AM.

      Comment

      Working...