<?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>
这个游戏中的问题是索引......
从0开始,当它达到值5时,它会产生错误,因为显然数组的最后一个值被索引为4,所以数组中没有索引5 我试图用if语句来阻止它,但它导致了其他错误! 任何解决方案?
答案 0 :(得分:0)
您可以通过将$index
与数组中的项目数进行比较来检查if ($index < count($strArray)) {
// $index is valid
} else {
// $index is too big
}
是否过大:
$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>";
}
?>
之前需要进行此检查。
你的php逻辑可以是看起来像这样:
$index
(旁注:实际上,您应该在第一次定义>= 0
时开始检查,同时检查is_numeric
并调用$_POST['index']
。否则恶意用户可以发送 curl -H @{"X-Api-Key" = "j65k423lj4k2l3fds"} -Method PUT 'https://some/working/file.xml'
,例如“100”或“-1”或“foo”)。
答案 1 :(得分:0)
在显示游戏表单之前,请检查$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>";
$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>