PHP Learning Path (v)

Source: Internet
Author: User
Tags php foreach

2017.08.13

Day 5 sunny days

php-Sequential structure

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

PHP Conditional Structure If...else ...

The conditional structure is like a fork that can walk to the left or to the right. For example, in the bathroom, we know our sex, this time we need to according to the bathroom conditions, the left men's room, the right woman's toilet, or just the opposite, where sex is the condition of the conditional structure. For example, now the scores are popular using a, B, C to grade, assuming that the test score is 93, you can set it to level A, the test score is 87, you can set it to level B, where the score interval is the condition of the conditional structure.

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

<?PHPIF (condition) {     //Assign Server Dry task a}else{     //Assign server to dry task b}?>

By conditional judgment, if the return value is Boolean true, then task A is executed, and if the return value is False, then task B is executed.

PHP conditional Structure If...else If ...

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

<?PHPIF (Condition one) {     //Assign Server Dry task A}else if (condition two) {     //Assign server to dry task b}?>

By condition one, if the return value is a Boolean value of True, then execute task A, if the return value is false, then the condition two is evaluated, if the return value is Boolean true, then the task B is executed, otherwise neither task A nor task B is executed. The server will continue to perform other tasks.

PHP conditional Structure If...else If ...

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

<?PHPIF (Condition one) {     //Assign Server Dry task A}else if (condition two) {     //Assign server to dry task b}?>

By condition one, if the return value is a Boolean value of True, then execute task A, if the return value is false, then the condition two is evaluated, if the return value is Boolean true, then the task B is executed, otherwise neither task A nor task B is executed. The server will continue to perform other tasks.

PHP Conditional Structure Switch...case ...

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

<?phpswitch (condition) {Case condition value one:  //Task one break  ; case condition Value two:/  /Task two  Break;default:  //Default task}?>

First judge the condition, if the return value of the condition is the condition value one, then perform task one, if the value returned by the condition is the condition value two, then perform task two, if the return value of the condition is neither the condition value nor the condition value two, the default task is executed. The effect of break is to end the switch (which is followed by a specific example), using the switch statement to avoid lengthy "if." else if: Else "code block.

PHP Conditional Structure Switch...case ... The break in

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

Task

The use of a example with break and B example without break can be clearly seen in its role. A example code and b example code:

In a example, the output $sum value is the 20,b example, and the output $sum value is 40. In the A example, the second case is executed, $sum becomes 20, and then break jumps out of switch to perform other tasks. In the B example, the second case is executed, the $sum becomes 20, and because there is no break, the third instance is executed and the default is executed, $sum becomes 40.

Loop structure in PHP while loop statement

The loop structure is like running a football field in a circle and running a circle. In other words, a task is performed repeatedly under the conditions that are met. Like 400.1-meter laps of the runway, run 800 meters, then run 2 laps, when running the first lap and then run the second lap, the second lap end has reached 800 meters, stop running.

In PHP, the while loop statement is as follows:

<?phpwhile (condition) {      //Perform task}?>

First, determine whether a condition is compliant (the conditional return value is true), perform the task if it is met, perform the task, and then determine whether the condition meets the requirements, and then repeat the task, or end the task.

Do and loop statements for the loop structure in PHP

There is another way of looping statements in PHP: The Do...while Loop statement syntax is as follows:

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

Perform the task first (the while statement in the previous section is 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), if the task is executed again, the completion of the task, continue to determine the condition.

Examples of the advantages of using Do...while statements in the loop structure of PHP

While and Do...while can be selected depending on the situation. Suppose there is a chess game, the first to roll the dice, if not 6, advance the dice of the step number of points, if 6, the number of steps forward dice, and can be thrown again.

Type:

<?php
While example
$sum = 0;
$num = rand (1,6); Get 1 to 6 random numbers, simulate roll dice
$sum = $sum + $num;//forward step
while ($num ==6) {
$num = rand (1,6);//Get random numbers from 1 to 6, simulate roll dice
$sum = $sum + $num;//forward step
};
echo "While example executes, forward:". $sum. " <br/> ";
Do...while Example
$sum = 0;
do{
$num = rand (1,6);//Get random numbers from 1 to 6, simulate roll dice
$sum = $sum + $num;//forward step
}while ($num ==6);
echo "do...while example execution complete, go ahead:". $sum. " <br/> ";
?>

Output:

While example execution completed, forward: 9 (random)
do...while example execution complete, forward: 3 (Random)

A For loop statement for the loop structure in PHP

There is also a looping statement in PHP, with the FOR Loop statement structured as follows:

<?phpfor (initialize; cycle condition; increment item) {      //Perform task}?>

For statement, initialization is evaluated unconditionally before the loop begins, and the loop condition is evaluated before each cycle 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 loop the number of times a code block is specified.

The Foreach Loop statement for the loop structure in PHP (task one)

In PHP, the Foreach Loop statement, commonly used to iterate over an array, is generally used in two ways: Do not remove the label, remove the label.

(1) value only, do not remove the label

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

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

<?phpforeach (array as Subscript = = value) {//task performed}?>

Type:

<?php
$students = Array (
' I ' = ' make Fox rush ',
' + ' and ' 林平 ',
' I ' and ' Song Yang ',
' ~ ' = ' ren ying ',
' The ' and ' to ask the heavens ',
' + ' = ' let me Do ',
' ~ ' and ' dashed ',
' = ' and ' founder ',
' 2018 ' and ' Yue Qun ',
' 2019 ' and ' Ning Zhong ',
)///10 student's number and name, stored in an array

Iterate through an array using the loop structure to get the number and name
foreach ($students as $v)
{
echo $v;//output (print) name
echo "<br/>";
}
?>

where "=" is indicated

The key name is 2013 and the value is either yingying

Iterate through an array using the loop structure to get the number and name
foreach ($students as $key = + $v)//Why????


{
echo $key. ":". $v;//output (print) Number: Name
echo "<br/>";
}
?>

This is the weight of today ~

2017/08/13

21:00

PHP Learning Path (v)

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.