0

私は現在、初心者向けの 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>
4

2 に答える 2