Comparison of if else in PHP with ElseIf usage

Source: Internet
Author: User

If ... Else statement

Use the If....else statement if you want to execute some code when a condition is set up and execute some other code when the condition is not valid.

Grammar

The code is as follows Copy Code

if (condition)//Set condition
Code to was executed if condition is true; If the condition is true, the code is executed;
Else
Code to was executed if condition is false; If the condition is false, the code is executed


Instance 1
If the current date is Monday, the following code will output "Happy Monday." "Or else it will output" happy every day. ”:

The code is as follows Copy Code

<?php
$d =date ("D"); Variable d Assignment
if ($d = = "Mon") {
echo "Monday happy!" ";
}else{
echo "Happy every day!" ";
}
?>

Instance 2

If D equals 1 o'clock Output "number 1", otherwise the output "number is not 1"

The code is as follows Copy Code
<?php
$d = 2; Variable d Assignment
if ($d ==1) {
echo "Number 1"; Value of variable D equals 1 o'clock output
}else{
echo "Number is not 1"; Values that are not equal to 1 o'clock output
}
?>

ElseIf statement

If you want to execute code when one of several conditions is true, use the ElseIf statement:

Grammar

The code is as follows Copy Code

if (condition)//Condition 1
Code to was executed if condition is true; Condition 1 is true, code is executed
ElseIf (condition)//Condition 2
Code to was executed if condition is true; Condition 2 is true, code is executed
Else
Code to was executed if condition is false; When all 2 conditions are false, the code is executed


Instance


When the value of D equals 1 o'clock, the output "number 1" is equal to 2 o'clock output "number 2" and output "no match number" when none of the two conditions are true.

The code is as follows Copy Code

<?php
$d = 3; Variable d Assignment
if ($d ==1) {
echo "Number 1"; When d equals 1 o'clock Output "Number 1"
}
ElseIf ($d ==2) {
echo "Number 2"; When d equals 2 o'clock Output "Number 2"
}
else{
echo "does not conform to the number"; Two conditions are not established when the output of the content
}
?>

Summary in if else and ElseIf if can include have else and elseif but if you use the if else will not appear elseif but instead, we can see from the above example, more PHP tutorials can be clicked on the connection view.

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.