首頁 > 後端開發 > php教程 > 第五章_PHP流程控制_PHP教程

第五章_PHP流程控制_PHP教程

WBOY
發布: 2016-07-13 17:17:25
原創
775 人瀏覽過

  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+...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板