Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

302
Visualizações
Guessing Game PHP
<?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?

The Error

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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").

over 4 years ago · Santiago Trujillo Relatório

0

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>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda