php实现每天自动变换随机问候语的方法

5年以前  |  阅读数:578 次  |  编程语言: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分页显示制作详细讲解
SSH 登录失败:Host key verification failed
将二进制数据转为16进制以便显示
获取IMSI
获取IMEI
Java生成UUID
PHP自定义函数获取搜索引擎来源关键字的方法
让你成为最历害的git提交人
在Zeus Web Server中安装PHP语言支持
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Yii2汉字转拼音类的实例代码
Python 2与Python 3版本和编码的对比
php+ajax+json 详解及实例代码
php封装的page分页类完整实例
PHP设计模式之工厂模式与单例模式
php数组合并array_merge()函数使用注意事项
PHP实现简单爬虫的方法
php实现数组中索引关联数据转换成json对象的方法
wget使用技巧