Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Php Question

Options
  • 03-02-2013 11:56am
    #1
    Closed Accounts Posts: 446 ✭✭


    Hi all

    Doing php in college and have been asked to create a coin toss program whereyou win if you get two heads in a row. Thought I had it, but I keep getting the"Undefined offset: 0 on line 16" error which seems to mean that that$check is getting decremented even when $i is not greater than two, which meansthe ‘if’ is getting ignored, but why?

    Sorry if this a real basic question, only started programming, still trying to get my head around the logic, have to say I’m am loving itthough.


    [php]
    <?php
    $tosses = array();
    $numTosses = 4;
    for($i = 1;$i <= $numTosses; $i++) {
    $coin = rand(0,1);
    if ($coin == 0)
    {
    $tosses[$i] = "Heads";
    if ($i >= 2); //seems to be ignored
    {
    $check = $i - 1;
    if($tosses[$check] == "Heads")
    {
    echo "You got two heads in a row, you won";
    exit();
    }
    }
    }

    else {
    $tosses[$i] = "Tails";
    }

    }
    echo "Sorry didnt win this time";

    ?>

    [/php]


Comments

  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    Devi wrote: »
    [php]
    if ($i >= 2); //seems to be ignored
    [/php]
    It's the semicolon. Remove it...


  • Closed Accounts Posts: 446 ✭✭Devi


    Sparks wrote: »
    It's the semicolon. Remove it...
    oh dear lord, how did I not see that. Thanks Sparks.


Advertisement