我目前正在通过一本初学者的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>
答案 0 :(得分: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>
答案 1 :(得分:0)
我相信你是在运行带有网络服务器的脚本。我自己尝试了它,它似乎工作,除了一些可以纠正的小错误。如果您没有Web服务器,请从here获取一个。并安装它。 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>
这是我跑的那个......