Php process control statement

Source: Internet
Author: User

This article describes two php process judgment statements: switch statement and if else and if () statement. Let's take a look at the example in detail.

 

The Code is as follows: Copy code
Switch (variable ){
Case value1:
Statement1;
Break;
Case value2:
...
Default:
Defulat statement;
}

The switch statement is compared with the value in case based on the value of variable. If the value is not equal, continue to find the next
Case; if they are equal, execute the corresponding statement until the switch statement ends or the break is met.

The Code is as follows: Copy code
<? Php
Switch ($ I ){
Case "apple ":
Echo "I is apple ";
Break;
Case "bar ":
Echo "I is bar ";
Break;
Case "cake ":
Echo "I is cake ";
Break;
}
?>

Instance 2

The Code is as follows: Copy code

<? Php
Switch ($ I ){
Case 0:
Echo "I equals 0 ";
Break;
Case 1:
Echo "I equals 1 ";
Break;
Case 2:
Echo "I equals 2 ";
Break;
}
?>


Instance 3

The Code is as follows: Copy code

<? Php
Switch ($ I ){
Case 0:
Case 1:
Case 2:
Echo "I is less than 3 but not negative ";
Break;
Case 3:
Echo "I is 3 ";
}
?>

If... Else statements can only select two types of results: true or false. But now there are two or more options. What should I do?
What about it? In this case, you can run the esleif (or write else if) Statement in the following format:

The Code is as follows: Copy code
If (expression1 ){
Statement1;
} Else if (expression2 ){
Statement2;
}
...
Else {
Statementn;
}

Instance

The Code is as follows: Copy code
<? Php
$ Moth = date ("n ");
$ Today = date ("j ");
If ($ today> = 1and $ today <= 10 ){
Echo 'Today is '. $ moth. 'month'. $ today. 'day-to-day ';
} Elseif ($ today> 10 and $ today <= 20 ){
Echo 'Today is '. $ moth. 'month'. $ today.' mid-day ';
} Else {
Echo 'Today is '. $ moth. 'month'. $ today.' late Thursday ';
}
?>

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! ":

The Code is as follows: Copy code

<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>

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.