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

1

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

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

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

有用。只需将代码保存为guessing.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>
于 2013-10-19T19:06:42.480 回答
0

我相信您在没有网络服务器的情况下运行脚本。我自己尝试过,除了一些可以纠正的小错误外,它似乎可以工作。如果您没有网络服务器,请从这里获取。并安装它。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>

这是我运行的...

于 2013-10-19T17:10:10.633 回答