manalnor Posted April 9, 2010 Share Posted April 9, 2010 Hello dear friends, Let say we have files red.php orange.php blue.php yellow.php here is the main question if ($any == 1) { require_once("red.php"); } else { xxxxxxxxxxxxxxxxxxxxxxxx } how to make it else ( show random files ) i mean instead of xxxxxxxxxxxxx it will pick up any file in random way. how to write this ! thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/198142-if-else-random/ Share on other sites More sharing options...
JAY6390 Posted April 9, 2010 Share Posted April 9, 2010 $files = glob('*.php'); require($files[array_rand($files)]); Quote Link to comment https://forums.phpfreaks.com/topic/198142-if-else-random/#findComment-1039615 Share on other sites More sharing options...
ddubs Posted April 9, 2010 Share Posted April 9, 2010 $colors = Array('red.php', 'blue.php', 'orange.php', 'yellow.php'); $max = count($colors) - 1; $rand = rand(0, $max); echo "Random Color: {$colors[$rand]}"; Careful on Jay's method - you could grab any php file. Quote Link to comment https://forums.phpfreaks.com/topic/198142-if-else-random/#findComment-1039620 Share on other sites More sharing options...
JAY6390 Posted April 9, 2010 Share Posted April 9, 2010 True, I did think that myself, but it was assuming that all the files are in a separate folder with the .php extension. If they aren't then go with a normal array method or give your files a certain suffix such as red.color.php then search for *.color.php instead of just *.php Quote Link to comment https://forums.phpfreaks.com/topic/198142-if-else-random/#findComment-1039622 Share on other sites More sharing options...
manalnor Posted April 9, 2010 Author Share Posted April 9, 2010 thanks so much for both ways and i understood that Jay method will take any *.php file with it but will be fine is the files in /templates dir. one last question, is that possible to be in that way if ($wpic == red) { require_once("red.php"); } else if($wpic == blue) { require_once("blue.php"); } else if($wpic == yellow) { require_once("yello.php"); } else { xxxxxxx ddubs code for random xxxxxxxx } i mean is it possible to use chain of (else if) i mean many times. Quote Link to comment https://forums.phpfreaks.com/topic/198142-if-else-random/#findComment-1039625 Share on other sites More sharing options...
ddubs Posted April 9, 2010 Share Posted April 9, 2010 Yea thats fine, or you can research "switch" switch($wpic) { case $wpic == 'red' : require_once("red.php"); break; case $wpic == 'blue' : require_once("blue.php"); break; ...etc default : //insert random code here... } Quote Link to comment https://forums.phpfreaks.com/topic/198142-if-else-random/#findComment-1039632 Share on other sites More sharing options...
Psycho Posted April 9, 2010 Share Posted April 9, 2010 i mean is it possible to use chain of (else if) i mean many times. Yes, but in this case it is inefficient $colors = array('red', 'blue', 'yellow'); if (in_array(strtolower($wpic), $colors)) { $require_file = strtolower($wpic) . '.php'; } else { $require_file = $colors[array_rand($colors)] . '.php'; } require_once($require_file); Quote Link to comment https://forums.phpfreaks.com/topic/198142-if-else-random/#findComment-1039634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.