Home Backend Development PHP Problem What is PHP Switch statement? Detailed explanation of switch statement usage

What is PHP Switch statement? Detailed explanation of switch statement usage

May 26, 2021 pm 04:56 PM

The previous article introduced you to "How do you understand the use of the string function library? Let’s discuss it together! ! ! ", this article continues to introduce to you what the PHP Switch statement is and how to use the Switch statement. If you are a "newbie", come and learn together! ! !

What is PHP Switch statement? Detailed explanation of switch statement usage

Usage of PHP Switch statement: The switch statement is used to perform different actions based on multiple different conditions.

The switch statement flow chart is as follows:

What is PHP Switch statement? Detailed explanation of switch statement usage

The syntax is as follows:

switch (n)
{
case 1:
如果 n=1,此处代码将执行;
break;
case 2:
如果 n=2,此处代码将执行;
break;
default:
如果 n 既不等于 1 也不等于 2,此处代码将执行;
}
Copy after login

Working principle: First, A simple expression n (usually a variable) is evaluated once. Compares the value of the expression to the value of each case in the structure. If there is a match, the code associated with the case is executed. After the code is executed, use break to prevent the code from jumping to the next case to continue execution. The default statement is executed when there is no match (that is, no case is true).

The following is a specific code demonstration:

<?php
$fiml="1";
switch ($fiml)
{
case "1":
    echo "你喜欢的电影是《我的姐姐》!";
    break;
case "2":
    echo "你喜欢的电影是《如果声音不记得》!";
    break;
case "3":
    echo "你喜欢的电影是《误杀》!";
    break;
default:
    echo "你没有喜欢的电影!";
}
?>
Copy after login

The running results are as follows:

What is PHP Switch statement? Detailed explanation of switch statement usage

Recommended learning: "PHP Video Tutorial

The above is the detailed content of What is PHP Switch statement? Detailed explanation of switch statement usage. 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 Article Tags

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)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

How to Implement message queues (RabbitMQ, Redis) in PHP?

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

Does PHP array deduplication need to be considered for performance losses?

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles