Php Switch statement usage instructions

Source: Internet
Author: User
Tags php switch

Switch statement

Working principle:

1. Perform a calculation on the expression (usually a variable).
2. Compare the expression value with the case value in the structure.
3. If a match exists, execute the code associated with the case
4. After the code is executed, the break statement prevents the code from jumping into the next case to continue execution.
5. If no case is true, use the default statement.


If you want to selectively execute one of several code blocks, use the Switch statement.

Use the Switch statement to avoid lengthy code blocks such as if... elseif... else.

Syntax

The code is as follows: Copy code

Switch (expression)
{
Case label1:
Code to be executed if expression = label1;
Break;
Case label2:
Code to be executed if expression = label2;
Break;
Default:
Code to be executed
If expression is different
From both label1 and label2;
}

Instance

Working principle:

1. Perform a calculation on the expression (usually a variable).
2. Compare the expression value with the case value in the structure.
3. If a match exists, execute the code associated with the case
4. After the code is executed, the break statement prevents the code from jumping into the next case to continue execution.
5. If no case is true, use the default statement.

The code is as follows: Copy code


<? Php
Switch ($ x)
{
Case 1:
Echo "Number 1 ";
Break;
Case 2:
Echo "Number 2 ";
Break;
Case 3:
Echo "Number 3 ";
Break;
Default:
Echo "No number between 1 and 3 ";
}
?>

</Body>
</Html>


In another example, the switch is used to implement a multi-purpose page. First, the test. php page is created:

The code is as follows: Copy code

<? Php
Echo "<a href = 'solution. php? Action = add'> add </a> <br> ";
Echo "<a href = 'solution. php? Action = del '> delete </a> <br> ";
Echo "<a href = 'solution. php? Action = search'> search </a> <br> ";
Echo "<a href = 'solution. php? Action = update'> update </a> ";
?>

Of course, sunec omitted most of the other code, but wrote some of the submit buttons. We can see that no matter which button we click, we will jump to the solution. php page. The only difference is that the content of the action after the question mark is divided into four types. We can call it a kind of prompt.

Next, let's see how solution. php handles these four operations.

The code is as follows: Copy code

<? Php
$ Action = $ _ GET ["action"];

Switch ($ action)
{
Case "add ":
Echo "now supports adding features! ";
Break;
Case "del ":
Echo "now supports deletion! ";
Break;
Case "search ":
Echo "now supports the query function! ";
Break;
Case "update ":
Echo "update is now available! ";
Break;
}
?>

Let's look at a simple instance.

The code is as follows: Copy code

<Html>
<Head>
<Title> A switch Statement </title>
</Head>
<Body>
<? Php
$ Mood = 'sad ';
Switch ($ mood ){
Case 'Happy ':
Print 'I'm in a good mood ';
Break;
Case 'sad ':
Print 'Don' t be down! ';
Break;
Default:
Print 'neither happy nor sad but $ mood ';
}
?>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.