pTutorial

New way to learn PHP Example

How To Change Background Automatically

Change background as per day

In this section, we will learn how to create a PHP script that change our web page background daily.

Change background of your web page using else-if automatically


<?php

$current_day = date("l");
print("Today is "."$current_day");
if($current_day == "Sunday") 
{
$bgcolor = "#FF66FF ";
}
elseif($current_day == "Monday")
{
$bgcolor =" #9900FF";
} 
elseif($current_day == "Tuesday") 
{
$bgcolor = "#CC6666";
} 
elseif($current_day == "Wednesday")
{
$bgcolor = "#FFCC33";
} 
elseif($current_day == "Thursday")
{
$bgcolor = "#E9FFE6";
} 
elseif($current_day == "Friday") 
{
$bgcolor = "#FFFF00" ;
}
 
else 
{
$bgcolor = "red";
}
print("<body bgcolor=\"$bgcolor\">\n>"); 


?>

Explanation

In this example first store the current day using the date function on the $current_day variable and print the current day after that set the different colours for different days using if else statements on the $bgcolor variable and at the end print the $bgcolor variable.

According to the day colour assign to the variable, because of elseif condition only one condition will execute.

Change background using switch case statement

<?php

$today = date("l");
echo"Today is "."$today";

	switch($today)
	{
		    case "Sunday":
			$bgcolor = "#FF66FF ";
			break;
		    case "Monday":
			$bgcolor =" #9900FF";
			break;
		    case "Tuesday":
			$bgcolor = "#CC6666";
			break;
		    case "Wednesday":
			$bgcolor = "#FFCC33";
			break;
            case "Thursday":
			$bgcolor = "#E9FFE6";
			break;
		    case "Friday":
			$bgcolor = "#FFFF00" ;
			break;
		    case "Saturday":
			$bgcolor = " #FFCCFF";
			default:
			$bgcolor = "blue";	
    }
    print("<body bgcolor='$bgcolor'>"); 


?>



Explanation

This is the easiest method as compared to the previous one. Switch case is used where a lot of condition available.

In this example, first store the current day using the date function on the $current_day variable and print the current day. After that eight cases are there seven for seven days and one for error (default case), all cases have the same variable to assign the colour and at last print the variable $bgcolor.

According to the day colour will assign to the variable and change the background.


QUOTES OF THE DAY

Everything flows, nothing stays still.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER