Php branch structure condition structure selection structure-PHP source code

Source: Internet
Author: User
Tags switch case
Ec (2); php tutorial branch structure condition structure selection Structure & lt ;? Php * & nbsp; Process Control & nbsp; * & nbsp; & nbsp; 1. Sequence Structure & nbsp; * & nbsp; 2. Branch Structure -- condition Structure -- select Structure & nbsp; * script ec (2); script

Php tutorial branch structure condition structure selection Structure

/*
* Process Control
*
* 1. Ordered Structure
* 2. Branch Structure-condition structure-select Structure
* 1. Single Branch
* // Condition bool, true or false, ><==! -& |!
* If (condition)
* Execute the following statement.
* If (condition)
*{
* Code segment;
* Code segment;
*}
*
* 2. Dual Branch
* Use else clause
*
* If (condition)
* Execute a statement
* Else
* Execute a statement
* If (condition ){
* One or more codes
*} Else {
* One or more codes
*}
*
* 3. Multiple branches
* If else if and switch case can be used
* // This is a mutually exclusive relationship.
* If (condition ){
*
*} Else if (condition ){
*} Else if (condition ){
*} Else {
*}
* 4. nested branches
* If (){
* If (){
*} Else {
* If (){}
*}
*}
* 3. Loop Structure
*
*
*
*
*
* Conclusion:
* Elseif is used to determine a range
* For a single match, use switch case
*/
// Single-path execution
$ A = 10;
$ B = 5;
If ($ a> $ B)
Echo "$ a is greater than $ B ";

// Two-way execution
$ A = 10;
$ B = 20;
If ($ a> $ B)
{
Echo "$ a greater than $ B
";
}
Else
{
Echo "$ a less than $ B
";
}

// Multi-path execution
$ Hour = date ("H ");
If ($ hour> 6 & $ hour <9)
{
Echo "good morning! ":
}
Else if ($ hour> 9 & $ hour <12)
{
Echo "good morning ";
}
Else if ($ hour> 12 & $ hour <14)
{
Echo "Good noon ";
}
Else if ($ hour> 14 & $ hour <17)
{
Echo "Good afternoon ";
}
Else if ($ hour> 17 & $ hour <19)
{
Echo "Good evening ";
}
Else if ($ hour> 19 & $ hour <22)
{
Echo "Good evening ";
}
Else
{
Echo "good night ";
}
// Code improvement based on mutual exclusion
$ Hour = date ("H ");
If ($ hour <9)
{
Echo "good morning! ":
}
Else if ($ hour <12)
{
Echo "good morning ";
}
Else if ($ hour <14)
{
Echo case "Mon ":
Echo "Monday ";
Break; "Good noon ";
}
Else if ($ hour <17)
{
Echo "Good afternoon ";
}
Else if ($ hour> 19)
{
Echo "Good evening ";
}
Else if ($ hour <22)
{
Echo "Good evening ";
}
Else
{
Echo "good night ";
}

// Determine the day of the week
$ Week = date ("D"); // obtain the day of the week
Switch ($ week) // switch (variable) variables only use integer and string
{
Case "Mon ":
Echo "Monday ";
Break;
Case "Tue"
Echo "Tuesday ";
Break;
Case "Wed ":
Echo "Wednesday ";
Break;
Case "Thu ":
Echo "Thursday ";
Break;
Case "Fri ":
Echo "Friday ";
Break;
Default:
Echo "Weekend ";
}

// Nested class
$ Sex = $ _ GET ["sex"];
$ Age = $ _ GET ["age"];
If ($ sex = "nan ")
{
If ($ age> = 60)
{
Echo "this $ sex has retired". ($ age-60). "years ";
}
Else
{
Echo "this man is still at work, and". (60-$ age). "retired only in years ";
}
}
Else
{
If ($ age> = 66)
{
Echo "this $ sex has retired". ($ age-66). "years ";
}
Else
{
Echo "this lady is still working, and". (66-$ age). "She retired in just one year ";
}
}
?>

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.