Home  >  Article  >  Backend Development  >  PHP newbies on the road (10)_PHP tutorial

PHP newbies on the road (10)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:00:56735browse

建设一个简单交互的网站(六)

9. 简易banner动态更替  

  不知大家有没有发现各大站点上的标头广告banner,我们每次访问这些站点时,都会看到不同的广告图标,或者如果你每次刷新页面时,这些广告banner就会不断地随机更替变换。要实现这种效果虽然用javascript也可以达到(象天极网站的动态变换广告banner就是通过调用javascript来实现的),但是如果我们用PHP的话,我们还可以结合数据库来做数据量很大,如每日一题之类的功能。费话少说,让我们立即来看看如何用PHP来实现banner的动态更替功能。  

简易banner动态更替PHP文件(banner.php3):
//产生随机数
srand((double)microtime()*1000000);
//在0和4之间取一个数字
$randval = rand(0,5);
// 显示结果
echo "进入php的世界";
?>  

  我们可以发现,实现的程序非常简单:主要是先利用srand这一初始化随机数产生器产生随机数,再调用rand函数在定义的有效范围内来获取其中一个随机值,最后显示$randval.gif各图片banner,即0.gif、1.gif、2.gif、3.gif或4.gif。为了便于大家理解,我将rand函数的语法及相关说明罗列如下:  
rand
语法:int rand([int min], [int max]);
返回值:整数
函数种类:数学运算
内容说明:本函数用来取得随机值。若没有指定随机数的最大及最小范围,本函数会自动地从0到RAND_MAX中取一个随机数。若有指定min及max的参数,则从指定参数中取一数字。例如rand(38,49)则会从38到49之间取得一个随机值。其中UNIX系统包含49,Win32系统不包含49。值得注意的是为了使随机数的随机率最大,每次在取随机数前最好使用srand()来设定新的随机数。在本例中在用srand()来产生新的随机数时加入了时间因素,执行时以百万分之一的随机率来产生随机数

9.1 我们更改head.inc文件以应用该简易banner动态更替功能,同时还得为不同的广告banner链接到它们对应的网址。

  当然,首先我们必须先准备好用于更换交换的banner图标,同时也给我们的页面标头加上自己网站的徽标(01DC.gif)。

新的标头文件(header.inc):
// 定义通用页面头部
?>


<? echo "$MySiteName - $title"; ?>
















//取得乱数种子
srand((double)microtime()*1000000);
//在0和4之间取一个数字
$randval = rand(0,5);
// 显示结果
switch($randval)
{
case "0";
echo "";
break;
case "1";
echo "";
break;
case "2";
echo "";
break;
case "3";
echo "";
break;
case "4";
echo "";
break;
}
?>



Simple reincarnation ad replacement









www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316989.htmlTechArticleBuilding a simple interactive website (6) 9. Simple banner dynamic replacement I wonder if you have found it on major websites Masthead banner, every time we visit these sites, we will see different...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn