PHP Note III: Use of loops (loop output table)

Source: Internet
Author: User

There are three types of cycles:
1. For loop
Format:

    For (initialization of the loop variable; loop condition; increment of the loop variable) {loop body}

2. While loop
Format:

   while (condition) {       loop body   }

3. Do While loop
Format:

   Do {      loop body   } while (condition);
Note: The semicolon after the while statement

Attention:
1), for loop for a regular cycle, with more
2), while and does while are different:
The while loop may not execute at one time, while the Do While loop executes at least once

Example:

<?PHP/** loop out a table, using for loop, while loop, do While loop, respectively*/  //For loop//Output table header    Echo"<table width= ' 80% ' align= ' center ' border=1 cellpadding=5 clellspacing=0> '; //output of the outer loop control row     for($i= 1;$i<=5;$i++) {         //Interlaced color Change        if($i% 2 ==0){            Echo("<tr align= ' center ' bgcolor= ' green ' >"); }Else{            Echo("<tr align= ' center ' >"); }                //the output of the Inner loop control column         for($j= 1;$j<=8;$j++) {             Echo"<td>".$i*$j." </td> "; }        Echo"</tr>"; }    Echo"</table>"; //While Loop    Echo"<table width= ' 80% ' align= ' center ' border=1 cellpadding=5 clellspacing=0> '; $m= 1;  while($m<= 5) {        //Interlaced color Change        if($m% 2 ==0){            Echo("<tr align= ' center ' bgcolor= ' green ' >"); }Else{            Echo("<tr align= ' center ' >"); }                //the output of the Inner loop control column        $n= 1;  while($n<= 8) {             Echo"<td>".$m*$n." </td> "; $n++; }        Echo"</tr>"; $m++; }    //Do and Loop    Echo"<table width= ' 80% ' align= ' center ' border=1 cellpadding=5 clellspacing=0> '; $k= 1;  Do{        //Interlaced color Change        if($k% 2 ==0){            Echo("<tr align= ' center ' bgcolor= ' green ' >"); }Else{            Echo("<tr align= ' center ' >"); }                //the output of the Inner loop control column        $p= 1;  Do {             Echo"<td>".$k*$p." </td> "; $p++; } while($p<= 8); Echo"</tr>"; $k++; } while($k<= 5);?>

PHP Note III: Use of loops (loop output table)

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.