PHP loop basics tutorial _ PHP Tutorial

Source: Internet
Author: User
PHP loop basics tutorial. In php, PHP loops include commonly used PHP loop statements such as while, dowhile, for, foreach, and each. below I will summarize some of my notes on learning loops. PHP loops mainly involve php loop statements, such as while, do while, for, foreach, and each, next I will summarize some of my notes during the learning cycle.

The loop in PHP mainly refers to the number of times specified by the user to run the same code block.

There are four main PHP loops: while, do... While, for, foreach. Next we will explain the usage of each loop separately.

While statement:

The code block is executed cyclically as long as the specified condition is true.

Format:

While (expr)
{
Statement;
}

Semantics: First, determine the expr. if the expression expr is false, it ends. if the expression expr is true, execute the statement. after the statement is executed, judge the expr again. if the expression expr is still true, execute the statement until the expression expr is false and ends.
Example:

The code is as follows:

$ I = 1;
While ($ I <= 5 ){
Echo $ I;
$ I ++;
}
?>

The above example demonstrates a loop. as long as the variable I is less than or equal to 5, the code will continue to run cyclically. The variable increments by 1 every cycle, and then input the I value;

Do... While statement:

Execute a code block first, and then repeat the loop when the specified condition is set.
Format:

Do {
Statement;
}

While (expr) syntax: execute a statement first, and then determine the expr. if the expression expr is false, the statement ends. if the expression expr is true, the statement continues to be executed cyclically, after the execution is complete, judge the expr again. if the expression expr is still true, continue to execute the statement until the expression expr is false and ends.

Note: the difference between him and while is that do... If you do not perform any judgment for the first time, execute a statement and then determine whether the condition is true. Note that the other conditions are the same as those for the while clause.
Example:

The code is as follows:

$ I = 6;
Do
{
$ I ++;
Echo "The number is". $ I ."
";
}
While ($ I <5 );
?>

The above example demonstrates a loop. I assign a value of 6 to variable I. here it is obvious that the condition in the while clause is not true, but the result input is 6, that is, do... For the while statement, he does not make any judgment for the first time. he executes the while statement first and then checks whether the condition is true.

For statement

: If you have determined the number of times the code block is repeated, you can use the for statement.
Syntax

For (initialization; condition; increment)
{
Code to be executed;
}

Syntax: the for statement has three parameters. The first parameter initializes the variable, the second parameter saves the condition, and the third parameter contains the increment required for the execution cycle. If the initialization or increment parameter contains multiple variables, separate them with commas. The condition must be calculated as true or false.
Example:

The following example shows the text "Hello World !" Shown 5 times:

The code is as follows:

For ($ I = 1; $ I <= 5; $ I ++)
{
Echo "Hello World!
";
}
?>

Foreach statement: The foreach statement is used to traverse arrays cyclically.

For each loop, the value of the current array element is assigned to the value variable (the array pointer is moved one by one), and so on.
Syntax

Foreach (array as value)
{
Code to be executed;
}

Example
The following example demonstrates a loop that outputs the value of a given array:

The code is as follows:

$ Arr = array ("one", "two", "three ");
Foreach ($ arr as $ value)
{
Echo "Value:". $ value ."
";
}
?>

Meaning: first, I define an array arr, and then I use foreach to loop. ($ arr as $ value) means that the value in the $ arr array is assigned to $ value, then execute the statement to output the value of $ value.
The output result is:

One
Two
Three

While, for, foreach, each, and other commonly used PHP loop statements, I will summarize some of my notes during the learning loop. Main users of loops in PHP...

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.