This code will help you to change the background image of a page randomly after refreshing page, using a function
i.e:- rand();
Here rand (1,5); is used to display 1 to 5 images dynamically changes when a user refresh the page
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Random image display after refreshing page using PHP</title>
</head>
<body>
<?php
srand( microtime() * 1000000 );
$num = rand( 1, 5 );
switch( $num )
{
case 1: $image_file = "ritu1.jpg";
break;
case 2: $image_file = "ritu2.jpg";
break;
case 3: $image_file = "ritu3.jpg";
break;
case 4: $image_file = "ritu4.png";
break;
case 5: $image_file = "ritu5.jpg";
break;
}
echo "<img src=images/".$image_file." />";
?>
</body>
</html>
i.e:- rand();
Here rand (1,5); is used to display 1 to 5 images dynamically changes when a user refresh the page
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Random image display after refreshing page using PHP</title>
</head>
<body>
<?php
srand( microtime() * 1000000 );
$num = rand( 1, 5 );
switch( $num )
{
case 1: $image_file = "ritu1.jpg";
break;
case 2: $image_file = "ritu2.jpg";
break;
case 3: $image_file = "ritu3.jpg";
break;
case 4: $image_file = "ritu4.png";
break;
case 5: $image_file = "ritu5.jpg";
break;
}
echo "<img src=images/".$image_file." />";
?>
</body>
</html>
No comments:
Post a Comment