Home > Backend Development > PHP Tutorial > A simple tutorial on flow control statements of PHP basics

A simple tutorial on flow control statements of PHP basics

伊谢尔伦
Release: 2023-03-11 06:10:01
Original
1340 people have browsed it

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.

From the perspective of execution mode, the control structure of the statement is divided into the following three types:

1. Sequential structure : Completely executed sequentially from the first statement to the last statement;

2. Selection structure: based on user input or the middle of the statement As a result, several tasks are performed;

#3. Loop structure: According to a certain Conditional execution of a task several times, or until the goal is achieved.

##There are three

control statements in PHP used to implement selection structures and loop structures:

1. Conditional control statement: if , else, elseif and switch;

2. Loop control statements: foreach, while, do while and for;

3. Transfer control statements: break, continue and return.

Conditional control statement:

If statement, usage: ##  

If(A) ##    Statement1;

 Else

Statement2;

Analysis: If A is true, execute statement1; otherwise, execute statement2.

Example, code:

<?php
  $a = 59;  //根据$a的值,判断是否及格。如果>=60则输出及格
  if($a>=60){
  echo “及格”;
   }else
  echo “不及格”;
?>
Copy after login
If···elseif· ··else statement, usage:

##If(A)

  Statement1;

Elseif(B)

  Statement2;

Else

 Statement3;

解析:如果A为TRUE,则执行statement1。否则,如果B的值为TRUE,则statement2;否则执行statement3。当然:if语句也可以嵌套。

下面是个If···elseif···else的例子:

<?php
  $a = 59;
  if($a>=60)        //在大于等于60的情况里在进行分类
 {
  if($a==100)
  echo “满分”;
  elseif($a>=90)
  echo “优秀”;
 else
  echo “及格”;
 }
 else
  echo “不及格”;
?>
Copy after login

Switch语句,语法如下:

Switch(A)

{

 Case val1:

  Statement1;

  Break;

 Case val2:

  Statement2;

  Break;

 Default:

  Statement3;

 }

当一个case语句中的值和switch表达式A的值匹配时,PHP开始执行语句,直到switch程序段结束或者遇到第一个break语句为止

(如果没有遇到break,则PHP将继续执行下一个case)。

下面是一个没有break的例子:

<?php
  switch($leve1)
 {
  case 3:
   echo “高级”;
  case 2:
       echo “中级”;
     case 1:
    echo “初级”;
  default:
    echo “错误的等级值”;
 }
?>
Copy after login

由此你想到了什么??

<?php
  $level = 3;
  switch($level)
 {
  case 3:
   echo “赋予管理员权限”;
  case 2:
    echo “赋予站务权限”;
  case 1:
     echo “赋予版主权限”;
  default:
   echo “赋予普通用户权限”;
 }
?>
Copy after login

与if相比switch达到了更高的效率:

<?php
  $a = 59;
 switch($a)
  {
 case $a == 100;
  echo “满分”;
  break;
 case $a >= 90;
  echo “优秀”;
  break;
 case $a >= 60;
  echo “及格”;
  break;
 default:
  echo “不及格”;
 }
?>
Copy after login

那么循环语句是干嘛用的呢?当然是用于反复地执行某一个操作。

While 与do···while

While的语法:

While(A)

 Statement;

解析:只要while表达式中的A为TRUE,就执行statement。

do···while的语法:

 do

 {

  Statements;

 }

 while(A)

do···while与while的区别只是在循环结束时do···while进行检查,不管循环的条件满足与否,do···while都将执行一次。

例如:

<?php
  $a = 5;          //先判断$a是否大于5,如果大于5则执行。
  while($a>5)
 {
  echo “This is  while.”;
  $a–;
 }
 do               //先执行do之内的语句,然后进行判断。
 {
  echo “This is do…while.”;
  $a–;
 }
 while($a > 5)
?>
Copy after login

For语句,语法:

 For(A;B;C)

  Statement;

分析:第一个表达式在循环开始时先无条件的执行一次,一般A都为赋值语句;B在循环开始前运行,如果为TRUE,

则继续循环,执行循环的嵌套语句;C在循环之后执行,一般都是自加自减运算。

代码:

 <?php
  for($a = 5;$a > 5;$a–);
  echo “This is for”;
 ?>
Copy after login

Foreach语句,用于数组的遍历,以后将会学到。

转移控制语句

PHP中主要有三种转移控制语句:break、continue和return。

1、 break语句

break语句用于结束当前循环,break可以接受一个可选的数字参数来决定跳出几重循环。

例子: 

<?php
  $a = 5;
  $b = 10;
 while($a <100)   //$a<100开始循环
 {
  echo “a = “.$a.”<BR>”;  //输出$a,“.”时连接运算符,相当于java中的“+”
 while($b > 0)           //$b>0,开始循环
 {
  echo “b = ” .$b.”<BR>”;  //输出$b
  $b–;
  if($b == 3 )                 //如果$b==3,则跳出while($b>0)
  break;              
 }
  $a++;
  if($a == 30)
  break;           //如果$a==30,就跳出while($a<100)
 }
 ?>
Copy after login

Continue语句

Continue用于跳出本次循环,与break不同的是,continue跳出后将继续执行下一次循环。 

Return语句 Return语句用于结束一个函数或者一个脚本文件。如果在一个函数中调用return语句将立即结束这个函数的执行,并将它的值作为参数返回。 

当然,在PHP中也可以将return当做一个函数来使用。如return(),并在括号内写上要返回的参数。这种用法并不常见。

The above is the detailed content of A simple tutorial on flow control statements of PHP basics. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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