Home  >  Article  >  Backend Development  >  PHP4 Practical Application Experience (7)_PHP Tutorial

PHP4 Practical Application Experience (7)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:04:21888browse

Author: Sun Movement

Another control statement that has almost the same function as the "if-else" family control statement is PHP's "switch" statement. It will look like this:


---------------------------------- -----------------------------------------------
switch (Decision variable)
{
case The first condition is correct:
do this!

case The second condition is correct:
do this!

case The third condition is correct:
do this!

... Wait...

}
-------------- -------------------------------------------------- ----------------
We will make this clearer by rewriting the fortune cookie example above using a "switch" statement:

[cookie.php]

--------------------------------------------- -------------------------------------
< ?

// The decision variable here is the date selected by the user
switch ($day)
{

// The first case
case "Monday":
$ fortune = "When you can find a way to make everything complicated and exciting, don't make it simple and effective";
break;

// Second case
case "Tuesday" :
$fortune = "Life is the bridge of the game? - You must have used some kind of trick.";
break;
// The third case
case "Wednesday":
$fortune = "What can make a sane person live in this world and never go crazy?";
break;
// The fourth case
case "Thursday":
$fortune = "Don't be crazy, be interesting";
break;
// The fourth case
case "Friday":
$fortune = "Just follow the times, go with the trend, when you get When you upgrade, you will find that type is a devil. ";
break;


// If none of the above conditions are met...
default:
$fortune = "Sorry. , closed on weekends";
break;

}

?>

< html>
< head>
< basefont face ="Arial">
< /head>

< body>
This is your lucky word for < ? echo $day; ?>:
< br> ;
< b>< ? echo $fortune; ?>< /b>

< /body>
< /html>
----- -------------------------------------------------- --------------------------
There are two important keywords: the keyword "break" is used to break the "switch" statement structure and immediately moves outside the structure block, then runs the line immediately following the block. The "default" keyword is used to execute a statement that is set when the variables passed to "switch" do not match any of the conditions listed in the statement structure.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315995.htmlTechArticleAuthor: Sun Movement Another control statement that has almost the same function as the if-else family control statement is the PHP switch statement. It will look like this: ---------------------------------------------- ----------...
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