Example of the use of switch statements in PHP _php examples

Source: Internet
Author: User
Tags case statement

Switch is a switch statement, so many friends only know the simple switch switch statement usage, the following tutorial small series for you to give a detailed introduction to switch usage examples.

Only so called "advanced" usage, because I have not even the most basic use of the switch is not mastered, so, the following is actually the basic use of it!

A 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 to achieve the same thing, one with a series of if statements and the other with a switch statement:

Example #1 Switch Structure

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

<?php
switch ($i)
{case
 "Apple":
 echo "I am apple";
 break;
 Case "Bar": The
 echo "I is bar";
 break;
 Case "Cake": The
 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:

<?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.

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

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

<?php
/**
 * Returns the number of bytes
 * *
 @param string $val such as 400M
/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, ' <br/> ';
echo return_bytes ($memorylimit);
Output:
400M
419430400

Special Note: $val = 400M, case ' m ' was hit, under the $val *= 1024; is executed, but because there is no break language, it continues to hit case ' K ' and executes its $val *= 1024; The statement, so, is generally equivalent to executing 400 * 1024 * 1024.

The above is the entire description of this article, I hope that you learn the switch usage is helpful.

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.