PHP Reading Notes Collation _ Structure Statement _php example

Source: Internet
Author: User

PHP Structure Statement order structure

The order structure is like a straight line, which is carried down in order. The code we write is executed by default in sequential structure.

The if...else of the condition structure ...

Conditional structure is like a fork in the road, you can go to the left, you can go right. Like going to the bathroom, we know our sex, at this time we need to according to the conditions provided by the toilet, the left men's room, the right women's room, or just the opposite, where the gender is the condition of the structure. For example, now the score is popular using A, B, C to grade, assuming that the test score is 93 points, you can set it to grade A, the test score is 87, you can set it to level B, where the fractional interval is the condition of the structure.

The "If...else ..." syntax in PHP is as follows:

<?php
if (condition) {
   //Assign Server Dry task a
}else{
   //Assign server Dry task B
}
?>

The condition determines that if the return value is a Boolean value of true, then task A is performed and task B is executed if the return value is false.

If...else of conditional structure if ...

The "If...else If ..." Syntax in PHP is as follows:

<?php
if (condition i) {
   //Assign Server Dry task a
}else if (condition II) {
   //assign server Dry task B
}
?>

By a conditional one, if the return value is a Boolean value of true, then task A, and if the return value is false, the condition two is judged, and if the return value is a Boolean value true, then task B is executed, or neither task a nor task B is executed. The server will continue to perform other tasks.

The switch...case of the condition structure ...

The "Switch...case ..." syntax in PHP is as follows:

<?php
Switch (condition)
{case
condition value one:
 //Task one break
 ; 
Case condition Value Two:
 //Task two break
 ;
Default:
 //Defaults task
}
?>

First judge the condition, if the condition return value is the condition value one, then executes the task one, if the condition returns the value is the condition value two, then executes the task two, if the condition return value is neither the condition value one also is not the condition value two, then executes the default task. The function of the break is to end the switch, and using the switch statement avoids the lengthy "if". else If.. Else "code block.

The function of a break is to prevent the code from entering the next case to continue execution.

While loop statement in PHP loop structure

The circular structure is like running a football field in a circle, running a lap and then running a lap. That is, you perform a task repeatedly under conditions that match. Like 400.1-meter laps of the runway, run 800 meters to run 2 laps, when the first lap and then run the second lap, the end of the second round has reached 800 meters, stop running.

In PHP, the while loop statement is as follows:

<?php while
(condition) { 
   //Perform task
}
?>

First of all, determine whether a condition is met (the condition return value is true), if the compliance then perform the task, the completion of the task, and then judge whether the conditions meet the requirements, the compliance is repeated to perform this task, or end the task.

The Do While loop statement of the loop structure in PHP

There is another type of looping statement in PHP: The Do...while loop syntax is as follows:

<?php
 do{ 
   //Perform task
 }while (condition)
 ?>

Perform the task first (the while statement in the previous section is the first to determine whether the condition is set up, then perform the task), perform the task, determine whether a condition is met (the conditional return value is true), perform the task again if it is met, perform the task, and continue to determine the condition.

The difference between a while and a do...while statement in a cyclic structure in PHP

The difference between a while and a do...while loop statement is that while the first determines whether the condition is set up, then executes the loop, Do...while performs a task first and then determines whether to continue the loop, which means that Do...while will perform at least one task at a time. When the condition is false, the tasks in the while are not executed once, and the tasks in the do...while are executed 1 times.

Loop structure in PHP for Loop statement

In PHP There is also a loop statement, the FOR Loop statement structure is as follows:

<?php for
 (initialize; loop condition; increment item) {
    //Perform task
 }
 ?>

For statement, initialization is evaluated unconditionally before the start of the loop, and the loop condition is evaluated before each loop begins. If the value is TRUE, the loop continues, executing the loop-body statement (performing the task). If the value is FALSE, the loop is terminated. The increment item is evaluated after each loop (execution). It is often used to iterate through the specified number of code blocks.

A Foreach Loop statement for looping structures in PHP

In PHP, the Foreach Loop statement, often used to traverse an array, is generally used in two ways: do not remove the subscript, remove the object.

(1) Only take the value, do not remove the mark

<?php
 foreach (array as value) {
 //performed task
 }
 ?>

(2) Remove the mark and value at the same time

 <?php
 foreach (array as subscript => value) {
 //performed task
 }
 ?>

The above PHP reading notes collation _ Structure statement is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.