Example of switch statement usage in PHP

Source: Internet
Author: User
The switch statement is used to execute different actions based on different conditions. This article describes how to use the switch statement in php. it involves some knowledge about switch usage. if you are interested in switch usage, let's take a look at this article. switch is a switch statement, so many of my friends only know how to use the simple switch statement. The following is a brief introduction to the switch usage example.

It is called "advanced" usage only because I have not mastered the basic usage of the switch. so, I will talk about the basic usage of the switch!

The switch statement is similar to a series of IF statements with the same expression. In many cases, you need to compare the same variable (or expression) with many different values and execute different codes based on the value it equals. This is exactly the purpose of the switch statement.
Note: Unlike other languages, the function of the continue statement on the switch is similar to break. If there is a switch in the loop and you want to continue to the next cycle in the outer loop, use continue 2.

The following two examples use two different methods to implement the same thing: one uses a series of if statements and the other uses the switch statement:

Example #1 switch structure

<?phpif ($i == 0){ echo "i equals 0";}elseif ($i == 1){ echo "i equals 1";}elseif ($i == 2){ echo "i equals 2";}switch ($i){ case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break;}?>

Example #2 the switch structure can be a string

<?phpswitch ($i){ case "apple": echo "i is apple"; break; case "bar": echo "i is bar"; break; case "cake": echo "i is cake"; break;}?>

Key points: (this is what I have never mastered before !)

To avoid errors, it is very important to understand how the switch is executed. The switch statement is executed one by one row (in fact, it is a statement ). No code is executed at the beginning. PHP executes the statement only when the value in a case statement matches the value of the switch expression until the switch program segment ends (such as the return statement) or the first break statement is encountered. If break is not written at the end of the case statement segment, PHP will continue to execute the statement segment in the next case. For example:

<?phpswitch ($i){ case 0: echo "i equals 0"; case 1: echo "i equals 1"; case 2: echo "i equals 2";}?>

Note: If $ I is equal to 3, PHP will not execute any echo statement! However, if $ I is equal to 0, PHP will execute all the echo statements! If $ I is equal to 1, PHP will execute the following two echo statements. Only when $ I is equal to 2 will the expected result be returned -- only "I equals 2" will be displayed ". So don't forget that the break statement is very important (even if you deliberately want to avoid providing them in some cases ).

[Efficiency] in the switch statement, the condition is evaluated only once and used to compare with each case statement. In the elseif statement, the condition is evaluated again. If the condition is much more complex than a simple one, or in a loop that occurs many times, the use of the switch statement may be faster.

The statement in a case can also be empty, which transfers the control to the statement in the next case.

<?phpswitch ($i){ case 0: case 1: case 2: echo "i is less than 3 but not negative"; break; case 3: echo "i is 3";}?>

The special case of a case is default. It matches any situations that do not match other cases. For example:

<?phpswitch ($i){ case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; default: echo "i is not equal to 0, 1 or 2";}?> 

A case expression can be any simple expression, that is, an integer, a floating point, or a string. Arrays or objects cannot be used unless they are unbound from the simple type.

[Practice] based on the knowledge above, write a function: calculate the actual number of bytes for the capacity value.

<? Php/*** number of returned bytes ** @ param string $ val, for example, 400 M */function return_bytes ($ val = '') {$ val = trim ($ val ); $ last = strtolower ($ val {strlen ($ val)-1}); switch ($ last) {case 'g': $ val * = 1024; case'm ': $ val * = 1024; case 'K': $ val * = 1024;} return $ val;} $ memorylimit = ini_get ('memory _ limit '); echo $ memorylimit ,'
'; Echo return_bytes ($ memorylimit); output: 400m419213400

Note: When $ val = 1024 m, case 'm' is hit and $ val * =; is executed, but because there is no break language, therefore, it will continue to hit case 'K' and execute the $ val * = 1024; statement, so, which is equivalent to executing 400*1024*1024.

The above is all the descriptions in this article, hoping to help you learn the switch usage.

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.