首页  >  文章  >  后端开发  >  第五章_PHP流程控制_PHP教程

第五章_PHP流程控制_PHP教程

WBOY
WBOY原创
2016-07-13 17:17:25765浏览

  1.顺序结构

  2.分支结构

    2.1 if...else

<span 1</span> <?<span php
</span><span 2</span>     <span $today</span>=<span date</span>("w");   <span //</span><span 获取今天星期几</span>
<span 3</span>     <span if</span>(<span $today</span>==0<span ){
</span><span 4</span>         <span echo</span> 'Sunday!'<span ;
</span><span 5</span>     }<span else</span><span {
</span><span 6</span>         <span echo</span> 'Sorry!'<span ;
</span><span 7</span> <span     }
</span><span 8</span> ?>

    2.2 if...elseif

    2.3 switch

  3.循环结构

    3.1 while

<span 1</span> <?<span php
</span><span 2</span>     <span $num</span>=1<span ;
</span><span 3</span>     <span while</span>(<span $num</span><=100<span ){
</span><span 4</span>         <span echo</span> <span $num</span>.' '<span ;
</span><span 5</span>         <span $num</span>++<span ;
</span><span 6</span> <span     }
</span><span 7</span> ?>

    3.2 do...while

    与C/C++类似

    3.3 for

<span 1</span> <?<span php
</span><span 2</span>     <span $week</span>=<span array</span>('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'<span );
</span><span 3</span>     <span $size</span>=<span count</span>(<span $week</span><span );
</span><span 4</span>     <span echo</span> '数组大小'.<span $size</span>.'<p>'<span ;
</span><span 5</span>     <span for</span>(<span $i</span>=0;<span $i</span><<span $size</span>;<span $i</span>++<span ){
</span><span 6</span>         <span echo</span> <span $week</span>[<span $i</span>].' '<span ;
</span><span 7</span> <span     }
</span><span 8</span> ?>

    3.4 foreach

    foreach语句专门用来遍历数组的,有两种语法格式:

      ①foreach(数组名 as 元素名){

          语句块;

       }

      ②foreach(数组名 as 键名 => 元素名){

          语句块;

       }

前者举例如下:

<span 1</span> <?<span php
</span><span 2</span>     <span $week</span>=<span array</span>('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'<span );
</span><span 3</span>     <span $size</span>=<span count</span>(<span $week</span><span );
</span><span 4</span>     <span echo</span> '数组大小'.<span $size</span>.'<p>'<span ;
</span><span 5</span>     <span foreach</span>(<span $week</span> <span as</span> <span $value</span><span ){
</span><span 6</span>         <span echo</span> <span $value</span>.' '<span ;
</span><span 7</span> <span     }
</span><span 8</span> ?>

后者举例如下:

<span 1</span> <?<span php
</span><span 2</span>     <span $week</span>=<span array</span>('星期日'=>'Sunday','星期一'=>'Monday','星期二'=>'Tuesday','星期三'=>'Wednesday','星期四'=>'Thursday','星期五'=>'Friday','星期六'=>'Saturday'<span );
</span><span 3</span>     <span $size</span>=<span count</span>(<span $week</span><span );
</span><span 4</span>     <span echo</span> '数组大小'.<span $size</span>.'<p>'<span ;
</span><span 5</span>     <span foreach</span>(<span $week</span> <span as</span> <span $key</span>=><span $value</span><span ){
</span><span 6</span>         <span echo</span> <span $key</span>.':'.<span $value</span>.' '<span ;
</span><span 7</span> <span     }
</span><span 8</span> ?>

  4.break 与 continue

  break后面可以接一个数字,表示跳出第几重循环,continue后面也可以接一个数字,表示跳出第几重循环后面尚未执行的语句。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/626584.htmlTechArticle1.顺序结构 2.分支结构 2.1 if...else ? = ("w"); ( ==0 'Sunday!' } 'Sorry!' ? 2.2 if...elseif 2.3 switch 3.循环结构 3.1 while ? =1 ( =100 .' ' ++ ? 3.2 do...while 与C/C+...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn