PHP Getting Started Tutorial-detailed description of process control statements-PHP Tutorial

Source: Internet
Author: User
A detailed description of the process control statements in the PHP Getting Started Tutorial. In php, the process control statements include the while, for, if, switch, foreach, and declare statements. The following describes how to use the process control statements, we hope this method includes the while, for, if, switch, foreach, and declare statements for each process control statement in php, the following describes how to use flow control statements. I hope this method will be rent out to all of you.

In PHP, a flow control statement consists of a conditional statement and a loop statement. Among them, the flow control statements include if/else and switch statements, and the loop statements include the while and for statements.

1. IF statement

In PHP, IF statements have two syntax structures. One is to use braces to represent statement blocks, and the other is to use colons to represent statement blocks. The former is generally used in pure code, while the latter is generally used in combination with HTML. The two methods are as follows:

The code is as follows:

// Braces indicate statement blocks
If ($ value ){
// Operation;
} Elseif ($ value ){
// Operation;
} Else {
// Operation;
}

// Colon indicates the statement Block
If ($ value ):
// Operation;
Elseif ($ value ):
// Operation;
Else:
// Operation;
Endif;

2. switch statement

The switch statement is similar to the C language. it can use numbers and strings as the judgment value. The code it executes starts with the first matching value (or default) and ends with a break. Therefore, do not forget to write break when writing a program. Otherwise, all branches will be executed. The sample code is as follows:

The code is as follows:

Switch ($ value ){
Case 'a ':
Case 'B ':
Echo 'value is a or B .';
Break;
Case 'C ':
Echo 'value is c .';
Break;
Default:
Echo 'value is others .';
Break;
}

3. while statement

The while statement, like the if statement, also uses two syntax structures. Braces indicate statement blocks and colons indicate statement blocks. The two methods are as follows:

The code is as follows:
// Braces indicate statement blocks
$ I = 0;
While ($ I <5 ){
Echo $ I;
}
// Colon indicates the statement Block
$ I = 0;
While ($ I <5 ):
Echo $ I;
Endwhile;

4. do... while statement

The difference between the do... while statement and the while statement is that the while statement is first judged and then executed, while the do... while statement is first executed and then judged. That is to say, even if the condition does not meet the requirements, the do... while statement will be executed once. The statement is as follows:

The code is as follows:

$ I = 0;
Do {
Echo $ I;
} While ($ I <5 );

5. for statement

In the for statement, you should note that it first executes the initialization code, and then runs the judgment statement for each loop. the execution loop then executes the auto-incrementing function. That is to say, if the conditions do not match, the loop will not be executed. There are two syntax structures. braces indicate statement blocks and colons indicate statement blocks. The two methods are as follows:

The code is as follows:
// Braces indicate statement blocks
For ($ I = 0; $ I <5; $ I ++ ){
Echo $ I;
}
// Colon indicates the statement Block
For ($ I = 0; $ I <5; $ I ++ ):
Echo $ I;
Endfor;

6. foreach statement

The foreach statement is used to traverse the entire array. Like for, its syntax structure also has two types. braces indicate statement blocks and colons indicate statement blocks. The two methods are as follows:

The code is as follows:
// Braces indicate statement blocks
Foreach ($ array as $ value ){
Each $ value;
}
// Colon indicates the statement Block
Foreach ($ array as $ value ):
Each $ value;
Endforeach;

7. declare statement

A declare statement is a function that is called after N statements are executed. For example

The code is as follows:
Register_tick_function ('test ');
Declare (ticks = 2 ){
Print '1 ';
Print '2 ';
Print '3 ';
Print '4 ';
Print '5 ';
}
Function test (){
Print 'descare
';
}

The result of this code is

1
2
3

...

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.