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.
Related
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;
}
}
How do i display php numbers like so?
1
21
321
4321
54321
654321
right now my code is like this and displays the following.
for ($i = 0; $i <= 5; $i++) {
for ($j = 5; $j > $i; $j--)
{ echo " ";
}
for ($l = 0; $l < $j; $l++){
}
for ($k = 1; $k <= $l; $k++) {
echo "$k";
}
echo "</br>";
}
Basically this shows the correct way of the pattern but does not the correct numbers in order.
1
12
123
1234
12345
The code seems to have too many loops, and can use str_repeat to simplify the logic and loop structure. The counter was dependent on another loop, and thus couldn't be easily changed. I have refactored it into the following:
$printOut = "";
$count = 5;
for ($i = 1; $i <= $count; $i++) {
$printOut = $i . $printOut;
echo str_repeat(" ", $count - $i) . $printOut;
echo "</br>";
}
Output:
1
21
321
4321
54321
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>';
}
?>
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";
}
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);