PHP basic conditional control statement study notes
条件控制语句主要有if、if…else、elseif和switch4种。 elseif,和此名称暗示的一样,是 if 和 else 的组合。和 else 一样,它延伸了 if 语句,可以在原来的 if 表达式值为 FALSE 时执行不同语句。但是和 else 不一样的是,它仅在 elseif 的条件表达式值为 TRUE时执行语句。
if语句
几乎所有的程序设计语言都有if语句,它按照条件选择执行不同的代码片段。PHP的if语句格式为:
if(expr) statement;
如果表达式expr值为真,那么就顺序执行statement语句,否则就会跳过该条语句,再往下执行,如果需要执行的语句不止一条,那么可以使用“{}”,在“{}”中的语句被称为语句组,格式为:
if(expr){ statement1; statement2; ...}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>PHP语言基础</title> </head> <body> <?php header("Content-Type:text/html; charset=gb2312"); $num=rand(1,20);//使用rand()函数生成一个随机数 if($num%2==0){ echo "\$num=$num"; echo "<br>$num 是偶数"; } ?> </body> </html>
运行结果:
$num=16
16 是偶数
if…else语句
大多数情况下,总是需要在满足某个条件时执行一条语句,而在不满足该条件时执行其他语句。这是可以使用if…else语句,语法格式为:
if(expr){ statement1; }else{ statement2; }
该语句的含义为:当表达式expr为真时,执行statement1;如果表达式expr为假,则执行statement2。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " <html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> <title>PHP语言基础</title> </head> <body> <?php header("Content-Type:text/html;charset=gb2312"); $num=rand(1,20);//使用rand()函数随机生成一个数 if($num%2==0){ echo "变量$num 是偶数"; }else{ echo "变量$num 是奇数"; } ?> </body> </html>
运行结果: 变量5 是奇数
elseif语句
if…else语句只能选择两种结果:要么执行真,要么执行假。但有时会出现两种以上的选择,这时可以使用elseif语句来执行,语法格式为:
if(expr1){ statement1; }else if(expr2){ }...else{ statementn; }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>PHP语言基础</title> </head> <body> <?php header("Content-Type:text/html; charset=gb2312"); $num=rand(1,20);//使用rand()函数随机生成一个数 if($num%2==0){ echo "变量$num 是偶数"; }else{ echo "变量$num 是奇数"; } ?> </body> </html>
switch…case多重判断语句
虽然elseif语句可以进行多重选择,但使用时十分繁琐。为了避免if语句过于冗长,提供程序的可读性,可以使用switch多重判断语句。语法格式如下:
switch(variable){ case value1: statement1; break; case value2: ... default: default statement; }
switch语句根据variable的值,依次与case中value值相比较,如果不相等,继续查找下一个case,如果相等,就执行对应的语句,直到switch语句结束或遇到break为止。一般来说,switch语句最终都有一个默认值default,如果在前面的case中没有找到相符的条件,则输出default后的语句,和else语句类似。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " <html xmlns=" <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> <title>PHP语言基础</title> </head> <body> <?php header("Content-Type:text/html;charset=gb2312"); $num=rand(0,4); switch($num){ case 0: echo "这是一个0"; break; case 1: echo "这是一个1"; break; default: echo "找不到了"; break; } ?> </body> </html>
输出结果:这是一个1
The above is the detailed content of PHP basic conditional control statement study notes. For more information, please follow other related articles on the PHP Chinese website!

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.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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

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