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

yipeiwu_com5年前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代码(抓取网页中的邮箱地址)

复制代码 代码如下: <?php $url='//www.jb51.net'; //这个网页里绝对含有邮件地址。 $content=file_get_contents($url);...

来自phpguru得Php Cache类源码

Cache的作用不用说大家都知道咯,这些天也面试了一些人,发现很多人框架用多了,基础都忘记了,你问一些事情,他总是说框架解决了,而根本不明白是怎么回事,所以也提醒大家应该注意平时基础知识...

Drupal7 form表单二次开发要点与实例

请记得收藏此文,在你进行Drupal 7 custom module时,经常会用到的form 表单的跳转或重载。主要汇总三个要点: 1.页面提交后,经过#submit处理后,需要redi...

PHP对象Object的概念 介绍

例如,员工管理应用程序可能包括一个EmPloyee 类。然后可以用这个类来创建和维护特定实例,比如Gonn和Sally。 根据预定义的类创建对象常称为类的实例化(class instan...

php+html5基于websocket实现聊天室的方法

本文实例讲述了php+html5基于websocket实现聊天室的方法。分享给大家供大家参考。具体如下: html5的websocket 实现了双向通信,折腾了几天弄了个聊天室,分享给大...