简体   繁体   English

编写PHP代码以打印以下数字模式

[英]Write a PHP code to print following number pattern

Write a PHP code to print following number pattern:
147
258
369

I am trying like this but its shows me shows below. 我正在尝试这样,但下面显示了它。 how to convert column to row pattern. 如何将列转换为行模式。

<?php
    $num = "";
    for($i=1;$i<=9;$i++) {

        $num .= $i; 
        if($i%3==0){
            $num .="<br />";
        } 

    }
    echo $num;
?>
please help me

You need to use for loop inside for loop to achieve this. 您需要在for循环内部使用for循环来实现此目的。 Below is the code 下面是代码

$num = "";
for( $i = 1; $i <= 3; $i++ ) {
    for( $j = 0; $j <= 2; $j++) {
        $k = $i + ( $j * 3 );
        $num .= $k; 
    }
    $num .= "<br />"; 
}
echo $num;

Here is another way to get this output. 这是获取此输出的另一种方法。

for($i = 1; $i <= 3; $i++) {
   $print = $i;
   for($j = 1; $j <= 3; $j++) {                
       echo $print;
       $print = $print + 3;
   }
   echo "<br />";
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM