Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Php help

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #41112

    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.

    $x = ‘rand int(1,20)’;
    if ($x <= 9 )
    {echo “

    congrats

    “;}
    elseif ($x >= 10 )
    {echo “

    you lose

    “;}
    ?>

    #116113
    __
    Participant

    Your code works perfectly. However, it probably does not do what you intended.

    // 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 “

    congrats”;
    }elseif($x >= 10 ){

    // and never FALSE.
    echo “you lose”;
    }

    You probably meant to do this:

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

    #116162

    Ok thanks a ton!

    #116229
    __
    Participant

    no prob : )

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘CSS’ is closed to new topics and replies.