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 7526623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:53:00+00:00 2026-05-30T03:53:00+00:00

I recently started a little project in PHP, I’m sort of new to this

  • 0

I recently started a little project in PHP, I’m sort of new to this and this may be a beginner question but I can’t find any help so I’m asking here.

I am working on a card game and I want to give up to six players random cards. I store how many cards everyone gets in $cardstoget and how many players there are in $player. I have an array $usedcards with values like [0] => 0, [1] => 0 and so on. I determine if the card is already used by putting it to 1 instead of 0 like this: [1] => 1. There are 80 cards and I plan on having [0] filled with the number of unused cards left.

I use this code:

for ($i = 1;$i <= $player;$i++) {
 for ($i2 = $cardstoget;$i2 > 0;$i2--) {
  do {
   $temp = mt_rand(1,80);
  } while ($usedcards[$temp] = 1);
  ${"cards".$i} = ${"cards".$i} . $temp;
  $usedcards[$temp] = 1;
 }
}

When I execute the code I get this error message:

Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/create.php on line 47

So now to my question: How can I change the do-while loop to make it to something similar but faster? I know this loop is the problem, I tried it without and it worked fine,but I can’t know it this card is already on someone’s hand.

I hope somebody out there can help me and any help is highly appreciated.

  • 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-30T03:53:02+00:00Added an answer on May 30, 2026 at 3:53 am

    Your immediate problem is the $usedcards[$temp] = 1 condition. You’re assigning 1 here, which results in the condition always being true, and the loop looping endlessly.

    More generally, you’ll want a deterministic algorithm, not one whose completion depends on randomness. Something like:

    $cards = range(1, 80);
    shuffle($cards);
    
    for ($i = 0; $i < $cardsPerPlayer; $i++) {
        foreach ($players as &$player) {
            $player['cards'][] = array_shift($cards);
        }
        unset($player);
    }
    

    This mimics a real shuffling-and-distribution of cards as you’d do in a real game.

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

Sidebar

Related Questions

I am little more than beginner PHP programmer and just recently started to use
I recently started to think of this problem and I can't find the answer.
Ok, so I recently started learning jQuery, but I'm having trouble with a little
I recently started using JQuery and created a little slider to display logos of
I've recently started dabbling with some time-series analysis in R, and I'm a little
Recently I have started learning rails and was a little surprised that the default
I recently started working with a client that had an index.html file with this
I recently started a project in Symfony2 and I need to run some methods
I recently started a project were I wanted the background image to scale to
I have recently started following the examples from The Little Schemer and when trying

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.