PHP Switch Statement

Source: Internet
Author: User
Tags php switch

The Switch statement in PHP is used to perform different actions based on a number of different conditions.

You can avoid lengthy if by using the Switch statement frequently. ElseIf. else code block.

Instance

Working principle:

    1. Perform a single calculation on an expression (usually a variable)
    2. Compare the value of an expression with the value of a case in the structure
    3. If there is a match, the code associated with the case is executed
    4. After the code executes, 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
<?php/* Time: 2014-09-14 * Gechong * Function: Switch Small instance * *///three variables to record three dice points $var1 = rand (1,6), $var 2 = rand (1,6); $var 3 = rand (1,6);//Village Home singing dice points $sum = $var 1+ $var $var 3;//used to determine whether the leopard $sum3 = $sum% 3;if ($sum 3 = = 0) {    echo "Leopard, Dealer kill! "." <p> ";} else{    Switch ($sum) {    case  $sum <9:        echo "on". $var 1. ",". " $var 2 ".", "." $var 3 "." <p> "." $sum "." Point "." <p> "." Buy Xiao Sheng ~_~ "." <p> ";        break;    Default:        echo "on". $var 1. ",". " $var 2 ".", "." $var 3 "." <p> "." $sum "." Point "." <p> "." Buy a victory ^_^"." <p> ";        break;}}? >

  

PHP Switch Statement

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.