PHP入门教程之流程控制语句详解
在php中流程控制语句包括了while,for,if,switch,foreach,declare语句了,下面我来给各位入门者介绍一下流程控制语句使用方法,希望此方法对各位朋友有据租。
在PHP中,流控制语句主要有条件语句和循环语句组成。其中,流控制语句有if/else和switch语句;循环语句有while和for语句。
1. IF语句
在PHP中,IF语句有两种语法结构。一种用大括号表示语句块,一种用冒号表示语句块。前者一般用于纯代码中,后者一般用于代码和HTML结合时。两种写法如下:
代码如下 | 复制代码 |
// 大括号表示语句块 // 冒号表示语句块 |
2. switch语句
switch语句和C语言有些相似,它可以使用数字和字符串做为判断值。它执行的代码是,从第一个匹配值(或者default)开始,到遇到break为止。所以,在写程序的时候一定不要忘记写break。不然会执行所有分支。代码示例如下:
代码如下 | 复制代码 |
switch ($value) { |
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; |
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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