编程语言
首页 > 编程语言> > PHP中的分支及循环语句

PHP中的分支及循环语句

作者:互联网

这次实践的都是PHP7的语法。

感觉是以前的5差别不是那么大,只是希望越来越快吧。

<?php
    $looking = isset($_GET['title']) || isset($_GET['author'])
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <title>BookStore</title>
    </head>
    <body>
        <?php
        echo "Befor the conditional.<br/>";
        if (2 > 2) {
            echo "Inside the conditional.<br/>";
        } elseif ( 2 == 2) {
            echo "Print when 2 == 2.<br/>";
        } elseif ( 2 < 2) {
            echo "Not evaluted.<br/>";
        } else {
            echo "Inside the else.<br/>";
        }
        echo "After the conditional.<br/>";
        
        $title = 'Harry Potter';
        switch ($title) {
            case 'Harry Potter':
                echo 'Nice story. a bit too long.';
                break;
            case 'Load of the Rings':
                echo 'A classic!';
                break;
            default:
                echo 'Dunno that one.';
                break;
        }
        echo "<br/>";
        
        $i = 1;
        while ($i < 4) {
            echo $i . "<br/>";
            $i++;
        }
        echo "<br/>";
        $i = 1;
        do {
            echo $i . "<br/>";
            $i++;
        } while ($i < 0);
        
        for ($i = 1; $i < 10; $i++) {
            echo $i . "<br/>";
        }
        
        $names = ['Harry', 'Ron', 'Hermione'];
        for ($i = 0; $i < count($names); $i++) {
            echo $names[$i] . "<br/>";
        }
        
        foreach ($names as $name) {
            echo $name . "<br/>";
        }
        
        foreach ($names as $key => $name) {
            echo $key . ' -> ' . $name .  "<br/>";
        }
        ?>
        
    </body>
</html>

输出:

Befor the conditional.
Print when 2 == 2.
After the conditional.
Nice story. a bit too long.
1
2
3

1
1
2
3
4
5
6
7
8
9
Harry
Ron
Hermione
Harry
Ron
Hermione
0 -> Harry
1 -> Ron
2 -> Hermione

 

标签:语句,++,Hermione,conditional,echo,names,Harry,PHP,分支
来源: https://www.cnblogs.com/aguncn/p/11120146.html