Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:40:38+00:00 2026-05-28T19:40:38+00:00

I am trying to get a program to reiterate until both the $heads variable

  • 0

I am trying to get a program to reiterate until both the $heads variable and $tails variable is greater than 0. I can’t figure out what I am doing wrong. The while loop always gets broken after just one iteration.

<?php
echo "<table border=\"1\">";
echo "<tr><td>Person</td><td>Heads</td><td>Tails</td><td>Total</td></tr>";



for ($person=1; $person < 11; $person++){
echo "<tr><td>Person $person </td>";
$both = 0;
$heads = 0;
$tails = 0;
$total = 0;
while ($both < 1){
    do {
        $total++;
        $random = rand(1,2);
        if ($random == 1){
            $heads++;
        } else{
            $tails++;
        }  
    } while (($tails < 0)  && ($heads < 0));
    $both = 1;

}
echo "<td>$heads</td><td>$tails</td><td>$total</td>";
echo "</tr>";

}

echo "</table>";

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:40:39+00:00Added an answer on May 28, 2026 at 7:40 pm

    In this line

    } while (($tails < 0)  && ($heads < 0));
    

    it appears that neither $tails nor $heads will ever be strictly less than 0, so that will always be false. Try <= 0 for both.

    Also, logically, you want to loop again if either of those conditions is true, right? So use || instead of &&.

    Result:

    } while (($tails <= 0) || ($heads <= 0));
    

    Also, I’m a little curious about the while ($both < 1) loop. It appears that you assign $both = 0 before the loop, and you assign $both = 1 at the end of its iteration. That would guarantee the loop is only executed once, in which case — what’s the point of having that loop? Perhaps that’s just unfinished code at this point?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out how to get the program to create a
I am trying to figure out how to create a java program that can
I'm trying to get my program to dump out data into an Excel spreadsheet.
Im doing C homework and trying to get this program to run, but I
I'm trying to get my program working without boost usage, but can't find an
I am trying to get the program to call up the current date, add
i'm trying to get this program to break down a user defined amount of
I'm trying to get this simple program to work on windows, but it crashes:
I'm trying to get started with Hibernate, and when executing my program I get
I'm trying to get console input in my Clojure program, but when it gives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.