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+ajax实现获取新闻数据简单示例

PHP+ajax实现获取新闻数据简单示例

本文实例讲述了PHP+ajax实现获取新闻数据的方法。分享给大家供大家参考,具体如下: Get方式获取到的信息是字符串(responseText) ① 可以借助JSON对象的方法:str...

php项目开发中用到的快速排序算法分析

本文实例讲述了php项目开发中用到的快速排序算法。分享给大家供大家参考,具体如下: 实际上在,做web开发,比较少遇到使用一些算法之类的,毕竟不是做搜索引擎,也不是写底层(比如写个类似于...

php基于SQLite实现的分页功能示例

本文实例讲述了php基于SQLite实现的分页功能。分享给大家供大家参考,具体如下: 这里操作数据库文件使用的是前面文章《PHP基于PDO实现的SQLite操作类【包含增删改查及事务等操...

PHP程序员学习使用Swoole的理由

PHP程序员学习使用Swoole的理由

最近两个月一直在研究 Swoole,研究成果即将在6.21正式开源发布,这段时间没有来水文章,趁着今天放假来水水吧。 借助这篇文章,我希望能够把 Swoole 安利给更多人。虽然 Swo...

php网站被挂木马后的修复方法总结

本文实例总结了php网站被挂木马后的修复方法。分享给大家供大家参考。具体方法如下: 在linux中我们可以使用命令来搜查木马文件,到代码安装目录执行下面命令 复制代码 代码如下:find...