【问题标题】:Cleaner PHP Random Text更干净的 PHP 随机文本
【发布时间】:2010-12-24 15:46:40
【问题描述】:

使用 php 在每次页面刷新时随机生成文本。有没有更清洁的方法来解决这个问题?另外,这可以用jquery来完成吗?

<?php
$random_text = array("Random Text 1",
                "Random Text 2",
                "Random Text 3",
                "Random Text 4",
                "Random Text 5");
srand(time());
$sizeof = count($random_text);
$random = (rand()%$sizeof);
print("$random_text[$random]");
?>

【问题讨论】:

    标签: php jquery random


    【解决方案1】:

    使用array_rand()

    $random_text = array("Random Text 1",
                    "Random Text 2",
                    "Random Text 3",
                    "Random Text 4",
                    "Random Text 5");
    
    print_r($random_text[array_rand($random_text)]);
    

    【讨论】:

    • 请注意,array_rand 返回的是数组的键,而不是值。
    • 是的,应该是 print_r($random_text[array_rand($random_text)]);
    猜你喜欢
    • 2013-10-15
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 2013-06-21
    • 2017-11-13
    相关资源
    最近更新 更多