Php 猜谜游戏
这个游戏的问题是索引 它从0开始,当它达到值5时,它给出一个错误,因为很明显数组的最后一个值被索引为4,所以数组中没有索引5 我试图用if语句阻止它,但它导致了其他错误! 有解决办法吗 通过将$index与数组中的项数进行比较,可以检查$index是否太大:Php 猜谜游戏,php,html,arrays,forms,shuffle,Php,Html,Arrays,Forms,Shuffle,这个游戏的问题是索引 它从0开始,当它达到值5时,它给出一个错误,因为很明显数组的最后一个值被索引为4,所以数组中没有索引5 我试图用if语句阻止它,但它导致了其他错误! 有解决办法吗 通过将$index与数组中的项数进行比较,可以检查$index是否太大: <?php $strArray = array("supermarket", "programming", "development", "apache","university"); $index = isset($_POST[
<?php
$strArray = array("supermarket", "programming", "development", "apache","university");
$index = isset($_POST['index']) ? $_POST['index'] : 0;
$message2 = str_shuffle($strArray[$index]);
if(!isset($_POST['guess'])){
$message1="<h1>Welcome to the shuffle game!</h1></br>";
$message2=str_shuffle($strArray[$index]);
}
elseif($_POST['guess'] == $strArray[$index]){
$message1="<h1>You guessed right!</h1></br>";
$index++;
$message2=str_shuffle($strArray[$index]);
}
elseif($_POST['guess'] != $strArray[$index]){
$message1="<h1>You guessed wrong!</h1></br>";
}
?>
<!DOCTYPE html>
<html>
<head><title>Guess the word!</title></head>
<body>
<?php echo $message1; echo "<b>Try to guess the shuffled word: </b>" . $message2; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p><label for="guess">Enter your guess:</label>
<input type="text" id="guess" name="guess" /></p>
<input type="hidden" name="index" value="<?php echo $index; ?>">
<button type="submit" name="submit" value="submit">Guess</button>
</form></body></html>
旁注:实际上,您应该在第一次定义$index的开始处进行此检查,同时检查>=0和调用is_numeric。否则,恶意用户可能会发送$_POST['index'],例如100或-1或foo。在显示游戏表单之前,请检查$index是否已达到数组的长度。如果是这样,则显示一条消息,说明游戏结束,而不是表单
<?php
$strArray = array("supermarket", "programming", "development", "apache","university");
$index = isset($_POST['index']) ? $_POST['index'] : 0;
$message2 = str_shuffle($strArray[$index]);
if(!isset($_POST['guess'])){
$message1="<h1>Welcome to the shuffle game!</h1></br>";
$message2=str_shuffle($strArray[$index]);
}
elseif($_POST['guess'] == $strArray[$index]){
$message1="<h1>You guessed right!</h1></br>";
if ($index < count($strArray) - 1) {
$index++;
} else {
$index = 0;
}
$message2=str_shuffle($strArray[$index]);
}
elseif($_POST['guess'] != $strArray[$index]){
$message1="<h1>You guessed wrong!</h1></br>";
}
?>
您可以尝试isset:当达到5时,您的预期行为是什么?如果post索引大于5,则返回false!我希望索引在达到值4thaaan后重置为0,真是太感谢你了!!
<?php
$strArray = array("supermarket", "programming", "development", "apache","university");
$index = isset($_POST['index']) ? $_POST['index'] : 0;
$message2 = str_shuffle($strArray[$index]);
if(!isset($_POST['guess'])){
$message1="<h1>Welcome to the shuffle game!</h1></br>";
$message2=str_shuffle($strArray[$index]);
}
elseif($_POST['guess'] == $strArray[$index]){
$message1="<h1>You guessed right!</h1></br>";
if ($index < count($strArray) - 1) {
$index++;
} else {
$index = 0;
}
$message2=str_shuffle($strArray[$index]);
}
elseif($_POST['guess'] != $strArray[$index]){
$message1="<h1>You guessed wrong!</h1></br>";
}
?>
<?php
$strArray = array("supermarket", "programming", "development", "apache","university");
$index = isset($_POST['index']) ? $_POST['index'] : 0;
$message2 = str_shuffle($strArray[$index]);
if(!isset($_POST['guess'])){
$message1="<h1>Welcome to the shuffle game!</h1></br>";
$message2=str_shuffle($strArray[$index]);
}
elseif($_POST['guess'] == $strArray[$index]){
$message1="<h1>You guessed right!</h1></br>";
$index++;
if ($index < count($strArray)) {
$message2=str_shuffle($strArray[$index]);
}
}
elseif($_POST['guess'] != $strArray[$index]){
$message1="<h1>You guessed wrong!</h1></br>";
}
?>
<!DOCTYPE html>
<html>
<head><title>Guess the word!</title></head>
<body>
<?php
echo $message1;
if ($index < count($strArray)) {
?>
<b>Try to guess the shuffled word: </b>" <?php echo $message2; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p><label for="guess">Enter your guess:</label>
<input type="text" id="guess" name="guess" /></p>
<input type="hidden" name="index" value="<?php echo $index; ?>">
<button type="submit" name="submit" value="submit">Guess</button>
</form>
<?php
} else {
?>
<b>Game over!</b>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Play again</a></p>
<?php
}
?>
</body></html>