Table of Contents
if语句
if…else语句
elseif语句
判断语句" >switch…case多重判断语句
Home Backend Development PHP Tutorial PHP basic conditional control statement study notes

PHP basic conditional control statement study notes

Jun 22, 2017 am 10:05 AM
php Base study control condition statement

条件控制语句主要有if、if…else、elseif和switch4种。 elseif,和此名称暗示的一样,是 if 和 else 的组合。和 else 一样,它延伸了 if 语句,可以在原来的 if 表达式值为 FALSE 时执行不同语句。但是和 else 不一样的是,它仅在 elseif 的条件表达式值为 TRUE时执行语句。

if语句

几乎所有的程序设计语言都有if语句,它按照条件选择执行不同的代码片段。PHP的if语句格式为:

if(expr)
     statement;
Copy after login

如果表达式expr值为真,那么就顺序执行statement语句,否则就会跳过该条语句,再往下执行,如果需要执行的语句不止一条,那么可以使用“{}”,在“{}”中的语句被称为语句组,格式为:

if(expr){
     statement1;
     statement2;     ...}
Copy after login
<!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>
Copy after login

运行结果:
$num=16
16 是偶数

if…else语句

大多数情况下,总是需要在满足某个条件时执行一条语句,而在不满足该条件时执行其他语句。这是可以使用if…else语句,语法格式为:

if(expr){
     statement1;
}else{
     statement2;
}
Copy after login

该语句的含义为:当表达式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>
Copy after login

运行结果: 变量5 是奇数

elseif语句

if…else语句只能选择两种结果:要么执行真,要么执行假。但有时会出现两种以上的选择,这时可以使用elseif语句来执行,语法格式为:

if(expr1){
     statement1;
}else if(expr2){

}...else{
     statementn;
}
Copy after login
<!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>
Copy after login

switch…case多重判断语句

虽然elseif语句可以进行多重选择,但使用时十分繁琐。为了避免if语句过于冗长,提供程序的可读性,可以使用switch多重判断语句。语法格式如下:

switch(variable){
     case value1:
          statement1;          break;
     case value2:     ...
     default:
          default statement;
}
Copy after login

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>
Copy after login

输出结果:这是一个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!

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles