PHP process control_PHP tutorial

WBOY
Release: 2016-07-13 17:14:02
Original
1223 people have browsed it

if statement, switch statement, while loop, do...while loop, for loop, foreach loop, break interrupt loop, continue instruction. These are demonstrated below using the day of the week function.

Three process control structures of the program

1. Sequential structure
2. Select structure
3. Loop structure

The results are as follows

The code is as follows Copy code
$d=date("D");
 代码如下 复制代码
$d=date("D");
if ($d=="Tue")
echo "今天是星期二";
else
echo "今天不是星期二";
?>
if ($d=="Tue")

echo "Today is Tuesday";

else

echo "Today is not Tuesday";

?>
 代码如下 复制代码

$srttime=date("w",time());
$array=array('天','一','二','三','四','五','六');
$todaytime=date("Y年m月d日 星期{$array[$srttime]}",time());
echo $todaytime;
?>

You can determine the day of the week through the array. The execution result should be as follows
The code is as follows Copy code
$srttime=date("w",time()); $array=array('天','一','二','三','四','五','六'); $todaytime=date("Y year m month d day week {$array[$srttime]}",time()); echo $todaytime; ?>

1. if (条件一) {

        ?⑹?/p>

    }   



IF - 1


    $a = 100 ;
    if ( $a == 100 ) {
    echo "A is 100. ";
    }
?>

           

    或

    if (条件一) {

        ?⑹鲆?/p>

    } else {

        ?⑹龆?/p>

    }



IF … ELSE - 1


            $a = 120 ;
        if ($a < 100 ) {
            echo " A was small than 100. ";
       }
       else {
            echo " A was big than 100. ";
       }
?>

              

 



IF … ELSE - 2


            $file = "files.txt" ;
        if ( file_exists($file) ) {        //?z查?n案是否存在
            echo " ?是?n案 files.txt的?热?
";
            readfile ($file) ;                  //?出?n案?热?br />         }
        else {
            echo " ?o此?n案
";
        }
    ?>

              


    或

    if (条件一) {

        ?⑹鲆?/p>

    } elseif {

        ?⑹龆?/p>

    } elseif {

        ?⑹鋈?/p>

    }

    ...........

    else {

        ?⑹?

    }

   
   
    IF … ELSEIF … ELSE - 1
   
   
                    $a = 100 ;
            $b = 200 ;
            if ($a > $b) {
                echo "a is bigger than b";

            } elseif ($a == $b) {
                echo "a is equal to b";

            } else {
                print "a is smaller than b";
            }
        ?>

                      


2. while (条件一) {  //条件?檎? ??

        ?⑹鲆?/p>

    }



WHILE


            $a = 1 ;
        while ( $a < 10 ) {
            echo "$a
";
            $a++;
        }
    ?>

                  

 

3. do {

            ?⑹?/p>

   } while (条件);  //条件?檎? ??



Do .. WHILE - 1


            $a = 1 ;
        do {
            echo "$a
";
            $a++;
        } while ( $a < 10 )
    ?>

                 

 

4. for (初始条件; 判?嗵跫? 条件改??⑹? {

        ?⑹?/p>

    }



For - 1


            for ( $a = 1 ; $a < 10 ; $a++ )
        {
            echo "$a
";
        }
    ?>

                 


   

5. break            //中?嗾?诘霓?圈



BREAK


            $abc[0] = '0' ;
        $abc[1] = '1' ;
        $abc[2] = '2' ;
        $a = 0 ;
        while ( $a < 4 )
        {
            if ( $abc[$a] == '2' ) {    //若?檎? 跳?while()?圈
            break;
        }
            echo "$a";
            $a++;
        }
    ?>

                 


6. continue        //中?嗾?诘霓?圈, 跳到?圈????下一次



CONTINUE


for ($i=10; $i>1; $i--) {
                                                                                                                                                                                                     continue;
            }
echo "$i
";
            }
?>

                                                          

7. switch (condition) {

case 'case value 1':
​​​              break;
case 'case value 2:
                                                                    break;
                                                  …                                                                  but                                           default:
                      ?⑹?
                 break;

}

SWITCH



switch ($i) {
case 0:
                     echo "i equals 0";
case 1:
                    echo "i equals 1";
case 2:
                       echo "i equals 2";
          }
?>

                                                               




Switch - SWITCH


            switch ( $a ) {
            case '1':
                echo "one";
                break;
            case '2':
                echo "two";
                break;
            case '3':
                echo "three";
                break;
            case '4':
                echo "four";
                break;
            case '5':
                echo "five";
                break;
            default:
                echo "ZERO";
                break;
        }
?>

    

注意事项

PHP 语法中在每条指令结束时都要加上分号 ;,但是在部分结尾符号 } 后面不用加上分号结束。
在流程的部分分隔符号上,都是使用 { 当作部分的开头,用 } 当作结尾,和 C 语言相同。不过 C 可以定义 begin 当开头、end 当结尾 (像 Pascal),而 PHP 中不能做这种特殊的定义。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/629020.htmlTechArticleif语句,switch语句,while循环,do…while循环,for循环,foreach循环,break中断循环,continue指令。下面通过星期函数来演示这些。 程序三种流程...
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