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中的注释、变量、数组、常量、函数应用介绍

php中单引号与双引号的区别? 1. 由下可看出,双引号的变量是解析并输出,而单引号的变量不解析。 2.单引号的解析速度比双引号的快 3. 对于单引号来说,只有两个转义 \',\\ 4....

ThinkPHP开发框架函数详解:C方法

C方法是ThinkPHP用于设置、获取,以及保存配置参数的方法,使用频率较高。 了解C方法需要首先了解下ThinkPHP的配置,因为C方法的所有操作都是围绕配置相关的。ThinkPHP的...

php 备份数据库代码(生成word,excel,json,xml,sql)

单表备份代码:复制代码 代码如下:<?php    class Db    {    &n...

php中防止恶意刷新页面的代码小结

防止恶意刷页面的原理是 要求在页面间传递一个验证字符串, 在生成页面的时候 随机产生一个字符串, 做为一个必须参数在所有连接中传递。同时将这个字符串保存在session中。 点连接或者表...

php 删除目录下N分钟前创建的所有文件的实现代码

复制代码 代码如下:<?php//delfile("upload",10);function delfile($dir,$n) //删除当DIR路径下N分钟前创建的所有文件;{if...