Maison > php教程 > php手册 > 第五章_PHP流程控制

第五章_PHP流程控制

WBOY
Libérer: 2016-06-13 10:17:52
original
1458 Les gens l'ont consulté

  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> ?>
Copier après la connexion

    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> ?>
Copier après la connexion

    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> ?>
Copier après la connexion

    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> ?>
Copier après la connexion

后者举例如下:

<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> ?>
Copier après la connexion

  4.break 与 continue

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

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal