Heim  >  Artikel  >  Backend-Entwicklung  >  PHP-----循环结构_PHP教程

PHP-----循环结构_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:39:201077Durchsuche

for循环语句

打印金字塔

完整的金字塔

<span  1</span> <span //</span><span 打印金字塔</span>
<span  2</span>     <span $n</span>=25<span ;
</span><span  3</span>     <span for</span>(<span $i</span>=1;<span $i</span><=<span $n</span>;<span $i</span>++<span ){
</span><span  4</span>         <span //</span><span 空格循环</span>
<span  5</span>         <span for</span>(<span $k</span>=1;<span $k</span><=<span $n</span>-<span $i</span>;<span $k</span>++<span ){
</span><span  6</span>             <span echo</span> ' '<span ;
</span><span  7</span> <span         }
</span><span  8</span>         <span //</span><span 字符循环</span>
<span  9</span>         <span for</span>(<span $j</span>=1;<span $j</span><=<span $i</span>*2-1;<span $j</span>++<span ){
</span><span 10</span> 
<span 11</span>             <span if</span>(<span $i</span>==1 || <span $i</span>==<span $n</span><span ){
</span><span 12</span>                 <span echo</span> '.'<span ;
</span><span 13</span> <span             }
</span><span 14</span>             <span else</span><span {
</span><span 15</span>                 <span if</span>(<span $j</span>==1 || <span $j</span>==<span $i</span>*2-1<span ){
</span><span 16</span>                     <span echo</span> '.'<span ;
</span><span 17</span>                 }<span else</span><span {
</span><span 18</span>                     <span echo</span> ' '<span ;
</span><span 19</span> <span                 }
</span><span 20</span> <span             }
</span><span 21</span> <span         }
</span><span 22</span>         <span /*</span>
<span 23</span> <span         for($j=1;$j<=($i-1)*2+1 ; $j++) { 
</span><span 24</span> <span             echo '.';
</span><span 25</span> <span         }</span><span */</span>
<span 26</span>         <span echo</span> '<br/>'<span ;
</span><span 27</span> 
<span 28</span>     }

switch语句:

<span  1</span> <span /*</span><span $a="1";
</span><span  2</span> <span     switch ($a) {
</span><span  3</span> <span         case 1:
</span><span  4</span> <span             echo $a;
</span><span  5</span> <span             break;
</span><span  6</span>         
<span  7</span> <span         default:
</span><span  8</span> <span             echo "错误";
</span><span  9</span> <span             break;
</span><span 10</span> <span     }
</span><span 11</span> <span     //自动转换字符串和数字</span>

switch选择语句中遇到布尔值时的处理:

<span  1</span> <span $b</span>=<span true</span><span ;
</span><span  2</span>     <span switch</span>(<span $b</span><span ){
</span><span  3</span>         <span case</span> <span false</span>:
<span  4</span>             <span echo</span> "不匹配"<span ;
</span><span  5</span>             <span break</span><span ;
</span><span  6</span>         <span //</span><span 代表非false的值都可以为true-----自动转换类型</span>
<span  7</span>         <span case</span> "1":
<span  8</span>             <span echo</span> "成功匹配"<span ;
</span><span  9</span>             <span break</span><span ;
</span><span 10</span>         <span default</span>:
<span 11</span>             <span echo</span> "ko"<span ;
</span><span 12</span> <span     }
</span><span 13</span>     <span //</span><span 1.default语句不论顺序都是最后执行,如果没有匹配到其他的case,那么就执行default语句
</span><span 14</span> <span     //2.如果没有break语句,那么就会输出下一个case的结果,直到有break为止。</span>

while循环和do..while循环:

<span  1</span> <span /*</span><span while循环
</span><span  2</span> <span     $i=0;
</span><span  3</span> <span     while($i<10){
</span><span  4</span> <span         echo "paxster<br>".$i;
</span><span  5</span> <span         $i++;
</span><span  6</span> <span     }
</span><span  7</span> <span     //do..while循环--------先执行再判断,至少执行一次
</span><span  8</span> <span     /*$do=0;
</span><span  9</span> <span     do{
</span><span 10</span> <span         echo '<br />Paxster';
</span><span 11</span> <span         $do=$do+1;
</span><span 12</span> <span     }while($do<8);</span><span */</span>

while循环和switch选择语句的结合:

常量:

<span 1</span> <span //</span><span 定义常量-----两种方法</span>
<span 2</span>     <span define</span>('TAX',200<span );
</span><span 3</span>     <span echo</span><span  TAX; 
</span><span 4</span> 
<span 5</span>     <span const</span> Tab=100<span ;
</span><span 6</span>     <span echo</span> Tab;

编写简易计算器:

step1:编写输入界面

<span  1</span> <html>
<span  2</span> <head>
<span  3</span>     <title></title>
<span  4</span>     
<span  5</span> </head>
<span  6</span> <body>
<span  7</span> 
<span  8</span> <form action="CAL.php" method="get">
<span  9</span>     <input type="text" placeholder="输入一个数" name="num1">
<span 10</span>     <input type="text" placeholder="输入一个数" name="num2">
<span 11</span>     <select name="operation">
<span 12</span>         <option value="+">+</option>
<span 13</span>         <option value="-">-</option>
<span 14</span>         <option value="*">*</option>
<span 15</span>         <option value="/">/</option>
<span 16</span>     </select>
<span 17</span>     <input type="submit" value="计算">
<span 18</span> </form>
<span 19</span> </body>
<span 20</span> </html>

step2:写计算后台代码

<span  1</span> <?<span php
</span><span  2</span>     <span $num1</span>=<span $_REQUEST</span>['num1'<span ];
</span><span  3</span>     <span $num2</span>=<span $_REQUEST</span>['num2'<span ];
</span><span  4</span> 
<span  5</span>     <span $operation</span>=<span $_REQUEST</span>['operation'<span ];
</span><span  6</span>     <span $res</span>=0<span ;
</span><span  7</span> 
<span  8</span>     <span switch</span>(<span $operation</span><span ){
</span><span  9</span>         <span case</span> '+':
<span 10</span>             <span $res</span>=<span $num1</span>+<span $num2</span><span ;
</span><span 11</span>             <span break</span><span ;
</span><span 12</span>         <span case</span> '-':
<span 13</span>             <span $res</span>=<span $num1</span>-<span $num2</span><span ;
</span><span 14</span>             <span break</span><span ;
</span><span 15</span>         <span case</span> '*':
<span 16</span>             <span $res</span>=<span $num1</span>*<span $num2</span><span ;
</span><span 17</span>             <span break</span><span ;
</span><span 18</span>         <span case</span> '/':
<span 19</span>             <span $res</span>=<span $num1</span>/<span $num2</span><span ;
</span><span 20</span>             <span break</span><span ;
</span><span 21</span>         <span default</span>:
<span 22</span>             <span echo</span> '输入有误'<span ;
</span><span 23</span> <span     }
</span><span 24</span> 
<span 25</span>     <span echo</span> '结果是'.<span $res</span><span ;
</span><span 26</span> ?>

continue语句:跳过本次循环后面的代码。可以指定跳出指定的层数,例如continue 2;表示跳出两层,类似于break 2;

goto语句:和c语言一样,跳转到有标签的代码处,中间的代码不会执行,直接忽略。

<span  1</span> <span //</span><span goto语句
</span><span  2</span> <span     //i只执行一次</span>
<span  3</span>     <span for</span>(<span $i</span>=0,<span $j</span>=50;<span $i</span><100;<span $i</span>++<span ){
</span><span  4</span>         <span while</span>(<span $j</span>--<span ){
</span><span  5</span>             <span if</span>(<span $j</span>==17) goto <span end</span><span ;
</span><span  6</span> <span         }
</span><span  7</span> <span     }
</span><span  8</span>     <span echo</span> 'i=$i'<span ;
</span><span  9</span>     <span end</span>:
<span 10</span>     <span echo</span> 'i='.<span $i</span>.'j='.<span $j</span>;

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/732358.htmlTechArticlefor循环语句 打印金字塔 完整的金字塔 =25 ( =1; = ; ++ ( =1; = - ; ++ 'nbsp;' ( =1; = *2-1; ++ ( ==1 || == '.' ( ==1 || == *2-1 '.' } 'nbsp;' 'br/' } switch语句: s...
Stellungnahme:
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
Vorheriger Artikel:Hello World_PHP教程Nächster Artikel:phpStudy自带Ftp Server使用_PHP教程