The various usages of the PHP switch method

Source: Internet
Author: User
Tags case statement php switch

The

is only so called "advanced" usage because I have not mastered the most basic usage of the switch, so, the following is actually the basic usage of it! The
 
Switch statement is similar to a series of IF statements that have the same expression. There are many situations where you need to compare the same variable (or expression) with a number of different values and execute different code based on which value it equals. This is the purpose of the switch statement.
Note: Note that unlike other languages, a continue statement acts on a switch similar to a break. If you have a switch in the loop and want to continue the next cycle in the outer loop, use continue 2.
The following two examples use two different methods for implementing the same thing, one with a series of if statements and the other with a switch statement:
Example #1 Switch structure

The code is as follows Copy Code

<?php
if ($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 switch structure can be used as a string

The code is as follows Copy Code
<?php
Switch ($i)
{
Case "Apple":
echo "I is Apple";
Break
Case "Bar":
echo "I is bar";
Break
Case "Cake":
echo "I is cake";
Break
}
?>


Key point: (This is the place that I have not mastered before!) )
To avoid errors, it is important to understand how the switch is performed. The switch statement executes one line at a line (actually a statement-taking statement). No code is executed at the beginning. PHP begins executing a statement only if the value in one case statement matches the value of the switch expression until the end of the switch's program segment (such as a return statement) or when the first break statement is encountered. If you do not write a break at the end of the statement section of the case, PHP will continue to execute the statement section in the next box. For example:

The code is as follows Copy Code

<?php
Switch ($i)
{
Case 0:
echo "I equals 0";
Case 1:
echo "I equals 1";
Case 2:
echo "I equals 2";
}
?>

Special Note: here if $i equals 3,php will not execute any ECHO statement! However, if $i equals 0,php will execute all the echo statements! If $i equals 1,php, the following two echo statements are executed. Only when the $i equals 2 o'clock will the result of "expected" be given--only "I equals 2" is displayed. So, don't forget that break statements are important (even if you deliberately avoid providing them in some cases).

[Efficiency] The condition in the switch statement is only one time and is used to compare with each case statement. The condition is evaluated again in the ElseIf statement. If the condition is more complex than a simple comparison or in a loop of many times, it may be quicker to use a switch statement.
The statement in a case can also be empty, which simply transfers control to the statement in the next box.

The code is as follows Copy Code

<?php
Switch ($i)
{
Case 0:
Case 1:
Case 2:
echo "I am less than 3 but not negative";
Break
Case 3:
echo "I is 3";
}
?>

The exception to a case is default. It matches anything that does not match any other case. For example:

The code is as follows Copy Code
<?php
Switch ($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 am not equal to 0, 1 or 2";
}
?>

A case expression can be any expression that is evaluated as a simple type, that is, an integer or floating-point number, and a string. You cannot use arrays or objects unless they are dereference as simple types.

"Combat" according to the above knowledge point, write such a function: Calculate the capacity value actually represents the number of bytes

  code is as follows copy code

<?php
/**
 * return bytes
 *
 * @param string $val such as 4 00M
 */
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;
 }< /p>

 return $val;
}

$memorylimit = ini_get (' Memory_limit ');
Echo $memorylimit, ' <br/> ';
Echo return_bytes ($memorylimit);

Output:
400M
419430400

Special Note: $val = 400M, case ' m ' is hit, its $val *= 1024; is executed, but because there is no break language, it will continue to hit case ' K ' and execute its $val *= 1024; statement, so, overall equivalent to the execution 400 * 1024 * 1024.

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.