php if else swicth条件控制语句学习笔记
在php中条件控制语句使用最多的就是if else或switch语句了,下面我来给大家详细介绍我在学习php中使用if else条件控制语句的笔记有需要的朋友可参考。
在php语言中,基本的流程控制结构有:顺序结构、分支结构、循环结构。
常用的控制语句主要有:
if、if…else判断语句
switch 分支语句
while、do…while循环语句
for 循环语句
break、continue中断语句
php if语句是一个简单的判断语句,即是实现条件判断的功能。当程序执行一段语句时,遇到二岔路口,通过判断语句的值是否满足条件而选择执行相对应的语句。所以if语句是最基本的php条件表达式。
基本结构为:
if(判断语句)
{
执行语句体
}
实例:
代码如下 | 复制代码 |
$a=8; |
if语句可以选择是否执行语句体,而if…else语句则是二选一,必须在两个语句体中选择一个来执行。可以解释为“若发生了什么事则怎么处理,否则该如何解决”,所以if…else本质上是一种选择性语句。
如果值为非0(即真),则执行语句体1,值为0(即假),则执行语句体2.
if…else语句的基本结构为:
if(判断语句)
{
执行语句体1
}
else
{
执行语句体2
}
实例:
代码如下 | 复制代码 |
$a=11; |
if…else语句只能实现两路分支,要实现多路分支用多个if…else语句嵌套即可。其结构形式如下:
if(判断语句1){
执行语句体1
}
else if(判断语句2){
执行语句体2
}
else if…
else…
实例:
代码如下 | 复制代码 |
$score=61; |
在if语句中可以嵌套多个if()语句,以实现多多个参数的判断,一般称为if语句多种嵌套,其基本结构形式如下:
if(判断1)
if(判断2) 语句体1
else 语句体2
else
…
这里应当注意if和else的配对关系,从内层开始,else总是与它上面最近的if配对,在编程时要特别小心。
实例:
代码如下 | 复制代码 |
$gender="女性"; |
前面介绍了if…else嵌套结构可以实现多分支选择的功能,不过这种方法代码比较冗长。实现多分支选择的功能,php还提供了switch语句。用switch语句事程序变得更加简明了。
基本结构:
switch(表达式){
case 1:
执行语句体1;
break;
case 2:
执行语句体2;
break;
...
default:
执行语句体n;
break;
}
表达式的值首先与case语句逐个匹配,如果匹配得上则执行该语句体,然后跳出循环。如果最终没有case语句匹配,则执行default后面的执行语句。
实例:
代码如下 | 复制代码 |
$i=0; |

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.
