For loops in PHP
<? PHP $arr Array ("A", "B", "C"); for $i $i Count ($arr$i+ +) {echo$arr[$i];}; // count equals the length?> in JS
optimization
<? PHP $arr Array ("A", "B", "C", ' d '); $num Count ($arr); for $i $i $num $i+ +) {echo$arr[$i]. ' <br> ';}; // count equals the length?> in JS
In PHP, the For loop is not commonly used, and the most commonly used is the Foreach Loop
foreach Loop
<? PHP $arr Array ("A", "B", "C", ' d ', ' e '); foreach ($arras$value) { echo "{$value}<br>" ; };? >
The power of foreach is that it can output subscript
<? PHP $arr Array ("A", "B", "C", ' d ', ' one ' = ' e '); foreach ($arras$key$value) { echo ' {$key}--- {$value}<br> "; };? >
See an example of an array turning into a table
<?PHP$group= [ [' Name ' = ' Zs ', ' age ' = ', ' sex ' = ' male ', ' email ' = ' [email protected] ', [' Name ' = ' as ', ' age ' = ', ' sex ' = ' female ', ' email ' = ' [email protected] ', [' Name ' = ' Zs ', ' age ' = ', ' sex ' = ' male ', ' email ' = ' [email protected] ', [' Name ' = ' as ', ' age ' = ', ' sex ' = ' female ', ' email ' = ' [email protected] ', [' Name ' = ' Zs ', ' age ' = ', ' sex ' = ' male ', ' email ' = ' [email protected] ', [' Name ' = ' as ', ' age ' = ', ' sex ' = ' female ', ' email ' = ' [email protected] ', [' Name ' = ' Zs ', ' age ' = ', ' sex ' = ' male ', ' email ' = ' [email protected] ', [' Name ' = ' as ', ' age ' = ', ' sex ' = ' female ', ' email ' = ' [email protected] '] ]; Echo' <table border= ' 1 "width=" align= "Center" > "; Echo' <caption>; //foreach Loop foreach($group as $row){ Echo' <tr> '; foreach($row as $col){ Echo' <td> '.$col.‘ </td> '; }; Echo' </tr> '; };?>
php for loops and foreach loops