PHP Rookie (2) PHP Process Control

Source: Internet
Author: User

1. Sequential control

2. Condition control

Two if else and switch

(1) if;  if else; If else if ... else

$day _11_11= false;
if($day _11_11){
   Echo"today is Singles Day .";
}Else{
   Echo"today is not Singles Day .";
}

(2) switch (variable) {

Case value1:

Statement1;

Break;//break cannot be lost, otherwise all cases will be executed.

Case value2:

Statement2;

Break

.....

Default

Statement2;

Break

$weekday= Strftime ("%A");
Switch($weekday){
    Case"Monday":
       Echo"Today is Monday";
        Break;
    Case"Tuesday":
       Echo"Today is Tuesday";
        Break;
    Case"Wednesday":
       Echo"Today is Wednesday";
        Break;
    Case"Thursday":
       Echo"Today is Thursday";
        Break;
    Case"Friday":
       Echo"Today is Friday";
        Break;
    Case"Saturday":
       Echo"Today is Saturday";
        Break;
   default:
       Echo"Go out";
        Break;
}

3. Cycle control

Four while do...while for foreach

while (condition) {

Statement

}

do{

Statement

}while (condition);//Don't forget;

while (condition) {

Statement

}

for (EXPR1;EXPR2;EXPR3) {

Statement

}

==============================

Highlight the Foreach Loop

foreach is best at working with arrays

The syntax is foreach (array as $value) {//each time the loop assigns the value of the array element to $value and then the array pointer moves back

Statement

}

or foreach (array as $key = = $value) {//$key is an index $value is the array element value under the corresponding index

Statement

}

$price= Array("Lenono"="200dollars","Dell"="100dollars","ASUS"="Dollars");
foreach($price as$key=$value){
   Echothe price of$key is$value"."<br>";
}

Note: An error occurs when using a Foreach loop for a variable of a non-array type, so you should use the Is_array () function to determine if the variable is an array.

=======================================================

Use break to jump out of a loop directly when a condition is met.

Using continue, you can skip this loop and continue executing the loop statement when a certain condition is met.

This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://joedlut.blog.51cto.com/6570198/1852969

PHP Rookie (2) PHP Process Control

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.