Home  >  Article  >  Backend Development  >  What are the flow control structures in PHP?

What are the flow control structures in PHP?

(*-*)浩
(*-*)浩Original
2019-09-06 13:13:383584browse

What are the flow control structures in PHP?

Definition of process control

Process control is a means of controlling the program. If the program did not have process control, there would be no programming language , most programs are not controlled by linear execution statements. If the program needs to communicate with the user, the execution sequence must be determined based on user input, and even the code needs to be executed repeatedly, all of which are inseparable from process control.

There are three basic program structures in PHP: sequential structure, branch structure, and loop structure.

1. Sequential structure (Recommended learning: PHP programming from entry to proficiency)

Sequential structure is the most commonly used statement structure For example, assignment statements are executed in order from top to bottom. For example:

$a=3;$b=4; If we want to exchange the values ​​​​of $a and $b, we need the third variable $c. The implementation method is as follows:

<?php
//实现$a和$b的值互换
$a=3;
$b=4;
$c=$a;
$a=$b;
$b=$c;
echo&#39;$a=&#39;.$a;
echo"<br/>";
echo&#39;$b=&#39;.$b;
?>

The result is $a=4;$b=3; to realize the exchange of $a and $b values:

2. Branch structure

The branch structure is also called condition Structure, it selects the execution path based on clear conditions, rather than strictly following the sequence. In actual programming, appropriate branch statements must be selected according to the program flow. It changes the executed program according to the results of the conditions. The branch structure mainly has the following forms.

Single branch structure (if statement)

The if structure is a single conditional structure. The if statement changes the execution order of statements. It is used in many languages, including PHP. One of the most important features.

if statement format:

if(表达式)
语句块;

The expression is the condition for execution. The condition can only be a Boolean value. If the expression is a value of other types, it will automatically be converted into a Boolean TRUE or FALSE, whether execution depends on the "expression" result. The following example will output "Good evening!" if the current time (HOUR) is greater than 18:

<?php
$t=date("H");
if ($t>"18") {
echo "晚上好!";
}
?>

Bidirectional conditional branch structure (if...else)

The if statement contains the else word. If the condition is met, one statement will be executed. If the condition is not met, other statements will be executed. This is the function of the else clause. else is the clause of the if statement. It must be used at the same time as if and cannot exist alone. else syntax format:

if (条件) {
条件为 true 时执行的代码;
} else {
条件为 false 时执行的代码;
}

The following example will output "Good day!" if the current time is less than 20, otherwise it will output "Good evening!" The code is as follows:

<?php
$t=date("H");
if ($t<"20") {
echo "白天好!";
} else {
echo "晚上好!";
}
?>

Multidirectional branches Structure (elseif clause)

It is a combination of if and else. The elseif clause will determine which statement block to execute based on different expression values. The format of the else statement is as follows

if (条件) {
条件为 true 时执行的代码;
} elseif (condition) {
条件为 true 时执行的代码;
} else {
条件为 false 时执行的代码;
}

3. Loop structure

What computers are best at is repeatedly performing certain operations according to conditions. Its characteristic is that when a given condition is established, It is executed repeatedly until the condition is not established. This condition becomes the loop condition, and the repeatedly executed program segment becomes the loop body. PHP provides three types of loops: while loop, do-while loop, and for loop

while loop

while loop is the simplest loop in php , while needs to set a Boolean condition first, and continuously execute a statement block until the condition is not established. Often used to control loop structures with unknown number of loops. The statement format of while is as follows

while (条件为真) {
要执行的代码;
}

Example

<?php
$x=0;
while($x<=3) {
echo "这个数字是:$x <br>";
$x++;
}
?>

do....while loop

do.... The while loop will first execute the code block once, and then check the condition. If the specified condition is true, the loop will be repeated. If it is FALSE at the beginning, the entire loop will stop. do....while loop statement format:

do {
要执行的代码;
} while (条件为真);

for statement loop

The for loop is suitable for situations up to the number of repeated executions. The for statement needs to be defined in advance Well, it is the most complex loop structure in PHP. The format of the for statement is as follows:

for(初始化;条件表达式;增量){
语句块;
}

The initial statement is an assignment statement, which sets the initial assignment of the loop variable. The expression is a relational expression that determines when to exit the loop. If the condition is true, execution will continue. If the condition is false, the loop will end and execute the following statement. Increment defines the loop control variable and how it changes each loop.

<?php
for ($y=5; $y<=10; $y++) {
echo "数字是:$y";
echo"<br/>";
}
?>

The above is a brief introduction to the process control in PHP. In actual work, a large number of control statements need to be used, and they will also be nested. The process control statements need to continuously improve their own capabilities and continuously Summarize progress.

The above is the detailed content of What are the flow control structures in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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