0

Can anyone help me understand how to pass a variable through a for loop? I want to print out variable $guess until the number 40 & print out either "you guessed to low" or "you guessed too high"...how do I connect the variable with the counter??

$Me=30;
$guess=$i;

for ($i = 0; $i < 40; $i++) {
    if($Me>$guess){
    print "$guess:you guessed too low<br />";
    }elseif($Me<$guess){
    print "$guess: you guessed to high<br />";
    }else{
    print "you guessed my age! i'm 31!";
    }
}

4 Answers 4

1
$Me=30;

for ($i = 0; $i < 40; $i++) {
$guess=$i; // move it here
    if($Me>$guess){
    print "$guess:you guessed too low<br />";
    }elseif($Me<$guess){
    print "$guess: you guessed to high<br />";
    }else{
    print "you guessed my age! i'm 31!";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

In the for loop replace $i with $guess.

However, the program will print "i'm 31" although $Me=30.

Comments

1
$Me=30;

for ($i = 0; $i < 40; ) {
     $guess=$i;
    if($Me>$guess){
        print "$guess:you guessed too low<br />";
    }elseif($Me<$guess){
        print "$guess: you guessed to high<br />";
    }else{
        print "you guessed my age! i'm 31!";
    }
$i++;
}

Comments

1
$Me=30;
/* actually you don't need to use $guess=$i; for your needs */ 
for($i = 0; $i < 40; $i++) {
    if($i < $Me){
        echo "$i:you guessed too low <br/>";
    }
    else if($i > $Me){
        echo "$i: you guessed to high<br/>";
    }
    else{
        echo "you guessed my age! i'm 31!";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.