Heim > Backend-Entwicklung > PHP-Tutorial > 第五章_PHP流程控制_PHP教程

第五章_PHP流程控制_PHP教程

WBOY
Freigeben: 2016-07-13 17:17:25
Original
774 Leute haben es durchsucht

  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> ?>
Nach dem Login kopieren

    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> ?>
Nach dem Login kopieren

    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> ?>
Nach dem Login kopieren

    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> ?>
Nach dem Login kopieren

后者举例如下:

<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> ?>
Nach dem Login kopieren

  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+...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage