Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Secret Santa Script

Featured Replies

Anyone got any idea why..

  1. My script doesn't work the way it should.
  2. It makes my laptop *really* hot, like there's an infinite loop in there or something?

<?php  
   function isOdd($num){
 $num = $num % 2;
 return $num;
   }

   function pair($names) {
 foreach ($names as &$name) {

  // So I got the first one.. I need to pair it with a second..
  $count = count($names)-1;
  $random = rand(1, $count);
  $match = $names[$random];

  while ($name == $match) {
   $random = rand(1, $count);
  }

  if ($name != $match && $match != "") {
   echo "[$count] $name and $match sitting in a tree<br/>";
   unset($names[$random]);
   unset($name);
  }
 }
   }

   $names = array("Lewis", "Steve", "Shelley", "Cal", "Danny", "Ruth", "Barry", "Nyall", "Scott", "Kerrie", "Carly", "Ray");
   $count = count($names);

   if (isOdd($count)) {
 echo "You can't have an odd number of people.";
   } else {
 echo "Let's crack on!";
   }

   pair($names);


  ?>

	 while ($name == $match) {
	   $random = rand(1, $count);
	  }

 

This loop was the first I saw,

and it looks like there isn't anything being changed to the condition.

So this loop never stops ?

 

Léon

  • Author

Hmm, the point of that is, I can't have the $name = $match.

 

I don't want to be able to draw myself when running the script.

 

How would I go about that?

I think this will fix it:

 

 while ($name == $match) {
	   $random = rand(0, $count);
	   $match = $names[$random];
	  }

 

Als you might want to use 0 instead of 1 in the random function:

$random = rand(0, $count);

Otherways the first name of the array is never used.

 

Léon

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.