Directory [1] if statement [2] switch [3] while [4] do-while [5] for statement [6] foreach [7] break [8] continue [9] goto PreviousAny PHP script is composed of a series of statements. A statement can be a value assignment statement, a function call, a loop, a condition statement, or even a statement that does nothing (empty statement ). The statement usually ends with a semicolon. In addition, you can use curly brackets to encapsulate a group of statements into a group of statements. A statement group can be considered as a row of statements. This document describes the types of statements in detail. If statementThe if structure is one of the most important features of many languages, including PHP. It allows code snippets to be executed according to conditions. If (condition) {code executed when the condition is true;} else {code executed when the condition is false ;} if (condition) {code executed when the condition is true;} elseif (condition) {code executed when the condition is true;} else {code executed when the condition is false ;} <? Php $ t = date ("H"); if ($ t <"10") {echo "Have a good morning! ";} Elseif ($ t <" 20 ") {echo" Have a good day! ";} Else {echo" Have a good night! ";}?> Switch statementThe switch statement is similar to a series of if statements with the same expression. In many cases, you need to compare the same variable (or expression) with many different values and execute different codes based on the value it equals. This is exactly the purpose of the switch statement. [Note] switch/case is loosely compared. Switch (expression) {case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2;} <? Phpswitch ($ x) {case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3" ;}?> While statementWhile loop is the simplest loop type in PHP. The while statement indicates that PHP repeats the nested loop statement as long as the while expression value is TRUE. The value of the expression is checked when the loop starts. Therefore, even if the value changes in the loop statement, the statement will not stop until the loop ends. Sometimes, if the value of the while expression is FALSE at the beginning, the loop statement will not be executed once. While (expr) statement <? Php $ I = 1; while ($ I <= 10) {echo $ I ++;} $ I = 1; while ($ I <= 10): print $ I; $ I ++; endwhile;?> Do-whileThe do-while loop is very similar to the while loop. The difference is that the expression value is checked at the end of each loop rather than the start. The main difference with a general while loop is that the do-while loop statement is always executed once (the true value of the expression is checked after each loop ends) Do {code to be executed;} while (the condition is true); <? Php $ I = 0; do {echo $ I;} while ($ I> 0);?> For statementA for Loop is the most complex loop structure in PHP. In a for loop statement, the value is obtained unconditionally before the start of the loop, and the loop condition is evaluated before the start of each loop. If the value is true, the loop continues and the loop body statement is executed. If the value is false, the loop ends. The incremental statement is executed after each loop. For (init counter; test counter; increment counter) {code to be executed;} <? Php for ($ x = 0; $ x <= 10; $ x ++) {echo ": $ x <br>" ;}?> ForeachThe foreach syntax structure provides a simple way to traverse arrays. Foreach can only be applied to arrays and objects. If you try to apply it to variables of other data types or uninitialized variables, an error message will be sent. Every iteration of an array, the value of the current array element is assigned to the $ value variable, and the array pointer is moved one by one until the last array element is reached. There are two methods: No subscript and no subscript [1] Only values, not subscript Foreach ($ array as $ value) {code to be executed;} <? Php $ colors = array ("red", "green", "blue", "yellow"); foreach ($ colors as $ value) {echo "$ value <br>" ;}?>[2] Both subscript and value are obtained. Foreach ($ array as $ index =>$ value) {code to be executed;} <? Php $ colors = array ("r" => "red", "g" => "green", "B" => "blue ", "y" => "yellow");/* r: redg: greenb: bluey: yellow */foreach ($ colors as $ key => $ value) {echo $ key. ":". $ value. "<br>" ;}?> BreakBreak ends the execution of the current for, foreach, while, do-while or switch structure. Break can accept an optional numeric parameter to determine the number of loops to jump out. $ I = 0; while (++ $ I) {switch ($ I) {case 5: echo "At 5 <br/> \ n"; break 1; /* exit switch only. */case 10: echo "At 10; quitting <br/> \ n"; break 2;/* exit switch and while LOOP */default: break ;}} ContinueContinue is used in the loop structure to skip the remaining code in this loop and start executing the next loop when the condition value is true. Continue accepts an optional numeric parameter to decide to skip multiple cycles to the end of the loop. The default value is 1, that is, jump to the end of the current loop $ I = 0; while ($ I ++ <5) {echo "Outer <br/> \ n"; while (1) {echo "Middle <br/> \ n"; while (1) {echo "Inner <br/> \ n"; continue 3 ;}echo "This never gets output. <br/> \ n ";} echo" Neither does this. <br/> \ n ";} GotoThe goto operator can be used to jump to another position in the program. The target location can be marked by the target name with a colon, while the jump command is the tag of the target location after the goto. The goto in PHP has certain restrictions. The target location can only be in the same file and scope. That is to say, you cannot jump out of one function or class method, or jump into another function. And cannot jump into any loop or switch structure. Can jump out of the loop or switch, the general usage is to use goto instead of multiple layers of break <? Phpgoto a; echo 'foo'; a: // 'bar' echo 'bar';?> |