php 自动变换随机问候语的简单示例

预先定义一个PHP数组,里面存放一些随机问候语,调用的时候指定是按照天,月还是年来自动更换问候语,如果选择月,则会每月更换一条问候语显示,不用每个月手动更换了,并且这段PHP代码比使用JS实现对搜索引擎友好,PHP自动变换的随机问候语的实现方法,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

/**
 * 自动更换问候语
 *
 * @param 
 * @arrange (编程之家) jb51.cc
 **/
function RandomQuoteByInterval($TimeBase,$QuotesArray){
 
    // Make sure it is a integer
    $TimeBase = intval($TimeBase);
 
    // How many items are in the array?
    $ItemCount = count($QuotesArray);
 
    // By using the modulus operator we get a pseudo
    // random index position that is between zero and the
    // maximal value (ItemCount)
    $RandomIndexPos = ($TimeBase % $ItemCount);
 
    // Now return the random array element
    return $QuotesArray[$RandomIndexPos];
}
 
/*
** --> See the example section below for a
**     detailed instruction.
*/

//使用范例:
// Use the day of the year to get a daily changing
// quote changing (z = 0 till 365)
$DayOfTheyear = date('z'); 
 
// You Could also use:
//  --> date('m'); // Quote changes every month
//  --> date('h'); // Quote changes every hour
//  --> date('i'); // Quote changes every minute
 
// Example array with some random quotes
$RandomQuotes = array(
    'No animals were harmed in the making of this snippet.','Nice snippets','The modulus operator rocks!','PHP is cool.'
);
 
print RandomQuoteByInterval($DayOfTheyear,$RandomQuotes);
 
// --> returns PHP is cool. on September 25,2007


/***   来自编程之家 jb51.cc(jb51.cc)   ***/

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...