教科书写法:
<?php for($i = 1; $i <= 9; $i++){ for($j = 1; $j <= $i; $j++){ echo $i .'*'. $j .'='. $i*$j .' '; } echo "<br/>"; }
while:
$i=1; $j=1; while($i<10){ echo "{$j}*{$i}=".$i*$j.' '; if($i==$j){ echo "<br/>"; $j=1; $i++; }else{ $j++; } }
for里边写上俩游标:
<?php function test(){ for($i=1, $j=1; $j<=9; $i++){ echo $i . "x" . $j . "=" . $i * $j . " "; if($i == $j){ $i = 0; $j++; echo '<br />'; } } } test(); ?>文青程序员:
<?php $tpl="AX%d=A "; $p='/((A)X(\d+)=(A))/e'; for ($i=1;$i<=9;$i++) { $j=1; $s=str_repeat(sprintf($tpl,$i),$i); preg_replace($p,'printf("%dX%s=%d\t",$j,"\\3",\\3*($j++));',$s); echo "\n"; }
苦力:
$arr = array( 9,8,7,6,5,4,3,2,1, 8,7,6,7,4,3,2,1, 7,6,5,4,3,2,1, 6,5,4,3,2,1, 5,4,3,2,1, 4,3,2,1, 3,2,1, 2,1, 1, ); $m=0; foreach($arr as $v) { if($m < $v) { echo " <br /> \n"; $m = $v; } echo "$m * $v = " . ($m * $v) . " "; if($v==1) { $m = 0; } }
2B:
$table = "1 * 1 = 1 \n1 * 2 = 2 2 * 2 = 4 \n1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 \n1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16 \n1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25 \n1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 \n1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49 \n1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64 \n1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81 \n"; do { printf("%s", $table); } while (0);
老夫蛋疼了写的:
<?php for ($i = 0; $i < 81; $i++) { $a = $i % 9 + 1; $b = floor($i / 9 + 1); if ($a <= $b){ echo $a . '*' . $b . '=' . $a * $b . ' '; } if ($a == 9){ echo '<br>'; } }