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实现二维数组按某列进行排序的方法。分享给大家供大家参考,具体如下: /* * 二维数组 按某列排序 * array_multisort($arr1,$arr2)...

php魔术方法与魔术变量、内置方法与内置变量的深入分析

php内置变量了:DIRECTORY_SEPARATORDIRECTORY_SEPARATOR是一个返回跟操作系统相关的路径分隔符的php内置命令,在windows上返回/,而在linu...

PHP使用preg_split()分割特殊字符(元字符等)的方法分析

本文实例讲述了PHP使用preg_split()分割特殊字符(元字符等)的方法。分享给大家供大家参考,具体如下: 这里所说的特殊字符就是正则中使用的特殊字符,如: | . + 等 其它的...

详解php的socket通信

详解php的socket通信

 对 TCP/IP 、 UDP 、 Socket 编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵。 那什么是TCP/IP、UDP? TCP/IP(Tran...

php中的抽象方法和抽象类

1、什么是抽象方法? 我们在类里面定义的没有方法提的方法就是抽象方法。所谓的没有方法体指的是,在声明的时候没有大括号以及其中的内容,而是直接在声明时在方法名后加上分号结束,另外在声明抽象...