The classic example of a for loop is continuous summation: 1+2+3+...+100. After teaching for more than an hour, some students still can't do it. You have to be thoughtful when making programs. Some students kept typing on the keyboard and couldn't get it right. Before doing this summation, we have to think about it. Summation is actually continuous accumulation. When variable $i increases by itself, it must be summed with the previous number. So how to sum with the previous number? We can do a split: treat the number before $i as one item, and add it to $i separately. In the same way, 100 is added to the sum of the previous 99 items, 99 is added to the sum of the previous 98 items...and so on. , 2 plus the previous number 1, then 1 is 1+0. When writing a program, you need to think in reverse. First calculate 0+1=1, then 1+2=3, then 3+3=6...
Copy the code The code is as follows:
< ;?php
/*
*file name: 1+...+100.php
*author: luchanghong
*email: luchanghong@xingmo.com
*time: 2011/5/24
*/
$sum = 0;
$str = '';
for($i = 0 ; $i <= 100 ; ++$i)
{
echo $str .= $i.'+';
// echo '< ;br>';
// echo $sum.'+'.$i.'=';
echo '=';
echo $sum = $sum+$i;
echo '
';
}
echo $sum;
?>
Copy code The code is as follows:
/*
*file name: 99.php
*author: luchanghong
*email: luchanghong@xingmo.com
*time: 2011/5 /9
*/
echo '
'.$j.'x'.$i.'='.$j*$i.'< /td>'; } echo ' |
The above has introduced the multiplication table for loop continuous summation and the multiplication table code, including the content of the multiplication table. I hope it will be helpful to friends who are interested in PHP tutorials.