PHP switch Statement usage detailed _php tips

Source: Internet
Author: User
Tags php switch

This article describes the use of switch statements in PHP, which is similar to the switch usage in other statements, but note that there is a break statement.

Standard syntax for switch statements in PHP:

switch (expression)
{case
Label1: The code to is
 executed if expression = Label1;
 break; 
Case LABEL2: The code to is
 executed if expression = Label2;
 break;
Default:
 code to was executed
 if expression is different from both Label1 and Label2;

Example:

Switch ($i) {case
  1:
    echo 1;
    break;
  Case 2:
    Echo 2;
    break;
  Default:
    echo ' others ';
}

You can also use a switch to determine a range of values, or to customize a condition in a case.

<?php
Header ("Content-type:text/html;charset=utf8");
$score =50;
Switch ($score) 
{case
 $score >=90 && $score <=100:
  echo "excellent <br>";
  break;
 Case $score >=80 && $score <90:
  echo "Liang <br>";
  break;
 Case $score >=70 && $score <80:
  echo "<br>";
  break;
 Case $score >=60 && $score <70:
  echo "pass <br>";
  break;
 Case $score >=0 && $score <60:
  echo "Less than lattice <br>";
  break;
 Default:
  echo "score input error <br>";
>

Simple example

<?php//switch Details/Situation one: When the numerical match is automatically converted into a string $a = 1; 
    Switch ($a) {case "1": Echo ' hello1 '; 
  Break 
    Default:echo ' Sorry none is the same! '; 
 Break } Echo ' <br/> '. ' 
 Successful exit "; 
  Echo '  

Run results

Hello1
Successful Exit
Hello2
Successful Exit
Hello3
Successful Exit
Hello4
Successful Exit
Hello5
Successful Exit
Hello6
Successful Exit
Hello7
Successful Exit
hello81
Successful Exit
I'm exit 5th,
exit
hello61. Hello6, I'm number 2nd. Exit
successfully

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.