从两个已知单词生成随机单词


Generate random word from two known words

我可以对数字这样做:

<?=number_format(mt_rand(0,1));?>

我想做的是,代替0或1,回显单词firstclasssecondclass

这是因为我不能在CSS中使用数字作为类标识符。

本质上,这只是为了在列表中显示随机的内容,并在类标识符前面加上firstclass或secondclass。

.firstclass {display:none;}

我不擅长PHP,所以我想我需要设置一个数组,并以某种方式属性:

0 = firstclass
1 = secondclass

,这样我可以让我的小测试脚本工作。

有什么建议吗?

<?php echo (mt_rand(0,1) == 0 ? 'firstclass' : 'secondclass'); ?>

像这样?:

$words = array('firstclass', 'secondclass');
$randomWord = $words[mt_rand(0,1)];

或者…

class="a-prefix-<?=number_format(mt_rand(0,1));?>"

CSS类必须以下划线、破折号或字母开头,但后面可以有数字

如果只有两种可能,可以使用三元格式的if语句:

<?=(mt_rand(0,1) == 0) ? 'firstclass' : 'secondclass'?>

如果你想说0 = firstclass, 1 = secondclass为什么不像这样使用array_rand:

$classes = array(
    'firstclass',
    'secondclass'
);
$randClass = $classes[array_rand($classes)];
echo $randClass;

如果需要的话,还可以添加更多的类