Home php教程 php手册 php中for while循环语句学习笔记

php中for while循环语句学习笔记

Jun 13, 2016 am 10:16 AM
for php while introduce Basic study cycle article of notes statement

本文章来给大家介绍php中基本的循环语句的使用方法,包括for循环,while循环与do while循环的使用方法,本文章很适合于php初学入门者哦。

用for语句可以控制多个变量,从而实现多重循环的高级应用。

下面看一个用for语句输出9*9乘法表的程序:

 代码如下 复制代码

  for($i=1;$i     for($j=1;$j    $sum=$i*$j;
   echo $sum ."t";
 }
 echo "
";
  } 
?>

除了前面提到的while循环语句,php还提供了for循环语句实现同样的功能。而且for语句可以实现更为复杂更多功能的循环,任何while循环都可以用for循环来代替。

基本结构形式:

for(表达式1;表达式2;表达式3){
    执行语句体
}

其执行过程如下:

1、先执行表达式1;

2、接着判断表达式2的真假,若为假则跳出for循环执行下一个php语句,若为真则进入for循环执行语句体;

3、然后执行表达式3;

4、返回第2步循环运行;

5、直至循环结束跳出for语句。

流程图:

实例:

 代码如下 复制代码

for($i=0;$i  $sum+=$i;
 echo $sum ."t";
}
?>


do…while循环语句是while循环的变体,功能类似于while,只是在其执行循环后再检查表达式是否为真,基本结构为:

 do{;
 语句体
}while(表达式)
do…while循环语句先执行一次语句体,然后判断表达式的条件,如果值为真则返回再循环一次,为假则跳出循环。

实例:

 代码如下 复制代码

 $i=1;
 $sum=0;
 do{
  $sum +=$i;
  echo $sum ."n";
  $i++;
 }while($i ?>

 

while循环语句
while语句是php程序中用来实现循环的语句,其基本结构如下:

while(判断语句){
 执行语句体;
}
判断语句一般用关系运算符或者逻辑运算符作为判断条件。

当判断语句为真实则执行语句体,然后再检查表达式的值,如果仍然为真,则执行语句再次被执行。直到判断语句为假时退出循环。

实例:

 代码如下 复制代码

 $i=0;
 while($i   $i++;
  echo $i ."
";
 }
?>


while和do…while的区别:

两者的主要区别是do…while语句的第一次循环肯定要执行。

若两者循环体执行语句相同,它们的运行结果一般也相同,但在表达式一开始为假时,两种循环的运行结果就有所不同。

实例:

 代码如下 复制代码

 /* while循环 */
 $a=9;  
 while($a>10){
  echo "进入while循环语句体";
 }
 /* do...while循环 */
 do{   
  echo "进入do...while循环语句体";
 }while($a>10) 
?>


php退出循环的方法有break和continu语句,它们的作用都是当判断条件满足时则跳出循环程序。

break语句用法:
当判断条件的值为真时提前结束整个循环,接着执行循环以外的语句。

实例:

 代码如下 复制代码

 /* 输出面积在100以内的圆形面积 */
 for($r=1;;$r++){
  $A=3.14*$r*$r;
  if($A>50) break; /* 如果没有break则形成死循环 */
  echo $A ."
";
 }
?>

continue语句用法:
continue语句的作用是结束本次循环而进入下一次循环,并不是退出整个循环程序。

实例:

 代码如下 复制代码

 /* 输出10以内的单数 */
 for($i=1;$i   if($i%2==0) continue;
  echo $i ."t";
 }
?>

break和continue语句的区别:

从上面的例子我们可以看到,break和continue语句在退出循环的作用是有着本质区别的。

continue只是结束本次循环,接着再返回循环体继续执行下次循环;

break则是立即终止整个循环,不再重复执行。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles