0

This is the code that I wrote and it's not working. it's suppose to ask the user a odd number and check is it correct.

Code:

$guess = $_POST['guess'];
$submit = $_POST['submit'];

if(isset($submit)){
        if ($guess/2 %0){
                echo "the number you are guessing is not odd";
        }elseif{

         ($guess<$rand)
                echo "Your Guess is Smaller than the secret number";
        }elseif{
                ($guess>$rand)
                         echo "Your Guess is bigger than the secret number";
        }else{
                          ($guess==$rand)
        echo "you guessed currectly. now try anouthe number!";}





else
        header("Location: index.php");
                exit();}

?>
Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35

2 Answers2

3

Can you try this?

You placed the '()' in your elseif wrong.

<?php
$rand = rand(1, 99);

$guess  = $_POST['guess'];
$submit = $_POST['submit'];

if(isset($submit))
{
    if($guess / 2 % 0)
    {
        echo "the number you are guessing is not odd";
    }
    elseif($guess < $rand)
    {
        echo "Your Guess is Smaller than the secret number";
    }
    elseif($guess > $rand)
    {
        echo "Your Guess is bigger than the secret number";
    }
    elseif($guess == $rand)
    {
        echo "you guessed currectly. now try anouthe number!";
    }
}
else
{
    header("Location: index.php");
    exit();
}

?>

I have not tested this code, so i need your feedback. Edit: You've confirmed this worked.

I'd like to provide you the manual about elseif: http://php.net/manual/en/control-structures.elseif.php

Please consider easier/cleaner coding. Personally I like to use ':' instead of '{}', it's less code and easier to read when you use HTML mixed with PHP like:

<?php
$rand = rand(1, 99);

$guess  = $_POST['guess'];
$submit = $_POST['submit'];

if(isset($submit)):
    if($guess / 2 % 0):
        echo "the number you are guessing is not odd";
    elseif($guess < $rand):
        echo "Your Guess is Smaller than the secret number";
    elseif($guess > $rand):
        echo "Your Guess is bigger than the secret number";
    elseif($guess == $rand):
        echo "you guessed currectly. now try anouthe number!";
else:
    header("Location: index.php");
    exit();
endif;
?>

And don't forget to check the $_POST data.

Same goes for array, but thats a side note:

$arr = array(1 => 'hi', 2 => 'hello'); // old
$arr = [1 => 'hi', 2 => 'hello']; // new
Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35
  • 1
    You solved the main error, but now it is giving me a logical error see if you can help me, at first it asks the user to enter an odd no. (the user enter even) and prompts him that it is even, then the user enters an odd no. prompts him that it is Smaller than $rand. then the user enters another no. and prompts him that it is bigger than $rand. at last the user enter the right odd no. – Zack Durrandon Dec 06 '17 at 10:16
  • Hi Zack, i am not sure what the new problem is. I'm glad ive solved the main error. It seems you are facing a new problem now. Perhaps you want to create a new question with the new problem you are facing. For now i suggest you mark one of the answers as the answer and create a new question. So we can do it step by step. – Ronnie Oosting Dec 06 '17 at 10:19
  • I'm banned this is all I could do. 1-It asks the user to enter an odd no. (the user enters even no.) and prompts him that it is not odd, 2-The user enters an odd no. prompts him that it is Smaller than $rand. 3-The user enters another no. and prompts him that it is bigger than $rand. 4-the user enters the right odd no. – Zack Durrandon Dec 06 '17 at 11:00
2

That's not the correct syntax for an if-else construct in php.

The elseif part needs to have a condition right after it (before the opening brace), while else doesn't expect a condition at all.

if ($guess/2 %0){
        echo "the number you are guessing is not odd";
} elseif ($guess<$rand) {
    // ....
} else {
    echo "you guessed currectly. now try anouthe number!";
}

Of course, before your else you have to be sure that the if and elseifs match all the "wrong" cases.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95