php流程控制
if语句,switch语句,while循环,do…while循环,for循环,foreach循环,break中断循环,continue指令。下面通过星期函数来演示这些。
程序三种流程控制结构
1、顺序结构
2、选择结构
3、循环结构
结果如下
代码如下 | 复制代码 |
$d=date("D"); if ($d=="Tue") echo "今天是星期二"; else echo "今天不是星期二"; ?> |
通过数组可以判断星期几。
执行结果应如下
代码如下 | 复制代码 |
$srttime=date("w",time()); |
1. if (条件一) {
?⑹?/p>
}
$a = 100 ;
if ( $a == 100 ) {
echo "A is 100. ";
}
?>
或
if (条件一) {
?⑹鲆?/p>
} else {
?⑹龆?/p>
}
$a = 120 ;
if ($a echo " A was small than 100. ";
}
else {
echo " A was big than 100. ";
}
?>
$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 {
?⑹?
}
$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>
}
$a = 1 ;
while ( $a echo "$a
";
$a++;
}
?>
3. do {
?⑹?/p>
} while (条件); //条件?檎? ??
$a = 1 ;
do {
echo "$a
";
$a++;
} while ( $a ?>
4. for (初始条件; 判?嗵跫? 条件改??⑹? {
?⑹?/p>
}
for ( $a = 1 ; $a {
echo "$a
";
}
?>
5. break //中?嗾?诘霓?圈
$abc[0] = '0' ;
$abc[1] = '1' ;
$abc[2] = '2' ;
$a = 0 ;
while ( $a {
if ( $abc[$a] == '2' ) { //若?檎? 跳?while()?圈
break;
}
echo "$a";
$a++;
}
?>
6. continue //中?嗾?诘霓?圈, 跳到?圈????下一次
for ($i=10 ; $i>1 ; $i--) {
if ($i == 2 ) { //若?檎? ?束?次的for()?圈, ??下一次for()?圈
continue;
}
echo "$i
";
}
?>
7. switch (条件) {
case 'case值1':
?⑹鲆?nbsp; //条件?case值相同,就?部分的?⑹? 直到遇到break才停止
break;
case 'case值2:
?⑹龆?br />
break;
............
default:
?⑹?
break;
}
switch ($i) {
case 0:
echo "i equals 0";
case 1:
echo "i equals 1";
case 2:
echo "i equals 2";
}
?>
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 中不能做这种特殊的定义。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
