search
Homephp教程php手册php 流程控制switch实例

switch允许对一个标量(表达式)的多个可能结果做选择。

语法:

switch (expr) {
    case result1:
    statement1
    break;
    case result2:
    statement2
    break;
    ……
    default:
    statement
}

系统计算expr的值,根据计算结果(result1、result2等)来选择下面对应执行语句,如果所有的case结果都不符合,则会执行default里的语句。

例子:

<?php
switch ($x) {
    case 0:
    echo "x 等于 0";
    break;
    case 1:
    echo "x 等于 1";
    break;
    case 2:
    echo "x 等于 2";
    break;
    default:
    echo "x 既不等于1和2,也不等于0";
}
?>

运行

提示

  • 可以有多个 case 条件判断
  • case 后面结果也不是仅限于数字,也可以是字符或其他 PHP 支持的类型
  • default 不是必须的

 

原文地址:http://www.manongjc.com/php/php_if_switch.html

 

相关阅读:

php getdate() 获取当前时间年月日时分秒星期

php date() 格式化日期时间

php date_sunset() 获取日落时间

php date_sunrise()根据地点(经度纬度)获取日出时间

php 全球合法时区列表大全

Statement
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

Video Face Swap

Video Face Swap

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

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

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

SecLists

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.