php实现每天自动变换随机问候语的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php实现每天自动变换随机问候语的方法。分享给大家供大家参考。具体分析如下:

这里预先定义一个php数组,里面存放一些随机问候语,调用的时候指定是按照天,月还是年来自动更换问候语,如果选择月,则会每月更换一条问候语显示,不用每个月手动更换了,并且这段php代码比使用JS实现对搜索引擎友好

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);

希望本文所述对大家的php程序设计有所帮助。

相关文章

php中Redis的应用--消息传递

php中Redis的应用--消息传递

阅读目录 1、摘要 2、实现方法 3、一对一消息传递 4、多对多消息传递 1、摘要 消息传递这一应用广泛存在于各个网站中,这个功能也是一个网站必不可少的。常见的消息传递应用有,新浪微博中...

php环境套包 dedeampz 伪静态设置示例

复制代码 代码如下: <Directory /> Options Indexes FollowSymLinks AllowOverride all Order allow,d...

PHP性能测试工具xhprof安装与使用方法详解

本文实例分析了PHP性能测试工具xhprof安装与使用方法。分享给大家供大家参考,具体如下: xhprof概述: XHProf是一个分层PHP性能分析工具。它报告函数级别的请求次数和各种...

PHP判断IP并转跳到相应城市分站的方法

本文实例讲述了PHP判断IP并转跳到相应城市分站的方法。分享给大家供大家参考。具体实现方法如下: <?php class QQWry{ var $Start...

解析PHP处理换行符的问题 \r\n

一首先说说 \r 与\n的区别回车”(Carriage Return)和“换行”(Line Feed)这两个概念的来历和区别。在计算机还没有出现之前,有一种叫做电传打字机(Teletyp...