Print numbers 1 to 10 group by 4 - php

How can i print output like this?
1 2 3 4
5 6 7 8
9 10
i had tried this code but not luck.
for ($i = 1; $i<=10; $i++)
{
if ($i <= 4)
{
echo $i;
if ($i >= 4){
echo'<br>';
for($x = $i; $x<=10; $x++){
echo $x;
}
}
}
}
this code output look like this.
1234
45678910

Try this
You need to check the value of $i is module by 4 or not, if then echo a break.
for ($i = 0; $i< 10; $i++){
if ($i % 4 == 0)
echo'<br>';
echo ($i + 1)." ";
}
Or
for ($i = 1; $i<= 10; $i++){
echo $i." ";
if ($i % 4 == 0)
echo'<br>';
}
Output
1 2 3 4
5 6 7 8
9 10

The idea is if the no. of elements in a row is a multiple of 4, then there's a line break.i.e for 4, 8, 12, 16...there will be line breaks.
Try this:
for ($i = 1; $i <= 10; $i++) {
echo $i;
if ($i % 4 == 0) {
echo "<br>";
}
}

Here is your code
for ($i = 1; $i<=10; $i++)
{
echo $i.' ';
if ($i % 4==0){
echo'<br>';
}
}
Output
1 2 3 4
5 6 7 8
9 10

Try this
for ($i = 1; $i<=10; $i++)
{
echo $i.' ';
if ($i % 4 == 0)
{
echo'<br>';
}
}
Output
1 2 3 4
5 6 7 8
9 10

Try this, you need to just check if the number is completely divisible by 4 or not
<?php
for ($i = 1; $i<=10; $i++)
{
echo $i.' ';
if ($i % 4 == 0) {
echo'<br>';
}
}
?>
also you can find a working example here http://phpfiddle.org/main/code/ae6y-3749

<?php
$numbers = range(1, 10);
$chunks = array_chunk($numbers, 4);
foreach($chunks as $chunk){
foreach ($chunk as $number){
echo "$number ";
}
echo "<br>\n";
}

Related

Php While Loop 3 : 4 Grid

Having some problems with print output like alternative rows 3 : 4 column.
<?php
$i = 1;
while($i=1 < 14){
if($i <= 3){
echo $i."<br>";
} else {
echo $i."<br>";
}
$i++;
}
?>
** I Want Output Like **
1 2 3
4 5 6 7
8 9 10
11 12 13 14
Use the modulo (remainder of a division). Apply a modulo 7 on $i:
for ($i=1; $i<15; $i++) {
echo $i, in_array($i % 7, [0, 3]) ? '<br>' : ' ';
}
If you just want to output:
1 2 3
4 5 6 7
8 9 10
11 12 13 14
you can just try this,
$firstRow = 0;
$secondRow = 0;
for ($i = 1; $i <= 14; $i++){
$firstRow++;
$secondRow++;
echo $i . ' ';
if($firstRow == 3){
echo '<br>';
$secondRow = 0;
}
if($secondRow == 4){
echo '<br>';
$firstRow = 0;
$secondRow = 0;
}
}

PHP Loop 1 to 800 separating every 5 lines

I need to achieve this:
1
2
3
4
5
---
6
7
8
9
10
---
11
12
13
14
15
---
16
17
18
19
20
---
...
800
my code:
<?php
$sum = 0;
$str = '';
for($i = 1; $i<=800; $i++) {
$sum = $sum + $i;
$str .= $i == 5 ? $i. "<br> --- <br>": $i."<br>";
}
echo $str;
the problem is that with this code it only managed to divide after the first block.
I hope you can help me, thank you very much in advance.
Try this
$sum = 0;
$str = '';
for($i = 1; $i<=800; $i++) {
$sum = $sum + $i;
$str .= $i%5 == 0 ? $i. "<br> --- <br>": $i."<br>";
}
echo $str;
just change $i == 5 into $i%5 == 0
This can be done in a much easier way:
<?php
for ($i = 1; $i<=800; $i++) {
echo $i . "\n";
if ($i % 5 == 0) {
echo "---\n";
}
}
This is meant for CLI output, but HTML basically works the same.

Wap a program to print 3 rows and 3 columns from 1 to 12 in php

I want to print number from 1 to 12 in matrix form and expected output is:
1 5 9
2 6 10
3 7 11
4 8 12
code:
<?php
for ($i=1; $i<=12; $i++)
{
for($j=1;$j<=$i;$j++)
{
echo $i." ";
}
echo "<br/>";
}
?>
I have got wrong output. So, How can I get expected output as I mention above? Please help me.
Thank You
Some "magic" code):
foreach (range(1,4) as $num) {
echo implode(' ', range($num,12,4)) . '<br />';
}
Version with for:
for ($i = 1; $i <= 4; $i++) {
for ($j = $i; $j <= 12; $j +=4) {
echo $j . ' ';
}
echo '<br />';
}
$z=0;
for ($x = 1; $x <= 4; $x++)
{
echo " $x ";
$z=$x;
for ($y = 1; $y <= 2; $y++)
{
$z=$z+4;
echo " $z ";
}
echo "\n";
}
$maxRow = 4;
$maxColumn = 3;
for ($row = 1; $row <= $maxRow; $row++)
{
for ($column = 1; $column <= $maxColumn; $column++) {
$number = $row + 4*($column-1);
echo $number." ";
}
echo "<br/>";
}
should work
foreach (range(1, 4) as $res) {
echo implode(' ', range($res, 12, 4));
echo "<br>";
}

PHP loop problems,how do i solv this

Hey all i can not solve this,that's why i post it here .i am a php larner and trying solve small php problems.my problems are below.
0
1 0 1
2 1 0 1 2
3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4
how to print the following pattern ?
image link-http://imgur.com/4Y5L8ZZ
This worked. I'm not sure whether it's the best approach.
for ($i = 0; $i < 5; $i++) {
for ($a = $i; $a > 0; $a--) {
echo $a;
}
for ($b = 0; $b <= $i; $b++) {
echo $b;
}
echo "\r\n";
}
<?php
for ($out = 0; $out < 5; $out++) {
for ($row = $out; $row > 0; $row--) {
echo $row . " ";
}
for ($col = 0; $col <= $out; $col++) {
echo $col. " ";
}
echo "<br>";
}
?>
Please try executing following code snippet as a solution according to above mentioned description.
$rows=5;
for($i=0;$i<$rows;$i++)
{
echo "<br>";
for($j=$i;$j>=0;$j--)
{
echo "\t".$j;
}
for($k=1;$k<=$i;$k++)
{
echo "\t".$k;
}
}

continously loop with adding different value

I'm new in PHP. How can i achieve continously loop with adding different value?
it's something like this
<?php
$gap1 = 2;
$gap2 = 3;
$lenght = 10;
for( $i=0; $i<$length; $i++ )
{
//the code
}
?>
and the result will be : 0 2 5 7 10 12 15 17
thank you for your help :)
$gap1=2;
$gap2=3;
$lenght = 10;
$p=0;
for($i=0;$i<$lenght;$i++)
{
if($i==0){$p=0;}
elseif($i%2==0)
{
$p+=$gap2;
}
else{
$p+=$gap1;
}
echo $p.'<br>';
}
Try this code:
$gap1 = 2;
$gap2 = 3;
$length = 10;$i=0;
$x = 0;
while($i<$length)
{
echo $x." ";
if($i%2 == 0)
$x+=$gap1;
else
$x+=$gap2;
$i++;
}
Output:
0 2 5 7 10 12 15 17 20 22
$gap = array(2, 3);
$result = array(-1 => 0);
$length = 10;
for($i = 0; $i < $length; $i++) {
$result[] = $result[$i-1] + $gap[($i) % count($gap)];
}
echo implode(' ', $result);

Categories