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命令行(cli)下执行PHP脚本文件的相对路径的问题解决方法

在php命令行下执行.php文件时,执行环境的工作目录(getcwd( ))是php命令程序(php.exe)所在目录,所以如果想在文件内使用相对路径时,要先切换当前的工作目录才行。 小...

PHP通过header实现文本文件下载的代码

这就是今天讨论的主要问题。PHP帮助文档里面关于PHP通过header触发下载的说明比较简单,而网上关于此方面的文章也少的可怜,有很多文章都无法实现所需要的效果。今天我也来谈一下这个方面...

PHP中的traits实现代码复用使用实例

PHP5.4后新增traits实现代码复用机制,Trait和类相似,但不能被实例化,无需继承,只需要在类中使用关键词use引入即可,可引入多个Traits,用','隔开。 (1)Trai...

php实现获取近几日、月时间示例

本文实例讲述了php实现获取近几日、月时间。分享给大家供大家参考,具体如下: <?php date_default_timezone_set('Asia/Shangha...

php实现留言板功能(代码详解)

php实现留言板功能(代码详解)

简单的PHP留言板制作 做基础的留言板功能  需要三张表: 员工表,留言表,好友表 首先造一个登入页面: <form action="drcl.php" metho...