inicio mail me! sindicaci;ón

Of Php of 10 days of societies the following day

Text key word: Programming / PHP/ skill

Learn a purpose: Master the flow control of Php

1, If. .else has 3 kinds of structures circularly

The first kind is to use If condition only, regard as pure judgement. If produced something,the explanation becomes ” how to handle ” . Grammar is as follows:

If (expr) {Statement}

Among them Expr is the condition of judgement, it is the condition that should judge with logistic operation symbol normally. And Statement is those who accord with a condition to execute partial order, if the program has group only, can omit big bracket {} .

  

Example: This exemple omits big bracket.

< ? Php

If ($state==1)echo ” with a ha breaths out ” ;

? >

Of special attention is here, whether is judgement equally==is not = , ASP programmer often may make this mistake, = is assignment.

Example: The executive part of this exemple has 3, cannot omit big bracket.

< ? Php

If ($state==1) {

Echo ” with a ha breaths out;

Echo "<br>" ;

}

? >

The second kind is besides If, imposed the requirement of Else, can explain how to if produced something,be handled into ” , how should solve ” otherwise. Grammar is as follows

Example of If (expr) {Statement1} Else {Statement2} : The case above will revise more complete processing. Because among them Else has the directive that implements all right only, need not add big bracket accordingly.

< ? Php

If ($state==1) {

Echo ” with a ha breaths out ” ;

Echo "<br>" ;

}

Else{

Echo ” is breathed out breathe out ” ;

Echo "<br>" ;

}

? >

The 3rd kind is recursive If. .else circulates, use when a variety of decision-making judgement normally. It will count an If. .else takes amalgamative use use to manage.

See the case below directly

< ? Php

If ($a>$b) {

Echo “a compares B big ” ;

} Elseif ($a==$b) {

Echo “a is equal to B” ;

} Else {

Echo “a compares B small ” ;

}

? >

Go up an If that uses 2 only. .else circulates, with will compare A and B two variable. Should use this actually to plant recursive If. When.else circulates, take care to use please, because too multilayer loop makes the logic of the design gives an issue easily, hit big bracket less perhaps to wait, can cause a program appear indescribable problem.

2, For loop is pure have a kind only, did not change, its grammar is as follows

For (expr1; Expr2; Expr3) {Statement}

Among them Expr1 is initiative value of the condition. Expr2 is the condition of judgement, it is to use logistic operation symbol normally (the condition that Logical Operators) should judge. Expr3 is the part that should implement after executive Statement, use change condition, offer next time circular judgement, if add one. . Etc. And Statement is those who accord with a condition to execute partial order, if the program has group only, can omit big bracket {} .

Next exemples are uses what For writes circularly case.

< ? Php

For ($i = 1; $i <= 10; $i ) {

Echo ” this is ” . $i. "Second loop <br>" ;

}

? >

3, Switch circulates, handle the conditional judgement of complex formula normally, every stature condition, it is Case instruction part. In solid make if use a lot of similar If to dictate,go up, can circulate it integratedly into Switch.

Grammar is as follows

Switch (expr) {Case Expr1: Statement1; Break; Case Expr2: Statement2; Break; Default: StatementN; Break; }

Among them Expr condition, it is variable name normally. And the ExprN after Case, represent variable value normally. Colon hind wants executive part to accord with this condition. The attention should jump with Break from the loop.

< ? Php

Switch (Date (”D” )) {

Case “Mon” :

Echo ” today on Monday ” ;

Break;

Case “Tue” :

Echo ” today Tuesday ” ;

Break;

Case “Wed” :

Echo ” today on Wednesday ” ;

Break;

Case “Thu” :

Echo ” today on Thursday ” ;

Break;

Case “Fri” :

Echo ” today on Friday ” ;

Break;

Default:

Echo ” has a holiday today ” ;

Break;

}

? >

What the attention needs here is Break; was not omited, default, it is possible elliptically.

Apparent, afore-mentioned example circulates with If very troublesome. When be being designed of course, want to lead occurrence machine the biggest condition to be put in foremost face, the condition that appears the least is put in final area, can increase the executive efficiency of the program. Go up because the exemple appears everyday machine rate is same, what need not notice a condition so is ordinal.

Today with respect to respecting here, will begin to say the use of the database tomorrow.

Bookmark:Digg Del.icio.us Reddit

Leave a Comment