PHP Process Control Statement _php Tutorial

Source: Internet
Author: User
This article is about PHP two process judgment statements, there are switch statements and if else, if () statements, let us take a closer look at the example bar.

The code is as follows Copy Code
Switch (variable) {
Case value1:
Statement1;
Break
Case value2:
...
Default
Defulat statement;
}

The switch statement, based on the value of the variable, is compared with the value in case, and if not equal, continues to find the next
case, if equal, executes the corresponding statement until the switch statement ends or a break is encountered.

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

Example Two

The code is as follows Copy Code

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


Example Three

The code is as follows Copy Code

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

The If...else statement can only select two results: either execute the Truth or perform a false. But now there are more than 2 kinds of options to do
It? At this point, you can use Esleif (or you can write the else if) statement to execute, which is the syntax format:

The code is as follows Copy Code
if (expression1) {
Statement1;
}else if (expression2) {
Statement2;
}
...
else{
STATEMENTN;
}

Instance

The code is as follows Copy Code
$moth = Date ("n");
$today = Date ("J");
if ($today >= 1and $today <= 10) {
Echo ' Today is '. $moth. ' Month ' $today. ' Early days ';
}elseif ($today >10 and $today <=20) {
Echo ' Today is '. $moth. ' Month ' $today. ' mid-day ';
}else{
Echo ' Today is '. $moth. ' Month ' $today. ' Late in the day ';
}
?>

If the current date is Friday, the following example outputs "a nice weekend!", and if it is Sunday, the output "has a nice sunday!", otherwise the output "has a nice day!" :

The code is as follows Copy Code


$d =date ("D");
if ($d = = "Fri")
echo "has a nice weekend!";
ElseIf ($d = = "Sun")
echo "has a nice sunday!";
Else
echo "has a nice day!";
?>


http://www.bkjia.com/PHPjc/629271.html www.bkjia.com true http://www.bkjia.com/PHPjc/629271.html techarticle This article is about PHP two process judgment statements, there are switch statements and if else, if () statements, let us take a closer look at the example bar. The code below copy code switch (variable) {ca ...

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