PHP文件无限循环

时间:2015-08-26 00:10:27

标签: php input infinite-loop

在我的游戏中,我实现了HackingWithPhp中最初设计用于在CLI上运行的文件,但我尝试将其移植到html / css / browsers上运行。显然,这段代码无限循环,但我不明白为什么。当我把它放在FTP服务器上时,我的浏览器在我尝试加载页面时崩溃了(因为php是后端的)...谁能告诉我为什么?任何帮助将不胜感激。

这里有代码:(我有include_once 'index.php';,因为我的主网​​站文件中有一个文本框输入,输入存储的变量位于该文件中,所以我需要导入它能够在这里使用它...如果有任何方法可以在此代码中输入工作文本框输入,那将不胜感激。)

<?php
include_once 'index.php';
$World = simplexml_load_file("gameworld.xml");
$CurrentPos = 0;
$Done = 0;
print "<br>";
printplace();
function printplace() {
    GLOBAL $World, $CurrentPos;
    $Room = $World->ROOM[$CurrentPos];
    $Name = $Room->NAME;
    $Desc = wordwrap((string)$Room->DESC);
    print "$Name<br>";
    print str_repeat('-', strlen($Name));
    print "<br>$Desc<br>";
    if ((string)$Room->NORTH != '-') {
        $index = (int)$Room->NORTH;
        print "North: {$World->ROOM[$index]->NAME}<br>";
    }
    if ((string)$Room->SOUTH != '-') {
        $index = (int)$Room->SOUTH;
        print "South: {$World->ROOM[$index]->NAME}<br>";
    }
    if ((string)$World->ROOM[$CurrentPos]->WEST != '-') {
        $index = (int)$Room->WEST;
        print "West: {$World->ROOM[$index]->NAME}<br>";
    }
    if ((string)$World->ROOM[$CurrentPos]->EAST != '-') {
        $index = (int)$Room->EAST;
        print "East: {$World->ROOM[$index]->NAME}<br>";
    }
    print "<br>";
}

while (!$Done) {
    print "<br>"; // add another line break after the user input

    $input = split(' ', $input);

    switch(trim($input[0])) {
        case 'north':
            if ((string)$World->ROOM[$CurrentPos]->NORTH != '-') {
                $CurrentPos = (int)$World->ROOM[$CurrentPos]->NORTH;
                printplace() ;
            } else {
                print "You cannot go north!<br>";
            }
            break;
        case 'south':
            if ((string)$World->ROOM[$CurrentPos]->SOUTH != '-') {
                $CurrentPos = (int)$World->ROOM[$CurrentPos]->SOUTH;
                printplace() ;
            } else {
                print "You cannot go south!<br>";
            }
            break;
        case 'west':
            if ((string)$World->ROOM[$CurrentPos]->WEST != '-') {
                $CurrentPos = (int)$World->ROOM[$CurrentPos]->WEST;
                printplace() ;
            } else {
                print "You cannot go west!<br>";
            }
            break;
        case 'east':
            if ((string)$World->ROOM[$CurrentPos]->EAST != '-') {
                $CurrentPos = (int)$World->ROOM[$CurrentPos]->EAST;
                printplace() ;
            } else {
                print "You cannot go east!<br>";
            }
            break;
        case 'look':
            printplace() ;
            break;
        case 'quit':
            $Done = 1;
            break;
    }
}

print "<br>Thanks for playing!<br>";
?>

1 个答案:

答案 0 :(得分:1)

你的循环再次重复相同的论点。你需要这样的我想:

$input = explode(' ', $input);
foreach ($input as $command) { //no need for "quit" case or $done flag
    switch ($command) {
        ...
        case default: break; //invalid command (ignore)
    }
}