PHP-----Loop Structure_PHP Tutorial

WBOY
Release: 2016-07-13 10:39:20
Original
1077 people have browsed it

for loop statement

Print Pyramid

Complete Pyramid

<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>     }
Copy after login

switch statement:

<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>
Copy after login

Handling when a Boolean value is encountered in the switch selection statement:

<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>
Copy after login

while loop and do..while loop:

<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>
Copy after login

The combination of while loop and switch selection statement:

Constant:

<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;
Copy after login

Write a simple calculator:

step1: Write input interface

<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>
Copy after login

step2: Write calculation background code

<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> ?>
Copy after login

continue statement: skip the code after this loop. You can specify the number of layers to jump out of, such as continue 2; it means jumping out of two layers, similar to break 2;

Goto statement: Just like the C language, jump to the labeled code, the code in the middle will not be executed and will be ignored directly.

<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>;
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/732358.htmlTechArticlefor loop statement prints pyramid complete pyramid = 25 ( =1; = ; ++ ( =1; = - ; ++ 'nbsp;' ( =1; = *2-1; ++ ( ==1 || == '.' ( ==1 || == *2-1 '.' } 'nbsp;' 'br/' } switch statement: s...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template