RELATED Links:
PHP Syntax (i): Foundations and variables
PHP Syntax (b): Data types, operators, and functions
PHP Syntax (iii): Control structure (for loop/if/switch/while)
This article I will summarize a few PHP commonly used control structure, first to the most special foreach, the rest of the control structure is similar to other languages, then this period is relatively simple.
foreach Loop
Iterate through each element in the array and loop through the block of code.
Usage:foreach ( $array as $value )
For each iteration of the loop, the value of the current array element is assigned to the $value variable, and the array pointer moves one at a time until the last array element is reached.
$age=array("Bill"=>"35","Steve"=>"37","Peter"=>"43"); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; }
For loop
The basic for loop is as follows:
for ($x=0; $x<=10; $x++) { echo "数字是:$x";
If judgment
If there is no particular usage.
if (条件) { 条件为 true 时执行的代码;} elseif (condition) { 条件为 true 时执行的代码;} else { 条件为 false 时执行的代码;}
Switch
switch is similar to C + +.
switch ($x){case 1: echo "Number 1"; break;case 2: echo "Number 2"; break;default: echo "No number between 1 and 3";}
While
while (条件为真) { 要执行的代码;}
Do While
The Do...while Loop executes the code block first, then checks the condition and repeats the loop if the specified condition is true.
do { 要执行的代码;} while (条件为真);
At last
Review the course of these days, and see the garden of other bloggers wrote "Why not share" article, feeling a lot. Actually willing to share a lot of people, but do not share the reasons for too many, no time, lazy, afraid of being jokes are factors. Originally read PHP syntax One hours can be read, but to organize into a blog post, it is necessary to think a lot of things, just write a basic article a few nights of time will not have, really make people happy mixed.
Now think of the people who can insist on blogging every day is really not easy, to salute you!
I hope I can get more than three days ' free time in a week to write a blog .
The above introduces the PHP syntax three: control structure for the loop/if/switch/while, including writing blog, PHP syntax content, I hope the PHP tutorial interested in a friend helpful.