<?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>
The problem in this game is the index....
It starts with 0, and when it reaches the value 5 it gives an error because obviously the last value of the array is indexed as 4, so there is no index 5 in the array I tried to stop it with an if statement but it caused other errors! Any solution?
You can check if $index is too big by comparing it to the number of items in your array:
if ($index < count($strArray)) {
// $index is valid
} else {
// $index is too big
}
You need this check before incrementing $index when choosing the next shuffled string.
Your php logic could e.g. look like this:
<?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>";
}
?>
(Side note: You should actually have this check also right at the beginning where you first define $index, together with a check for >= 0 and a call to is_numeric. Otherwise a malicious user could send $_POST['index'] as e.g. "100" or "-1" or "foo").
Before displaying the game form, check if $index has reached the length of the array. If so, display a message saying that the game is over instead of the form.
<?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>