Jump to content

if ... else random


manalnor

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/198142-if-else-random/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/198142-if-else-random/#findComment-1039622
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/198142-if-else-random/#findComment-1039625
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/198142-if-else-random/#findComment-1039634
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.