PHP syntax 3: control structure For loop/If/Switch/While

Source: Internet
Author: User
: This article mainly introduces PHP syntax 3: control structure For loop IfSwitchWhile. if you are interested in PHP tutorials, refer to it. Related links:

PHP syntax(1): Basics and variables

PHP syntax(2): data types, operators, and functions

PHP syntax(3): control structure (For loop/If/Switch/While)

In this article, I will summarize several common control structures in PHP. first, I will give you the most special foreach. The remaining control structures are similar to those in other languages, so this phase will be relatively simple.

Foreach loop

Traverses each element in the array and loops through the code block.

Usage:foreach ( $array as $value )

Every iteration of an array, the value of the current array element is assigned to the $ value variable, and the array pointer is moved one by one until it reaches the last array element.

    $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 "number: $ x ";}

If judgment

If has no special usage.

If (condition) {code executed when the condition is true;} elseif (condition) {code executed when the condition is true;} else {code executed when the condition is 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 (condition true) {code to be executed ;}

Do while

Do... while loop first executes a code block and then checks the condition. if the specified condition is true, it repeats the loop.

Do {code to be executed;} while (the condition is true );

Last

Looking back at the past few days, I have seen many articles written by other bloggers in the garden "why not share. In fact, there are still many people willing to share, but there are too many reasons for not sharing. no time, laziness, and fear of jokes are all factors. For more information, seePHP syntaxI can read it in an hour, but if I want to organize my blog articles, I have to think about a lot of things. I just have to write a basic article for a few nights, which is really a blessing.

Now think about how to stick to everydayBlog WritingIt's really not easy. I would like to salute you!

I hope I can spend more than three days in a week.Blog WritingRight.

The above introduces PHP syntax 3: control structure For loop/If/Switch/While, including blog writing and PHP syntax content, if you are interested in PHP tutorials.

Related Article

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.