开发者

variables outside loop

I want to use all $gamename varibles again:

foreach($gamelist as $e){
            $chunks = explode('/',$e->nodeValue);
     开发者_StackOverflow社区       $gamename = substr($chunks[2],0,-5);
            echo $gamename;
            echo "<br/>";
        }
 getObjects($fullPath,$folder,$gamename)

As I saw $gamename is undefined outside the foreach loop.


Based on your edit, you would need something like:

$gamenames = array();

foreach($gamelist as $e){
            $chunks = explode('/',$e->nodeValue);
            $gamename = substr($chunks[2],0,-5);
            echo $gamename;
            $gamenames[] = $gamename;
            echo "<br/>";
        }

// $gamenames is an array containing all game names
getObjects($fullPath,$folder,$gamenames[0])    // for the first game name


out the loop $gamename had the value of the last iterate,

or undefined if the loop never run ($gamelist is empty)


for loops have local scope, meaning that variables declaring inside them don't exist outside the execution of the loop, if you want to reuse the $gamename variables you should create them outside the loop, refer to @jeroen's post for an example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜