我的部分脚本不断出现在浏览器中,无法正常工作,我做错了什么


Part of my script keeps appearing in the browser and it doesn't work properly, what am I doing wrong?

我目前正在阅读一本初学者 php 书,并试图做一个练习,希望我做一个猜数字游戏。 在我自己的尝试失败后,我向附录寻找答案。 我已经多次输入此代码,我相信答案键具有的字符与字符,但是当我运行脚本时,脚本的一部分不断出现在浏览器中。 我错过了什么?谢谢!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="common.css" />
    </head>
    <body>
    <?php
    if ( isset($_POST["submitButton"] ) and isset( $_POST["guess"] ) ) {
        processForm();
    } else {
        displayForm( rand( 1, 100 ) );
    }
    function processForm() {
        $randNum = (int)$_POST["randNum"];
        $guessLeft = (int)$_POST["guessLeft"];
        $guess = (int)$_POST["guess"];
        if ( $guess == $randNum ) {
            displaySuccess( $randNum );
        } elseif ( $guess == 0 ) {
            displayFail( $randNum );
        } elseif ( $guess < $randNum ) {
            displayForm( $randNum, $guessLeft, "Too low, try again.");
        } else ( $guess > $randNum ) {
            displayForm( $randNum, $guessLeft, "Too high, try again.");
        }
    }
    function displayForm( $randNum, $guessLeft=5, $message="") {
    ?>
        <form action="" method="post">
            <div>
                <input type="hidden" name="randNum" value="<?php echo $randNum?>" />
                <input type="hidden" name="guessLeft" value="<?php echo $guessLeft?>" />
                <?php if ( $message ) echo "<p>$message</p>" ?>
                <p>Guess my number.  You have <?php echo $guessLeft?> <?php echo ( $guessLeft == 1 ) ? "try" : "tries"?> left to guess correctly.</p>
                <p>What's your guess? <input type="text" name="guess" value="" style="float: none; width: 3em;" />
                                        <input type="submit" name="submitButton" value="Guess" style="float: none;" /></p>
            </div>
        </form>
        <?php
        }
        function displaySuccess( $randNum ) {
        ?>
            <p>You guess it! <?php echo $randNum?> is correct!</p>
            <form action="" method="post">
                <p><input type="submit" name="tryAgain" value="Try Again" style="float: none;" /></p>
            </form>
            <?php
            }
        function displayFail( $randNum ) {
        ?>
            <p>You ran out of guesses! My number was <?php echo $randNum?></P>
            <form action="" method="post">
                <p><input type="submit" name="tryAgain" value="Try Again" style="float: none;" /></p>
            </form>
            <?php
            }
            ?>

    </body>
</html>
我知道

你已经接受了答案,但我继续根据你的脚本为你编写了一个示例。我想如果有的话,它会在未来帮助你。整个脚本中存在许多语法错误。如果你从你的书中逐字编码,我会摆脱这本书并寻找更好的来源。

我冒昧地修改了一些东西。如果您有任何疑问,请随时与我联系。

这是游戏的工作副本,在整个脚本中进行了大量更改。

它有效。只需将代码保存为猜测.php然后在浏览器中运行它。

<?php
// Set inital variables //
// Guesses for form //
$guessLeft = 5;
// Output message //
$message = '';
// Whether to show the form or not //
$showForm = true;
// Help message to help user decide where to go,
// Up or down, and how many guesses //
$helpMessage = '';
// Displays a fail message //
function displayFail($randNum) {
    $message = "You ran out of Guesses! My number was {$randNum}.";
    return $message;
}
// Displays a success message //
function displaySuccess($randNum) {
    $message = "You guessed it! {$randNum} is correct!";
    return $message;
}
// Displays a help message //
function helpMessage($help, $guessLeft) {
    // Check plural //
    $plu = ($guessLeft > 1) ? "tries" : "try";
    // Format Output message //
    $message = '<p>';
    $message .= $help;
    $message .= " You have {$guessLeft} {$plu} left to guess correctly";
    $message .= '</p>';
    return $message;
}
// Check that submit button has been set //
if (isset($_POST["submitButton"]) and isset($_POST["guess"])) {
    // Collect $_POST information //
    $randNum = (int) $_POST["randNum"];
    $guessLeft = (int) $_POST["guessLeft"];
    $guess = (int) $_POST["guess"];
    // Check user guess //
    if ($guess < $randNum) {
        // Number too low //
        // Decrease guesses left //
        $guessLeft -=1;
        $helpMessage = helpMessage('Too low, try again.', $guessLeft);
    } elseif ($guess > $randNum) {
        // Number too high //
        // Decrease guesses left //
        $guessLeft -=1;
        $helpMessage = helpMessage('Too high, try again.', $guessLeft);
    } else {
        // Number correct, hide form show message //
        $showForm = false;
        $message = displaySuccess($randNum);
    }
    // If there are no more guesses left //
    // Hide page and display fail message //
    if ($guessLeft == 0) {
        $showForm = false;
        $helpMessage = '';
        $message = displayFail($randNum);
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Guessing Game</title>
        <link rel="stylesheet" type="text/css" href="common.css" />
    </head>
    <body>            
        <?php
        // Show help message if it is set //
        // Else display nothing //
        if (strlen($helpMessage)) {
            echo $helpMessage;
        }
        ?>
        <?php if ($showForm) { ?>
            <form action="guessing.php" method="post">
                <input type="hidden" name="randNum" value="<?php echo (isset($_POST['randNum'])) ? $_POST['randNum'] : rand(1, 100); ?>" />
                <input type="hidden" name="guessLeft" value="<?php echo $guessLeft; ?>" />
                <label for="guess">What is your guess?</label>
                <input type="text" name="guess" id="guess" value="" /><br />
                <input type="submit" name="submitButton" value="Guess" />
            </form>
            <?php
        } else {
            // Output Message //
            echo $message;
        }
        ?>
    </body>
</html>

我相信您正在运行没有Web服务器的脚本。我自己尝试过,除了一些可以纠正的小错误外,它似乎可以工作。如果您没有 Web 服务器,请从此处获取一个。并安装它。PHP不会在没有解释器的情况下运行,要让它在Web浏览器上运行,你需要一个Web服务器。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<?php
if ( isset($_POST["submitButton"] ) and isset( $_POST["guess"] ) ) {
    processForm();
} else {
    displayForm( rand( 1, 100 ) );
}
function processForm() {
    $randNum = (int)$_POST["randNum"];
    $guessLeft = (int)$_POST["guessLeft"];
    $guess = (int)$_POST["guess"];
    if ( $guess == $randNum ){
        displaySuccess( $randNum );
    } elseif ( $guess == 0 ){
        displayFail( $randNum );
    } elseif ( $guess < $randNum ){
        displayForm( $randNum, $guessLeft, "Too low, try again.");
    } 
    elseif( $guess > $randNum ){
        displayForm( $randNum, $guessLeft, "Too high, try again.");
    }
}
function displayForm( $randNum, $guessLeft=5, $message="") {
?>
    <form action="" method="post">
        <div>
            <input type="hidden" name="randNum" value="<?php echo $randNum?>" />
            <input type="hidden" name="guessLeft" value="<?php echo $guessLeft?>" />
            <?php if ( $message ) echo "<p>$message</p>" ?>
            <p>Guess my number.  You have <?php echo $guessLeft?> <?php echo ( $guessLeft == 1 ) ? "try" : "tries"?> left to guess correctly.</p>
            <p>What's your guess? <input type="text" name="guess" value="" style="float: none; width: 3em;" />
                                    <input type="submit" name="submitButton" value="Guess" style="float: none;" /></p>
        </div>
    </form>
    <?php
    }
    function displaySuccess( $randNum ) {
    ?>
        <p>You guess it! <?php echo $randNum?> is correct!</p>
        <form action="" method="post">
            <p><input type="submit" name="tryAgain" value="Try Again" style="float: none;" /></p>
        </form>
        <?php
        }
    function displayFail( $randNum ) {
    ?>
        <p>You ran out of guesses! My number was <?php echo $randNum?></P>
        <form action="" method="post">
            <p><input type="submit" name="tryAgain" value="Try Again" style="float: none;" /></p>
        </form>
        <?php
        }
        ?>

</body>

这是我运行的那个...