PHP If Else (elsefi) Statement

Source: Internet
Author: User

Condition Statement

When writing code, you often need to perform different actions for different judgments.

You can use conditional statements in the code to complete this task.

If... else statement

Execute one piece of code when the conditions are met, and execute another piece of code when the conditions are not met.
Elseif statement
Used with if... else to execute a code block If... Else statement when several conditions are met
If you want to execute some code when a condition is set, and execute other code when the condition is not set, use the if... else statement.

Syntax

If (condition)
Code to be executed if condition is true;
Else
Code to be executed if condition is false;

Instance

If the current date is Friday, the following code will output "Have a nice weekend! ", Otherwise it will output" Have a nice day! ":

Copy codeThe Code is as follows: <Body>
<? Php
$ D = date ("D ");
If ($ d = "Fri ")
Echo "Have a nice weekend! ";
Else
Echo "Have a nice day! ";
?>
</Body>
</Html>

If you need to execute multiple lines of code when the conditions are true or fail, you should include these lines in curly brackets:

Copy codeThe Code is as follows: <Body>
<? Php
$ D = date ("D ");
If ($ d = "Fri ")
{
Echo "Hello! <Br/> ";
Echo "Have a nice weekend! ";
Echo "See you on Monday! ";
}
?>
</Body>
</Html>

ElseIf statement
To execute code when multiple conditions are created, use the elseif statement:

Syntax

Copy codeThe Code is as follows: 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;

Instance
If the current date is Friday, the following example will output "Have a nice weekend! ", If it is Sunday, output" Have a nice Sunday! ", Otherwise output" Have a nice day! ":

Copy codeThe Code is as follows: <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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.