Control structure of PHP beginners

Source: Internet
Author: User
: This article mainly introduces the control structure of PHP beginners. For more information about PHP tutorials, see. Control Structure
I. preface;
Quantity; variables, constants, numerical values
OPERATOR. a tool supporting operations between quantities
Control Structure; application of computing results
Scenario: "Fei Ge", "I love you", and "Hello". These are three types: "Fei Ge, I love you", "Fei Ge, hello, "This is the result of connecting two quantities after the operator is added,
At this time, Fei Ge and Fei Jie walked out and were seen by a pretty girl and flew to Fei Ge. what should I say? This is the control structure. in different situations,
Make different responses;
2. sequential structure: execute from top to bottom in the order of writing.
3. branch structure. when a program reaches a certain node, several situations may occur. select one type of execution based on the conditions.
1. single branch;
If (condition ){
Result;
}
If the condition is true, the program continues to run. if the condition is not true, the program terminates. if the condition is true, the program stops... (Yes...
$ Age = 20;
If ($ age = 20 ){
Echo 'you can marry Feige ';
}
2. dual branch;
If (condition ){
Result 1;
} Else {
Result 2;
}
If the condition is true, execution result 1. if the condition is false, execution result 2. if... (Yes... (No) otherwise ..
$ Age = 20;
If ($ age> = 20 ){
Echo 'you can marry Feige ';
} Else {
Echo 'You are still young, wait another two year ';
}
3. multiple branches;
(1)
If (condition 1 ){
Result 1;
} Else if (condition 2 ){
Result 2;
} Else (
Result 3;
)
If condition 1 is true, result 1 is executed. if condition 1 is false, condition 2 is judged. if condition 2 is true, result 2 is executed. if condition 2 is false, result 3 is executed. So I will make multiple judgments, and finally add a situation where all the conditions are false (sometimes this is not added ).
$ Age = 20;
If ($ age> = 38 ){
Echo ' ';
} Else if ($ age >=20 & $ age <= 38 ){
Echo 'Brother Fei's arms are always open for you ';
} Else {
Echo: you are still young. wait for another two years! ';
}
(2)
Switch (variable ){
Case value 1;
Result 1;
Break;
Case value 2;
Result 2;
Break;
Case value 3;
Result 3;
Break;
}
From top to bottom, make a judgment on the variables and the values below. if the value of the variable is equal to a few, execute the corresponding result value,
Switch ($ age = 20 ){
Case 20;
Echo 'Brother Fei's arms are always open for you ';
Break;
Case 15;
Echo: you are still young. wait for another two years! ';
Break;
Case 45;
Echo ' ';
Break;
}
Break; damage, terminate, and exit the program after the qualified operation is executed. if there is no such person, the program will continue to execute the following judgment, and the judgment conditions will be met by default.
Comparison; if else if; can be used to determine the range, or even crazy complex expressions
Switch case; you can only judge whether a variable is equal to the specified value. you cannot make more complex judgments. it can be understood, the switch case is short for if else if

IV. loop structure;
1. while loop;
While (condition ){
Result;
}
While, when the condition is true, execute the result and continue to judge the condition until the condition is false and the program exits. Therefore, this condition is generally in the form of an expression.
$ Age = 5;
While ($ age> 20 ){
$ Age + = 5;
If ($ age> = 20 ){
Echo 'you can marry Feige ';
} Else {
Echo 'You are still young, wait another two year ';
}
}
Disadvantages: (1) when the condition is false when it is opened, the while directly jumps out.
(2) when the condition is always true, the program will continue to execute until it crashes.
2. do while loop;
Do {
Result;
} While {
Condition
}
Execute this statement at least once and then judge.
Note: the while loop and do while loop are generally used together with the if statement.
3. for loop;
For (initial value 1; judgment condition 2; initial value change 4 ){
Execute statement 3;
}
The initial value 1 is the initialization data. it is executed once first, and then condition 2 is used to judge the initialization data. if it is true, statement 3 is executed, and the initialized data changes,
If the condition is false, skip the step of executing the statement and change the initial value by 4 until the condition is false.
Note: break is used when we want to terminate the loop when the loop reaches a certain condition. it will terminate the loop directly.
When outputting data, we need to use continue to ignore data of a certain condition,

The above introduces the control structure of PHP beginners, including some content, and hopes to help those who are interested in PHP tutorials.

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.