<\/script>')

PHP randomly selects from a list

I am currently working with PHP code that randomly selects colors:

<div onclick="location.href='<?php the_permalink() ?>';" 
    style="cursor:pointer;background:#<?php 
        echo rand(0, 9); ?><?php 
        echo rand(0, 9); ?><?php 
        echo rand(0, 9); ?><?php 
        echo rand(0, 9); ?><?php 
        echo rand(0, 9); ?><?php 
        echo rand(0, 9); ?>;" 
    class="post bg thickbox" 
    id="thickbox post-<?php the_ID(); ?>">

What I would like to do is define a list of preferred colors in a single PHP file, and then randomly try an element from this list in the above code.

What is the correct PHP code for randomly selecting such color lists? How do you define a list of colors?

+3
source share
3 answers

I would like most to suggest, define your colors as an array in one php file:

$colors = array("red", "blue", "#00ff00");

And then use array_rand to select one:

...background:<?= $colors[array_rand($colors, 1)] ?>;" class=...
+15
source

, rand (0, to) .

0
<?php

function getRandomColor(){
    $a = array('#ff5500', '#000066', '#555555');
    $indice = rand(0, count($a)-1);
    return $a[$indice];
}
0

Source: https://habr.com/ru/post/1759198/


All Articles