PHP If…Else Statements

2012 年 9 月 25 日6750

PHP If...Else Statements

« Previous

Next Chapter »


Conditional statements are used to perform different actions based on different conditions.


Conditional Statements

Very often when you write code, you want to perform different actions for
different decisions.

You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

if statement - use this statement to execute some code only if a specified condition is true
if...else statement - use this statement to execute some code if a condition is true and another
code if the condition is false
if...elseif....else statement - use this statement to select one of
several blocks of code to be executed
switch statement - use this statement to select one of many blocks of code to be executed


The if Statement

Use the if statement to execute some code only if a specified condition is true.

Syntax

if (condition) code to be executed if condition is true;

The following example will output "Have a nice weekend!" if the current day
is Friday:

<html>
<body>

<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>

</body>
</html>

Notice that there is no ..else.. in this syntax. The code is executed only if the specified condition is true.


The if...else Statement

Use the if....else statement to execute some code if a condition is true and another code if a
condition is false.

Syntax

if (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, otherwise it will output "Have a nice day!":

<html>
<body>

<?php
$d=date("D");
if ($d=="Fri")

{
echo "Have a nice weekend!";

}
else

{
echo "Have a nice day!";

}
?>

</body>
</html>


The if...elseif....else Statement

Use the if....elseif...else statement to select one of several blocks of code to be executed.

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>

« Previous

Next Chapter »



W3Schools Certification

W3Schools' Online Certification

The perfect solution for professionals who need to balance work, family, and career building.

More than 10 000 certificates already issued!

Get Your Certificate »

The HTML Certificate documents your knowledge of HTML.

The CSS Certificate documents your knowledge of advanced CSS.

The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.

The jQuery Certificate documents your knowledge of jQuery.

The XML Certificate documents your knowledge of XML, XML DOM and XSLT.

The ASP Certificate documents your knowledge of ASP, SQL, and ADO.

The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

Your suggestion:

Close [X]

Thank you for your support.

Close [X]

0 0