PHP for while and do while differences

Source: Internet
Author: User

This is a tasty question oh, for, while, doing while the difference I think you should understand the three usages and the difference after you look carefully.

Do...while statement

A do-while statement should have the following format:

do {
statements;
while (condition);

<?php

$a = 1;

do {
$a + +;
}while ($a <1);

echo $a;

?>

Output is 2

Does while it executes once, regardless of whether the condition is set up, to see the implementation.


While statement

A while statement should have the following format
while (condition) {
statements;
}

An empty while statement should have the following format:
while (condition);

Look at a while implementation

<?php

$a = 1;
while ($a <1) $a + +;
echo $a;

?>

Output is 1

NOTE: The while statement must be conditional to true to perform the contents of the while inside, for and while is the same


For statement

A For statement should have the following format:


for (initialization; condition update) {
statements;
}


An empty for statement (all work in initialization, conditional judgment, UPDATE clause completion) should have the following format:


for (initialization; condition; update);


When using commas in initialization or update clauses of a for statement, avoid using more than three variables, resulting in increased complexity. If necessary, separate statements can be used before the for loop (for initialization clauses) or at the end of the For loop (for UPDATE clauses).

For implementation

<?php

$a = 1;
For ($a =1 $a <1; $a + +) {
$a + +;
}
echo $a;
?>

Output is 1

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.