循环结构
 一、while循环
 while(表达式)
{
 循环体;//反复执行,直到表达式为假
}

<?php
$num = 1;

while ($num <= 10){
    print "Number is $num<br />";
    $num++;
}

print 'Done.';
?>

Do While 循环语句与while有一定的区别,它们的区别就是do while不管条件是否为真都会先执行一下,而while必须为真才会执行一次.

do {
echo "Mmmmm...I love cookies! *munch munch munch*";
} while ($cookies > 1);

输出就是.

Mmmmm...I love cookies! *munch munch munch

三、for循环

根据循环条件不同,有两种类型的循环

一种:计数循环 (一般使用for)
另一种:条件型循环 (一般使用 while   do-while)

for (expr1; expr2; expr3) {
  statement
}

其中的 expr1 为条件的初始值。expr2 为判断的条件,通常都是用逻辑运算符号 (logical operators) 当判断的条件。expr3 为执行 statement 后要执行的部份,用来改变条件,供下次的循环判断,如加一..等等。而 statement 为符合条件的执行部分程序,若程序只有一行,可以省略大括号 {}。

下例是用 for 循环写的 "以后不敢了" 的例子,可以拿来和用 while 循环的比较。

<?php
for ($i=1; $i<=10; $i++) {
  echo "$i. 以后不敢了<br>n";
}
?>

输出表格

<HEAD>
  <TITLE>Value of Shares</TITLE>
  <H3>Table Showing Cost of Shares</H3>
  <BODY>
  <TABLE BORDER=1>
  <?php
  for ($shares = 1; $shares <= 20; $shares++){
     $cost = $shares * 20;
     echo "<TR><TD>The cost of $shares share/s is $cost #x0024s</TD>","n";
     $dividend = $cost * 0.10;
     echo "<TD>The dividend is $dividend #x0024s</TD></TR>  " ,"n";
  }
  ?>
  </TABLE>
  </BODY>
  </HTML>

累加计算

<?php
   $count = 1;
   while ($count < 5) {
      echo "$count squared = ".pow($count,2). "<br />";
       $count++;
   }
?>

do while 循环

<html>
<head>
<title>The do...while Statement</title>
</head>
<body>
<div>
<?php
    $num = 1;
    do {
      print "Execution number: $num<br />n";
      $num++;
    } while ( $num > 200 && $num < 400 );
?>
</div>
</body>
</html>

最新文章

  1. Eclipse新增Web项目
  2. spring3.0事务管理配置
  3. 隐藏nginx 版本号信息
  4. copyallwaterdata
  5. Python 主要模块和常用方法简览
  6. gulp和webpack初探
  7. Mac搭建Git/GitHub全过程
  8. CVE-2015-8660分析
  9. iOS 原生地图 开发
  10. 【BOI2007】【BZOJ1176】Mokia
  11. ios 当margin-left margin-right 超过设备宽度
  12. CF1103D Professional layer 状压DP
  13. 关于形如--error LNK2005: xxx 已经在 msvcrtd.lib ( MSVCR90D.dll ) 中定义--的问题分析解决
  14. ecshop常见sql注入修复(转)
  15. linux下svn不能连接上windows服务器:SSL handshake failed: SSL error
  16. Linux 学习总结(三)
  17. npm WARN react-native-maps@0.14.0 requires a peer of react@&gt;=15.4.0 but none was installed
  18. Python学习-28.Python中的列表切片
  19. Nginx1.8.1 编译扩展https
  20. Nginx基本介绍

热门文章

  1. 不是充许的静态以太网地址,它与vmware保留的mac地址冲突
  2. django之路由分组,路由分发,FBV,CBV,ORM框架
  3. node.js express 中文参考手册
  4. Leetcode 12,452,455-贪心算法
  5. Spring MVC 解读——@Autowired、@Controller、@Service从原理层面来分析
  6. Vue项目——去哪网(首页部分)
  7. 线程池三种队列使用,SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue
  8. 一次从Github向Gitee迁库踩坑记录
  9. JavaScript - String对象,字符串,String包装类型
  10. Python Tkinter学习笔记