PHP Basics Tutorial _php Tutorial

Source: Internet
Author: User
Tags php basics
In PHP php loop includes while,do While,for,foreach,each and so on commonly used PHP loop statements, let me summarize my study in the cycle of some notes.

In PHP, the primary user executes the same block of code to run the specified number of times.

There are four main types of PHP loops: While,do...while,for,foreach. Let's explain the usage of each cycle separately.

While statement:

Executes the code block as long as the specified condition is true.

Format:

while (expr)
{
Statement
}

Semantics: First Judge expr, if the expression expr is false and end, if the expression expr is true, executes the statement statement, executes the sentence, and then evaluates expr again, if the expression expr is still true, continue to execute the statement statement until the expression expr is false and ends.
Cases:

The code is as follows Copy Code

$i = 1;
while ($i <=5) {
echo $i;
$i + +;
}
?>

The example above demonstrates a loop that, as long as the variable i is less than or equal to 5, the code will continue to loop. Once each loop is cycled, the variable increments by 1 and the value of I is entered;

Do...while statement:

Executes the code block first, and then repeats the loop when the specified condition is set.
Format:

do{
Statement
}

while (expr) semantics: First executes the statement statement once, then evaluates expr, if the expression expr is false and ends, and if the expression expr is true, proceeds to loop through the statement statement, executes again to judge expr, If the expression expr is still true, the statement statement continues execution until the expression expr is false and ends.

Note: The difference between him and while is that Do...while executes a statement for the first time without making any judgments, and then makes the judgment conditional, and it is important to note that the other is the same as while.
Example:

The code is as follows Copy Code

$i = 6;
Do
{
$i + +;
echo "The number is". $i. "
";
}
while ($i <5);
?>

The above example shows a loop, I assign a value of 6 to the variable I, here is clearly and while the conditions in the comparison is not true, but the result of input 6, that is, the Do...while statement before the first time, he did not make any judgment, execute once, and then determine whether the condition is established.

For statement

: You can use a For statement if you have determined the number of repeated executions of the code block.
Grammar

for (initialization; condition; increment)
{
code to be executed;
}

Semantics: The For statement has three parameters. The first parameter initializes the variable, the second parameter holds the condition, and the third parameter contains the increment required to execute the loop. If you include multiple variables in the initialization or increment parameter, you need to separate them with commas. The condition must be evaluated as TRUE or FALSE.
Example:

The following example displays the text "Hello world!" 5 times:

The code is as follows Copy Code

for ($i =1; $i <=5; $i + +)
{
echo "Hello world!
";
}
?>

foreach statement: A foreach statement is used to iterate through an array.

For Each loop, the value of the current array element is assigned to the value variable (the array pointer moves one at a time) – and so on.
Grammar

foreach (array as value)
{
code to be executed;
}

Example
The following example shows a loop that can output the value of a given array:

The code is as follows Copy Code

$arr =array ("One", "one", "one", "three");
foreach ($arr as $value)
{
echo "Value:". $value. "
";
}
?>

Explanation: First I define an array of arr, and then I use foreach to loop, where ($arr as $value) means to assign the values in the $arr array to $value, and then execute the statement to be the value of the output $value.
The result of the output is:

One
Both
Three

http://www.bkjia.com/PHPjc/628654.html www.bkjia.com true http://www.bkjia.com/PHPjc/628654.html techarticle in PHP php loop includes while,do While,for,foreach,each and so on commonly used PHP loop statements, let me summarize my study in the cycle of some notes. Loop main user 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.