Learning Purpose: Mastering the Process Control of PHP
1. If: else loop has three types of structure
The first is to use the IF condition as a mere judgment. Explain how to deal with something if it happens. The syntax is as follows:
if (expr) {statement}
The conditions in which expr is judged are usually determined by the use of logical operation symbols. And statement is a qualifying execution part of the program, if the program has only one row, you can omit the curly brace {}.
Example: This example omits curly braces.
if ($state ==1) echo "haha";
?>
It is particularly important to note that determining whether equality is = = rather than =,asp programmers can often make this error, = is an assignment.
Example: The execution section of this example has three rows and cannot omit curly braces.
if ($state ==1) {
echo "haha;
echo "
" ;
}
?>
The first two is the addition of the IF condition, which can be interpreted as "what to do if something happens or how to resolve it". The syntax is as follows
if (expr) {statement1} else {Statement2} example: the example above is modified to a more complete processing. The else in which there is only one line of instructions, so do not add braces.
if ($state ==1) {
echo "haha";
echo "
";
}
else{
echo "hehe";
echo "
";
}
?>
The third type is recursive if. else loops, often used in a variety of decision-making judgments. It will be a few if. Else to combine the use of processing.
Look directly at the following example
if ($a > $b) {
echo "A is larger than B";
} elseif ($a = = $b) {
echo "a equals B";
} else {
echo "A is smaller than B";
}
?>
The above example uses only two layers of if. else loop, which is used to compare the two variables of a and B. This recursive if is actually to be used. else loop, please be careful to use, because too many layers of the loop is easy to make the logic of the design problems, or less curly braces, etc., will cause the program to appear inexplicable problem.
2, for the loop is only one, no change, its syntax is as follows
for (EXPR1; expr2; expr3) {statement}
Where the EXPR1 is the initial value of the condition. EXPR2 is a condition for judging, usually using the logic operation symbol (logical operators) when judging the condition. EXPR3 the part to be executed after the execution of the statement, to change the condition for the next cycle judgment, such as add one. Wait a minute. And statement is a qualifying execution part of the program, if the program has only one row, you can omit the curly brace {}.
The following example is an example written with a for loop.
for ($i = 1; $i <=; $i + +) {
echo "This is the first". $i. " Secondary cycle
" ;
}
?>
3, switch loop, usually deal with the conditional judgment of the compound, each sub-condition, is the case instruction part. In practice, if you use many similar if directives, you can combine them into a switch loop.
The syntax is as follows
Switch (expr) {case expr1:statement1, break, Case Expr2:statement2, break, DEFAULT:STATEMENTN, break;}
The expr condition, which is usually the variable name. The ExprN after a case usually represents the value of the variable. The colon is followed by the part that meets the criteria to be executed. Note to use break to jump off the loop.
Switch (date ("D")) {
Case "Mon":
echo "Today Monday";
Break
Case "Tue":
echo "Today Tuesday";
Break
Case "Wed":
echo "Today Wednesday";
Break
Case "Thu":
echo "Today Thursday";
Break
Case "Fri":
echo "Today Friday";
Break
Default
echo "Holiday Today";
Break
}
?>
Here is the break; don't miss it, default, omitting is possible.
Obviously, the above example is very troublesome with the IF loop. Of course, in the design, the most likely to be the highest probability of the conditions at the front, the least occurrence of the conditions on the last side, you can increase the efficiency of the execution of the program. The previous example has the same probability of appearing every day, so don't pay attention to the order of the conditions.
Today, we'll start by talking about the use of the database tomorrow.
http://www.bkjia.com/PHPjc/314910.html www.bkjia.com true http://www.bkjia.com/PHPjc/314910.html techarticle Learning Purpose: Mastering the PHP Process Control 1, if. Else loop there are three kinds of structures the first is to use the IF condition as a mere judgment. Explain how to deal with something if it happens ...