PHP syntax Three: control structure for Loop/if/switch/while

Source: Internet
Author: User
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.

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