Php While Loop 3 : 4 Grid - php

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;
}
}

Related

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.

Add all the previous numbers in a for loop to get the current number

I have this for loop:
for($i = 1; $i <= 7; $i++) {
echo $i . "<br>";
}
Which outputs:
1
2
3
4
5
6
7
Now what I want is to add all the previous numbers on each loop. So the output should be:
1
2 // add all above to get this number
3 // add all above to get this number
6 // add all above to get this number
12 // add all above to get this number
24 // add all above to get this number
48 // add all above to get this number
96 // add all above to get this number
...etc
The first and second number doesn't necessarily have to be in the loop, that can be defined manually outside.
What I don't want is to add the value of $i on each loop, but to add all the previous numbers on each loop.
I have tried summing up using this code:
$sum = 0;
for($i = 1; $i <= 5; $i++) {
$sum = $sum + $i;
echo $sum . "<br>";
}
But I get this output:
1
3
6
10
15
21
28
How can I achieve my desired output?
Try this
<?php
$results = [];
for ($i = 0; $i <= 7; $i++){
$currentResult = 0;
if ($i < 2){
$currentResult = $i+1;
}
else{
foreach($results as $currenNumber){
$currentResult += $currenNumber;
}
}
echo $currentResult . '<br>';
$results[] = $currentResult;
}
?>
<?php
$value = 0;
for($i = 1; $i <= 8; $i++) {
if($value < 3){
$value = $value + 1;
} else{
$value = $value * 2;
}
echo $value . '<br>';
}
?>

Print numbers 1 to 10 group by 4

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";
}

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);

Made a series of prime numbers using php

I made this code to create a series of prime numbers using php
$number = 53;
for ($i=0; $i<=$number; $i++)
{
if ( $i == 2 )
{
echo "$i ";
}
else if ( $i == 3 )
{
echo "$i ";
}
else if ($i % 2 != 0 && $i % 3 != 0)
{
echo "$i ";
}
}
and the result: 1 2 3 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53
why number 25, 35 and 49 still appear.?
or maybe made like this code ?
$number = 53;
for( $i = 2; $i <= $number; $i++ )
{
for( $k = 2; $k < $i; $k++ )
{
if( $i % $k == 0 )
{
break;
}
}
if( $k == $i )
echo $i." ";
}
but I want to include number 1 in the result
25 % 2 = 1
25 % 3 = 1
As such, it displays the number.
Your function does not display prime numbers; rather, it displays number which are not divisible by 2 or 3.
You can use this bit of code.
<?php
$number = 983;
$i = 0;
while($i <= $number)
{
$prime = true;
if($i != 0 && $i != 1 && $i != 2)
{
echo 'Number: ' . $i;
// echo $i .'</br>';
$x = range(2,$i-1);
foreach($x as $try => $value)
{
if(($i % $value) === 0)
{
$prime = false;
}
}
if($prime == true)
{
echo ' is a prime';
}
}
$i++;
echo '</br>';
}
?>
Sorry, I know this is coming a bit late, but here's a function that can help you do this exactly...
<?php
//Prime Function
function fn_prime($number) {
$i = 2; $result = TRUE;
while($i < $number) {
if(!($number%$i)) {
$result = FALSE;
}
$i++;
}
return $result;
}
//Declare integer variable...
$k = 0;
//Start Loop up to any number of your choice for e.g. 200
while($k < 200) {
if(fn_prime($k)) {
echo "$k is a prime number<br/>";
} else {
echo "$k is not a prime number!<br/>";
}
$k++;
}
?>

Categories