pTutorial

New way to learn PHP Example

How To Change Image Automatically In PHP

Change image automatically

In this section we will discuss about how to change image automatically using switch case.

Change image using PHP switch case


<?php

$a=rand(1,5);
	switch($a)
	{	    case 1:
			$greet2 ="../../images/founder-of-php.jpg";
			break;
			case 2:
			$greet2 ="../../images/linkedin-logo.png";
			break;
			case 3:
			$greet2 ="../../images/Pinterest-logo.png";
			break;
			case 4:
			$greet2 ="../../images/youtube-icon.png";
			break;
			case 5:
			$greet2 ="../../images/rem1.jpg";
	}
			
 echo" <img src='$greet2' height='300' width='300'>";

?>

Explanation

Firstly create a random number and assign to the variable $a and after that put the variable into switch case.

There have five case and each have unique image address that store the variable $greet2 and at the end print the variable $greet2. When you refresh your page image will change automatically.

Height="400" width="400" specify the size of image.


QUOTES OF THE DAY

Beauty is truth, Truth beauty- That is all

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER