PHP Basic Loop Control Statement Learning notes

Source: Internet
Author: User
Loop control Statements

Conditional control statements, which can execute different statements depending on the condition selection. But sometimes you need to reuse a piece of code or function.

While Loop statement

The while loop is the simplest looping statement in PHP, which has the following syntax:

while (expr) {     statement;}

When the value of the expression expr is true, the statement statement is executed, and after execution finishes, the return to the expr expression continues to be judged. Do not jump out of the loop until the value of the expression is false, execute the following statement.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Operation Result:
An even number within 10 is: 2 4 6 8 10

Do...while Loop Statements

The while statement also has another form of representation, namely Do...while. The difference between the two is that the do...while is more cyclical than the while statement. When the value of the while expression is false, the while loop jumps directly out of the current loop, while the Do...while statement executes the block first and then evaluates the conditional expression.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Operation Result:
'll see

For Loop statement

The For loop is the most complex looping structure in PHP, and its syntax is:

for (EXPR1;EXPR2;EXPR3) {   statement;}

Wherein, EXPR1 the first time in the loop, the value is unconditional, expr2 in each cycle before the start of the evaluation, if the value is true, this execution statement, otherwise jump out of the loop, continue to execute; EXPR3 is executed after each loop.

<! DOCTYPE html>
foreach (array_expression as $value)     statement;

Or

foreach (array_expression as $key = $value)     statenment;
<! DOCTYPE html >

Operation Result:
Jack Ben, Bill.

Another writing format for process Control

In a complex PHP page that may contain multiple conditional control statements, loop control statements, and functions, it is cumbersome to find only the matching curly braces "{}". To do this, PHP provides a different writing format, including if, while, for, foreach, and switch can be used. The basic form of the writing format is: Use the colon ":" Instead of the curly brace "{" On the left, use endif, Endwhile, ENDfor, Endforeach, and endswitch; to replace the curly brace "}" on the right.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Operation Result:
The prime number within 1000 is: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 31 7 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 70 9 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

Use the Break/continue statement to jump out of a loop

When using a looping statement, it is sometimes not certain how many times the loop is encountered, which can be used in an infinite loop, such as:

while (true) {     ...}

Or

for (;;) {     ...}

The keyword that jumps out of the loop is break and continue only if the block meets certain conditions before it jumps out of the loop.

The break keyword can terminate the current loop, including all control statements, such as while, Do...while, for, foreach, and switch.
The break statement can not only jump out of the current loop, but also specify a few layers of loops, formatted as:

Break $num;

The parameter $num specifies a few layers of looping to jump out.

Continue the function of the keyword does not break strong, continue can only terminate this cycle and enter into the next cycle, continue can also specify a few loops jump.

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.