php - Use function but run it again if it has the same result as function before -
so i've made function random colour. problem im gonna print same function 8 times , dont want 2 of prints return same variable. want run function again if has printed colour before.
function random_color(){ $color_numb = "8"; $color = rand(1, $color_numb); if($color == "1"){ $type = "blue"; } if($color == "2"){ $type = "red"; } if($color == "3"){ $type = "orange"; } if($color == "4"){ $type = "green"; } if($color == "5"){ $type = "yellow"; } if($color == "6"){ $type = "black"; } if($color == "7"){ $type = "purple"; } if($color == "8"){ $type = "white"; } return $type; } print random_color(); print random_color(); print random_color(); print random_color(); print random_color(); print random_color(); print random_color(); print random_color();
so idea how can manage this?
instead of random_color()
function , 8 print
s, try :
$colors = array(); $colors[] = "blue"; $colors[] = "red"; $colors[] = "orange"; $colors[]= "green"; $colors[]= "yellow"; $colors[] = "black"; $colors[]= "purple"; $colors[]= "white"; shuffle($colors); ($i = 0; $i < 8; $i++) { echo $colors[$i] }
Comments
Post a Comment