</html> If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:

<html>

<body>

<?php

$d=date("D");

if ($d=="Fri")

{

echo "Hello!

";

echo "Have a nice weekend!";

echo "See you on Monday!";

}

?>

</body>

</html>

The ElseIf Statement

If you want to execute some code if one of several conditions are true use the elseif statement

Syntax

if (condition)

code to be executed if condition is true;

elseif (condition)

code to be executed if condition is true;

else

code to be executed if condition is false; Example

The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":

<html>

<body>

<?php

$d=date("D");

if ($d=="Fri")

echo "Have a nice weekend!";

elseif ($d=="Sun")

echo "Have a nice Sunday!";

else

echo "Have a nice day!";

?>

</body>

</html> The Switch Statement

If you want to select one of many blocks of code to be executed, use the Switch statement.

The switch statement is used to avoid long blocks of if..elseif..else code.

Syntax

switch (expression)

{

case label1:

code to be executed if expression = label1;

break;

case label2:

code to be executed if expression = label2;

break;

default:

code to be executed

if expression is different

from both label1 and label2;

} Example

This is how it works:

A single expression (most often a variable) is evaluated once

You've reached the end of published parts.

⏰ Last updated: Jan 14, 2010 ⏰

Add this story to your Library to get notified about new parts!

PHP-languageWhere stories live. Discover now