开发者

variables outside loop

开发者 https://www.devze.com 2023-03-12 15:21 出处:网络
I want to use all $gamename varibles again: foreach($gamelist as $e){ $chunks = explode(\'/\',$e->nodeValue);

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

精彩评论

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

关注公众号