Random background color from array - PHP -
i beginner when comes programming wanted see if right way code this. trying generate random background color array.
if there i'm missing or there better please let me know.
<?php $background_colors = array('#282e33', '#25373a', '#164852', '#495e67', '#ff3838'); $count = count($background_colors) - 1; $i = rand(0, $count); $rand_background = $background_colors[$i]; ?> <html> <head> </head> <body style="background: <?php echo $rand_background; ?>;"> </body> </html>
that's pretty good.
however, i'd array_rand()
...
$background_colors = array('#282e33', '#25373a', '#164852', '#495e67', '#ff3838'); $rand_background = $background_colors[array_rand($background_colors)];
it less code , more readable imo.
Comments
Post a Comment