search
HomeBackend DevelopmentPHP Tutorial第十节 建设一个简单交互的网站(六)_PHP

第十节 建设一个简单交互的网站(六)_PHP

Jun 01, 2016 pm 12:38 PM
bannerhrefinteractionconstructionSimplewebsiterandom

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

9. 简易banner动态更替

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

简易banner动态更替PHP文件(banner.php3):

//
产生随机数
srand((double)microtime()*1000000);
//
04之间取一个数字
$randval = rand(0,5);
//
显示结果
echo "href
=//gophp.heha.net/index.html></SPAN进入php的世界 border=0 src=$randval.gif>";
?>

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

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

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

新的标头文件(header.inc):

//
定义通用页面头部
?>


echo "$<span class="SpellE">MySiteName</span> - $title"; ?>
宋体"; font-size: 12pt; color: #006633; text-decoration: none}
-->


topmargin
=2>


tr>



tr>
tr>

tr>
tr>

tr>
rowspan="3" width="19%">img src="http://gophp.heha.net/test/images/01dc.gif" width="140" height="60"> rowspan="3" width="29%">

//取得乱数种子
srand((double)microtime()*1000000);
//
04之间取一个数字
$randval = rand(0,5);
//
显示结果
switch($randval)
{
case "0";
echo "href
=//gophp.heha.net/index.html>第十节 建设一个简单交互的网站(六)_PHPsrc=$randval.gif>";
break;
case "1";
echo "href=//personal.668.cc/haitang/index.htm>img border=0 src=$randval.gif>";
break;
case "2";
echo "href=//gophp.heha.net/index.html>第十节 建设一个简单交互的网站(六)_PHPsrc=$randval.gif>";
break;
case "3";
echo "href=//gophp.heha.net/index.html>第十节 建设一个简单交互的网站(六)_PHPsrc=$randval.gif>";
break;
case "4";
echo "href=//personal.668.cc/haitang/index.htm>img border=0 src=$randval.gif>";
break;
}
?>










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
What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

What are Traits in PHP ?What are Traits in PHP ?Apr 30, 2025 pm 03:31 PM

PHP traits enable code reuse in single inheritance contexts, offering benefits like reusability and simplified inheritance. They can be effectively combined with traditional inheritance to enhance class flexibility and modularity.

Is PHP supports multiple inheritance ?Is PHP supports multiple inheritance ?Apr 30, 2025 pm 03:30 PM

PHP does not support multiple inheritance but uses interfaces and traits as alternatives to achieve similar functionality, avoiding issues like the diamond problem.

What is inheritance in PHP ?What is inheritance in PHP ?Apr 30, 2025 pm 03:29 PM

Inheritance in PHP allows classes to inherit properties and methods, promoting code reuse and hierarchical organization. Key benefits include reusability, abstraction, and polymorphism. Common mistakes to avoid are overuse of inheritance and ignoring

What are the main error types, and how do they differ?What are the main error types, and how do they differ?Apr 30, 2025 pm 03:28 PM

The article discusses three main error types in programming: syntax, runtime, and logical errors. It explains their causes, prevention strategies, impacts on performance and user experience, and methods for diagnosis and resolution.

How can PHP and HTML interact?How can PHP and HTML interact?Apr 30, 2025 pm 03:27 PM

Article discusses PHP and HTML interaction, best practices for embedding PHP in HTML, dynamic HTML content generation, and recommended development tools.

What is the difference between for and foreach loop in PHP?What is the difference between for and foreach loop in PHP?Apr 30, 2025 pm 03:26 PM

The article discusses the differences between for and foreach loops in PHP, focusing on syntax, usage, control, and performance. Foreach is preferred for array iteration due to simplicity and efficiency, but for loops are better for index-based opera

Explain the importance of Parser in PHP.for eachExplain the importance of Parser in PHP.for eachApr 30, 2025 pm 03:25 PM

The article discusses the crucial role of the PHP parser in script execution, focusing on its tasks in syntax analysis, error handling, and code optimization, and how its efficiency impacts web application performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor