Avatar of EagleJess
EagleJess
 asked on

Can you tell me why I get this error please.?

17,16,21,2,5,13,11,10,7,14,8,0,18,20,15,1,4,3,6,9,12,19
Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/test_qu2.php on line 3

My code is:
<?php
   function contain($boxValues, $number) {
     for ($k=0; $k<sizeof($boxValues); $k++) {
       if ($boxValues[$k] == $number)
            return true;
     }
       return false;
   }
 
   $num[0] = rand(1,21);
   echo $num [0];
   $boxValues[0] = $num[0];
   for ($i=1; $i<=22; $i++) {      
      $num[$i] = rand(0,21);      
      while (contain($boxValues, $num[$i])) {          
         $num[$i] = rand (0,21);      
      }      
    $boxValues[$i] = $num[$i];    //append into the array  
   
      echo "," .$num[$i];
      }  
     
            
?>
PHP

Avatar of undefined
Last Comment
EagleJess

8/22/2022 - Mon
Chris Harte

You are filling the array, soon it will contain every integer between 1 and 21. Then the while loop will just keep running because it will always be true.
Ray Paseur

Maybe step back from the technical details of the code and just tell us in plain language what you want to accomplish here?  There are almost certainly built-in PHP functions to do these things.  Thanks.
Julian Hansen

It looks like you are trying to randomize an array.

Why not use the shuffle function http://php.net/manual/en/function.shuffle.php

<?php
$result = range(0,21);
shuffle($result);
var_dump($result);

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur

@julian: That's what I was thinking, too.  But I wasn't sure about value constraints, or uniqueness, etc.  Maybe we can get a bit more explanation.
EagleJess

ASKER
Hi both.

I want to create 22 non-repeating numbers in the range 0-21 and store them in an array.  They have to be displayed in the array using comma separation.  Thanks.
Ray Paseur

Please see: http://iconoun.com/demo/temp_eaglejess_contain.php

If you do not want the numbers to be in random order, remove the shuffle() statement.
<?php // demo/temp_eaglejess_contain.php
/**
 * http://www.experts-exchange.com/questions/28938272/Can-you-tell-me-why-I-get-this-error-please.html
 *
 * http://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
 */
error_reporting(E_ALL);

// MAKE AN ARRAY OF UNIQUE RANDOM NUMBERS BETWEEN ZERO AND 21
$nums = range(0,21);
shuffle($nums);

// SHOW THE ARRAY
$str = implode(',', $nums);
echo $str;

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

And since the next step in learning about arrays involves finding things in arrays, array_search() may be helpful.  All of the PHP array functions are documented in the online man pages linked here:
http://php.net/manual/en/ref.array.php
<?php // demo/temp_eaglejess_contain.php
/**
 * http://www.experts-exchange.com/questions/28938272/Can-you-tell-me-why-I-get-this-error-please.html
 *
 * http://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
 * http://php.net/manual/en/ref.array.php
 */
error_reporting(E_ALL);

// MAKE AN ARRAY OF UNIQUE RANDOM NUMBERS BETWEEN ZERO AND 21
$nums = range(0,21);
shuffle($nums);

// SHOW THE ARRAY
$str = implode(',', $nums);
echo $str;

// FIND THE LOCATION (KEY) FOR A GIVEN NUMBER
$num = 7;
$key = array_search($num, $nums);
echo PHP_EOL . "The $num is at position $key";

Open in new window

EagleJess

ASKER
Hi Ray
By removing the shuffle the numbers appear to be non random.  I thought i would have to use the rand function.
Ray Paseur

By removing the shuffle the numbers appear to be non random.
'Zackly! :-)

These are two different functions.  Have a quick look at the PHP man pages.
http://php.net/manual/en/function.shuffle.php
http://php.net/manual/en/function.rand.php

Writing our own sort or randomization functions is interesting as an academic exercise, but nobody would do that today because the built-in functions are faster and are already debugged.
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
EagleJess

ASKER
Thank for your help guys.  Interesting to see the many takes on the question.