Home > Backend Development > PHP Tutorial > Learn PHP statements

Learn PHP statements

高洛峰
Release: 2016-11-21 11:51:49
Original
1085 people have browsed it

Table of Contents

[1]if statement[2]switch [3]while[4]do-while[5]for statement[6]foreach[7]break[8]continue[9]goto

Previous words

Any PHP script is composed of a series of statements. A statement can be an assignment statement, a function call, a loop, a conditional statement or even a statement that does nothing (empty statement). Statements usually end with a semicolon. In addition, you can also use curly braces to encapsulate a group of statements into a statement group. The statement group itself can be thought of as a line of statements. This article will introduce various statement types in detail


if statement

 The if structure is one of the most important features of many languages, including PHP. It allows code fragments to be executed according to conditions

if (condition) { When the condition is true Code to be executed; } if (condition) { Code to be executed when the condition is true; } else { Code to be executed when the condition is false; } if (condition) { Code to be executed when the condition is true; } elseif (condition) { Condition Code executed when the condition is true;} else { Code executed when the condition is false;}
switch statement

 The switch statement is similar to having the same A series of if statements for an expression. There are many situations where you need to compare the same variable (or expression) with many different values ​​and execute different code depending on which value it equals. This is exactly the purpose of the switch statement

 [Note] switch/case does a loose comparison

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;}
while statement

 While loop is the simplest loop type in PHP. The meaning of the while statement is simple. It tells PHP to repeatedly execute the nested loop statement as long as the while expression evaluates to TRUE. The value of the expression is checked each time the loop starts, so even if the value changes during the loop statement, execution of the statement will not stop until the end of the loop. Sometimes if the value of the while expression is FALSE at the beginning, the loop statement will not be executed even once

while (expr) statement
do-while

 The do-while loop is very similar to the while loop, the difference lies in the value of the expression It is checked at the end of each loop, not at the beginning. The main difference from a normal while loop is that the do-while loop statement is guaranteed to be executed once (the truth value of the expression is checked after each loop)

do { Code to be executed;} while (condition is true); 0);?>
for statement

 The for loop is the most complex loop structure in PHP. In a for loop statement, the initialization is evaluated unconditionally once before the loop starts, and the loop condition is evaluated before each loop starts. If the value is true, the loop continues and the loop body statement is executed; if the value is false, the loop is terminated. The increment statement is executed after each loop

for (init counter; test counter; increment counter) { code to be executed;} ";} ?>
foreach

 The foreach syntax structure provides a simple way to traverse the array. 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 issued. Each time a loop iteration is performed, the value of the current array element will be assigned to the $value variable. And the array pointer will move one by one until it reaches the last array element. There are generally two ways: without removing the subscript, removing the subscript

 【1】Only get the value, without removing the subscript

foreach ($array as $value) { code to be executed;} ";}?>

 【2】Fetch at the same time Subscript and value

foreach ($array as $index => $value) { code to be executed;} "red", "g"=>"green", "b"=>"blue", "y"=>"yellow"); /*r:redg:greenb:bluey:yellow*/foreach ($colors as $key => $value) { echo $ key.":".$value."
";}?>
break

 break ends the execution of the current for, foreach, while, do-while or switch structure

 break can accept an optional Numeric parameters to decide how many loops to break out of

$i = 0; while (++$i) { switch ($i) { case 5: echo "At 5
n"; break 1; /* only Exit switch. */ case 10: echo "At 10; quitting
n"; break 2; /* Exit switch and while loop*/ default: break; }}
continue

continue is used in loop structures to skip the remaining code in this loop and start executing the next loop when the condition evaluates to true

 continue accepts an optional numeric parameter to determine how many loops to skip 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
n"; while (1) { echo "Middle
n"; continue 3; echo "This never gets output.
n"; echo "Neither does this.
n";}
goto

 The goto operator can be used to jump to another location in the program. The target position can be marked with the target name followed by a colon, and the jump instruction is the goto followed by the mark of the target position. Goto in PHP has certain restrictions. The target location can only be in the same file and scope, which means that it cannot jump out of a function or class method, nor can it jump into another function. It also cannot jump into any loop or switch structure. You can break out of the loop or switch. The usual usage is to use goto instead of multi-level break


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template