treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Php help

  • can someone tell me whats wrong with this php script? every time i go to the page i get a "congrats" i never get the other event.

    <?php $x = 'rand int(1,20)'; if ($x <= 9 ) {echo "<p>congrats";} elseif ($x >= 10 ) {echo "

    you lose

    ";} ?>
  • Your code works perfectly. However, it probably does not do what you intended.

      <?php
      // you defined $x as a _string_.
      // "rand int(1,20)" <-- not a function. 
      // just a sentence.
      $x = 'rand int(1,20)';
    
      // here, you cast the string to an integer
      // (because you're comparing it to an integer).
      // 'rand ...' equates to (0).
      if ($x <= 9 ){
          // therefore, 
          // the comparison is always TRUE
          echo "<p>congrats";
      }elseif($x >= 10 ){
    
          // and never FALSE.
          echo "you lose";
      }
    

    You probably meant to do this:

      <?php
          // no quotes or "int"
          $x = rand( 1,20 );
    

    The rest of your code should work as expected after that.

    (Though you don't need the elseif; you could use just else and get the same result.)

  • Ok thanks a ton!

  • no prob : )