search
Homephp教程php手册PHP入门教程之流程控制语句详解

PHP入门教程之流程控制语句详解

Jun 13, 2016 am 10:17 AM
forphpswitchwhileGetting Started TutorialexistcontrolprocessDetailed explanationstatement

在php中流程控制语句包括了while,for,if,switch,foreach,declare语句了,下面我来给各位入门者介绍一下流程控制语句使用方法,希望此方法对各位朋友有据租。

在PHP中,流控制语句主要有条件语句和循环语句组成。其中,流控制语句有if/else和switch语句;循环语句有while和for语句。

1. IF语句

在PHP中,IF语句有两种语法结构。一种用大括号表示语句块,一种用冒号表示语句块。前者一般用于纯代码中,后者一般用于代码和HTML结合时。两种写法如下:

 代码如下 复制代码

// 大括号表示语句块
if ($value) {
    // 操作;
} elseif($value) {
    // 操作;
} else {
    // 操作;
}

// 冒号表示语句块
if ($value) :
    // 操作;
elseif($value) :
    // 操作;
else :
    // 操作;
endif;

2. switch语句

switch语句和C语言有些相似,它可以使用数字和字符串做为判断值。它执行的代码是,从第一个匹配值(或者default)开始,到遇到break为止。所以,在写程序的时候一定不要忘记写break。不然会执行所有分支。代码示例如下:

 代码如下 复制代码

switch ($value) {
    case 'a':
    case 'b':
        echo 'value is a or b.';
        break;
    case 'c':
        echo 'value is c.';
        break;
    default:
        echo 'value is others.';
        break;
}

3. while语句

while语句和if语句一样,也用两种语法结构。大括号表示语句块以及冒号表示语句块表示语句块。两种写法如下:

 代码如下 复制代码
// 大括号表示语句块
$i = 0;
while ($i < 5) {
    echo $i;
}
// 冒号表示语句块
$i = 0;
while ($i < 5) :
    echo $i;
endwhile;

4. do...while语句

do...while语句和while语句区别是while语句先判断后执行,而do...while语句是先执行后判断。也就是说,即便条件不符合要求,do...while语句也会执行一次。其写法如下:

 代码如下 复制代码

$i = 0;
do {
    echo $i;
} while ($i < 5);

5. for语句

for语句里要注意的是,它先执行初始化代码,然后每一次循环首先执行判断语句,执行循环会再执行自增自减函数。也就是说,如果条件不符合,循环不会被执行。其语法结构也有两种,大括号表示语句块以及冒号表示语句块表示语句块。两种写法如下:

 代码如下 复制代码
 // 大括号表示语句块
for ($i = 0; $i < 5; $i++) {
    echo $i;
}
// 冒号表示语句块
for ($i = 0; $i < 5; $i++) :
    echo $i;
endfor;

6. foreach语句

foreach语句用于遍历整个数组。和for一样,它的语法结构也有两种,大括号表示语句块以及冒号表示语句块表示语句块。两种写法如下:

 代码如下 复制代码
// 大括号表示语句块
foreach($array as $value) {
    each $value;
}
// 冒号表示语句块
foreach($array as $value) :
    each $value;
endforeach;

7. declare语句

declare语句指每执行N条语句后调用一个函数。如

 代码如下 复制代码
register_tick_function('test');
declare(ticks = 2) {
    print '1';
    print '2';
    print '3';
    print '4';
    print '5';
}
function test() {
    print 'declare
';
}

这段代码的结果为

1
2
3

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software