Learning Objective: To control the flow control php
1, if .. else cycle of three structures
The first one is only used if conditions, as a simple judgement. Cheng explained, “if something happened, how to deal with.” Syntax is as follows:
if (expr) (statement)
The expr for judging the conditions, are often used as symbols judgement logic operation conditions. The statement was in line with the conditions operative procedures, and his party only if the process can be omitted braces ().
Example: in this case omitted the brackets.
<? php
if ($ state == 1) echo “. He”;
«>
Here special attention is to determine whether the same is not == =, ASP programmers may often commit this error, = is the assignment.
Example: in this case the operative part of a three lines, can not be omitted from the brackets.
<? php
if ($ state == 1) (
echo “. He;
echo “<br>”;
)
«>
In addition to the first two is if, coupled with the conditions else, could be interpreted as “if something happened, how to deal with, or how to solve.” Syntax is as follows
if (expr) (statement1) else (statement2) Example: the above example to edit into a more complete treatment. Only one else because of his implementation of the directive and therefore do not have to add braces.
<? php
if ($ state == 1) (
echo “. He”;
echo “<br>”;
)
else (
echo “Oh”;
echo “<br>”;
)
«>
The third is the recurrent cycle of if .. else, usually used in a variety of decision-making judgement. It will a few if .. else used the merger application processing.
Direct look at the following example
<? php
if ($ a> $ b) (
echo “a major than b”;
) Elseif ($ a == $ b) (
echo “a mean b”;
) Else (
echo “a smaller than b”;
)
«>
On the second floor of the cases only if .. else cycle, a and b used to compare the two variables. Actual use of this recurrent cycle if .. else, please be used with care, because too many of the cycle tends to design the logic of the problem, or less playing a big brackets, the procedure will appear baffling problem.
2, for the simple cycle only one, has not changed, its syntax is as follows
for (expr1; expr2; expr3) (statement)
Expr1 one of the conditions for the initial value. expr2 to judge the conditions are usually used logical symbols (logical operators) when the judgement of the conditions. expr3 to implement the statement after the implementation of the part used to change the conditions for the next cycle of judgement, such as plus-one .. and so on. The statement was in line with the conditions operative procedures, and his party only if the process can be omitted braces ().
The following example is written in the cycle for example.
<? php
for ($ i = 1; $ i <= 10; $ i + +) (
echo “This is the first”. $ i. “cycles <br>”;
)
«>
3, switch cycle, usually dealing with complex conditions judgement, each of the conditions, the instructions are part of case. In practice, if the use of many similar if instructions, it can be integrated into a switch cycle.
Syntax is as follows
switch (expr) (case expr1: statement1; break; case expr2: statement2; break; default: statementN; break;)
The expr conditions, usually variable name. And the case after the exprN, usually expressed variable value. After the colon is in conformity with the conditions to the implementation part. Attention should break Tiaoli cycle.
<? php
switch (date ( “D”)) (
case “Mon”:
echo “today Monday”;
break;
case “Tue”:
echo “Today, Tuesday”;
break;
case “Wed”:
echo “today Wednesday”;
break;
case “Thu”:
echo “this Thursday”;
break;
case “Fri”:
echo “this Friday”;
break;
default:
echo “leave today”;
break;
)
«>
It should be noted that the break; other omitted, default, the omission can be.
Obviously, if the above example with very cycle of trouble. Of course, in the design, to the greatest probability of the conditions in front, at least the conditions on the final face, can increase the efficiency of the implementation process. On the cases because of the probability of the same day, so do not have to pay attention to the order.
Here today, tomorrow that the use of the database.

by pagecity.org : http://www.pagecity.org