Trying to find my mistake in code

been working on it all day but can not get it to work, thx for any help

<?php
$guess = 7;
$number = 7;

if ($guess < $number) {
echo “too low!”;
}
elseif ($guess > $number) {
echo “too high!”;
}
else { ($guess == $number)
echo “you win!”;
}
?>

I have not tried it but the else is the default and will not need any more than the echo:

<?php
 $guess = 7;
 $number = 7;

 if ($guess < $number) {
 echo "too low!";
 }
 elseif ($guess > $number) {
 echo "too high!";
 }
 else { echo "you win!"; }
 ?>

As @Rubble mentioned the else does not require any tests or curly brackets.

Try this:


$guess = 7;
$number = 7;

   if ($guess < $number)
   {
      echo "too low!";
   }
   elseif ($guess > $number)
   {
      echo "too high!";
   }
   else 
   {  // ($guess == $number) 
      echo "you win!";
   }


I have started using PHP Alternative Syntax: because it is shorter and far easier than checking curly brace closures.:
http://php.net/manual/en/control-structures.alternative-syntax.php



   $guess   = 7;
   $number = 7;

   if ($guess < $number):
      echo "too low!";
   elseif ($guess > $number):
      echo "too high!";
   else: 
      echo "you win!";
   endif;


Take a look also at using while/endwhile , for/endfor, foreach/endforeach, switch/case/endswitch, etc .

Ternary Operators:


   $guess   = 7;
   $number = 7;
 
   echo ($guess < $number) ? "too low!" :  ($guess > $number) ? "too high!" : "You Win!!!";


Also although this works it does take far more effort to easily see at a glance what is happening and also debugging is more difficult:

Thx for all your help, I see what you are talking about. I am working through code academy, trying to get a handle on PHP, thx again

echo ($guess < $number) ? “too low!” : ($guess > $number) ? “too high!” : “You Win!!!”;

I was wondering if you could do this!!!
I use ternary operators all the time for true/false things but end up going with IF statements when three or more checks are needed. GOOD TO KNOW.

This doesn’t do what you think it does !!!

For some reason the ternary operator in PHP is left associative while it really should be right associative (like it is in all other languages…). Meaning PHP interprets this code as


$guess = 7;
$number = 8;

echo (($guess < $number) ? "too low!" :  ($guess > $number)) ? "too high!" : "You Win!!!";
//   ^                                                     ^ <<--- parentheses added 

(note the added parentheses that indicates how PHP interprets this code)

so it first evaluates


(($guess < $number) ? "too low!" :  ($guess > $number))

Which results in the string “too low!” for this example, which is then cast to a boolean for the second ternary operator, so it becomes true, and the result of this is “too high!”, while the expected output in this case would be “too low!”

while what you meant was:


echo ($guess < $number) ? "too low!" :  (($guess > $number) ? "too high!" : "You Win!!!");

(note position of parentheses!)

To fix this, add parentheses like in my last code block.

To confirm, see http://3v4l.org/bpPkt

That’s one reason why I restrict use of ternary operators to single checks (if/then/else). Plus nested ternary operators becomes a bind to interpret whereas single checks are quite easy. :slight_smile:

Many thanks for the detailed explanation.

In future where three results are possible I will endeavour to format my script using this style:



 echo ($guess<$number)  
         ? $tooLow 
         : ( ($guess>$number) ? $tooHigh : $youWin );