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程序设计有所帮助。

相关文章

使用PHPExcel导出Excel表

本文实例为大家分享了PHPExcel导出Excel表的具体代码,供大家参考,具体内容如下 /** * Excel导出 * @param $fileName(文件名)...

PHP7新特性之抽象语法树(AST)带来的变化详解

本文分析了PHP7新特性之抽象语法树(AST)带来的变化。分享给大家供大家参考,具体如下: 这里大部分内容参照 AST 的 RFC 文档而成:https://wiki.php.net/r...

php常用字符串查找函数strstr()与strpos()实例分析

本文实例讲述了php常用字符串查找函数strstr()与strpos()。分享给大家供大家参考,具体如下: 一句话使用strpos判断 ===或!==,这样才能达到预期的效果,性能要比s...

PHP 杂谈《重构-改善既有代码的设计》之五 简化函数调用

PHP 杂谈《重构-改善既有代码的设计》之五 简化函数调用

思维导图 介绍   前几篇系列文章,我比较关注的是<PHP 杂谈《重构-改善既有代码的设计》之一 重新组织你的函数>,但是我觉得我还是没有说清楚,我自己也有很多不理解的地方,...

PHP sprintf()函数用例解析

复制代码 代码如下: <?php //sprintf()函数,返回值为格式化后的字符串 string sprintf ( string $format [, mixed $args...