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

慕斯
慕斯Original
2021-05-26 16:56:132211browse

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,此处代码将执行;
}

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 "你没有喜欢的电影!";
}
?>

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:
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