Home >Backend Development >PHP Tutorial >Three major process controls of PHP programs_PHP tutorial
Three major process controls for PHP programs
① Sequential control (from top to bottom, from left to right)
②Branch Control
if(conditional expression){
//nMultiple statements
}else if (conditional expression){
//n Multiple statements
}else if(conditional expression){
//.
}//There may be more else if
else{
}
u switch branch statement
Basic grammatical structure
switch(expression){
case constant 1:
//nMultiple statements;
break;
case constant 2:
//nMultiple statements
break;
default:
//nMultiple statements;
break;
}
u Type of constant (int, float, string, boolean and null
default can be placed anywhere
Hello12 will be output on the right.
Conclusion: First match in case order, if none matches
Whenis reached, default will be executed until break or }
is encountered.Application scenario: When our branch is a few points (such as judging the direction of a tank), we should use switch. If your branch is a judgment of several areas (range), consider using if.
③Loop control process
for(initial value of loop; condition of loop; step size){
//nMultiple statements;
}
while(loop condition){
//Loop body, statement
}
do{
//Loop body
}while(loop condition);
42. According to normal thinking, we should ensure that when accepting data, $_REQUEST['parameter'] must be consistent with the html element name given on the data submission page. If they are inconsistent, a notice will appear. At the same time, the data we accept is null, which is equivalent to "".
//$_REQUEST This method can accept the user's post or get request data
43. break and continue
u Loop-related statements-break
Basic concept: It means to end the current for, while, do..while, switch, process. You can give a number to indicate which layer to exit to.
$i=0;
while( $i){
switch($i){
case 5:
echo quit at5
;
break;
case 10:
echo quit 10
;
break 2;
default:
break;
}
}
echo '$i='.$i;
The result is:
quit at 5
quit at 10
$i=10
From the above cases, we draw several conclusions:
1. The break statement will jump out to level 1 by default
2. The number after the break statement cannot exceed the actual number of loop levels that can be jumped out, otherwise, a fatalerror will be reported
Ø Loop related statements-continue
Basic concept: continue is used to end the remaining code of this loop and start a new loop from scratch (if the condition is true, continue execution), continue can also be followed by a number to indicate the number of loops to restart from