Print 1 to 100 using for loop in php

For and foreach loop in PHP with break statement and creating patterns using nested for loops

We can print numbers from 1 to 10 by using for loop. You can easily extend this program to print any numbers from starting from any value and ending on any value.

for( $i=1; $i<=10; $i++ )
{
echo $i;
echo "
"; }
The echo command will print the value of $i to the screen. In the next line we have used the same echo command to print one html line break. This way all numbers will be printed in one new line.

More Details on FOR loop
Here we have printed from 1 to 10 numbers, by changing the values inside the for loop you can print numbers starting from any value to ending at any value.

For loop is used when we know exactly how many times the looping is required. Here no condition is set for looping. While loops are used for conditional looping.

What happens when we keep a condition which the loop can never meets?

for( $i=1; $i<=10; $i++ )
{
echo $i;
$i = 5;
}
Inside the loop each time we are resetting the value of $i to 5 , so it can never reach value of 10 to exit the loop.
Output is here.
....
Fatal error: Maximum execution time of 30 seconds exceeded in H:\php_files\plus2net\z\for.php on line 18
Server stops the execution of the script after 30 seconds. This value of 30 seconds is set at php.ini file, we can change this upper limit of execution time by using ini_set() function.

Just add this line to the above code.

error_reporting(E_ALL); // Display all types of error 
set_time_limit ( 2 );      // Max execution time is set to 2 seconds 
for( $i=1; $i<=10; $i++ )
{
echo $i;
$i = 5;
}
Now script execution will stop after 2 seconds
More on PHP Maximum Execution time

Let us try with some simple PHP examples.
Introduction to PHP Sample codes in PHP
Check the string or number is Palindrome or not in PHP

Print 1 to 100 using for loop in php


I need to make a loop in php that does 1 + 2 + 3 + 4 .... + 10 = 55 but icant get it to work. I did this:




    


    ";

        };

    ?>

Hope you can help me thanks :)

asked Nov 21, 2016 at 9:15

8

This code should help you:


You are incorrect using loop.

Explanation

I think it will be easier to understand with following table:

_____________________
|JUMP | $i   | $sum  |
|1    | 1    | 1     |
|2    | 2    | 3     |
|3    | 3    | 6     |
|4    | 4    | 10    |
|5    | 5    | 15    |
|6    | 6    | 21    |
|7    | 7    | 28    |
|8    | 8    | 36    |
|9    | 9    | 45    |
|10   | 10   | 55    |

More about for you can read in PHP: for

Update

If you want your structure it can be as follows:


And it will output 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

answered Nov 21, 2016 at 9:18

0

Just make a for loop from starting 1 to 10, like given below. You need to initialize the counter as 0 and while the loop executes you need to collect / sum them to the counter and finally outside the loop print/ echo the counter.

$count = 0;
$string = '';
for ($i = 1; $i <= 10; $i++){
    $count += $i;

    $string .= ($i == 10) ? $i : $i." + ";
}
echo $string." = ".$count; // 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

This is a very simple and straight forward way, you can do this using some array functions which is built-in in PHP.

answered Nov 21, 2016 at 9:19

Print 1 to 100 using for loop in php

Murad HasanMurad Hasan

9,4852 gold badges20 silver badges40 bronze badges

0

If you don't want to hardcode it, you can do this


Output is:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

answered Nov 21, 2016 at 9:23

RonaldRonald

1,2873 gold badges17 silver badges29 bronze badges

4




    


    

This way is shorter. You generate the array from 1 to $n(10);

Then you calculate the sum of the items.

And then join each element with '+' and add the sum.

answered Nov 21, 2016 at 9:18

There you go:

$sum = 0;
for($i = 1; $i <= 10; $i++){
    if($i == 10){
        echo $i;
    } else {
        echo $i." + ";
    }
    $sum = $sum + $i;
}
echo " = ".$sum;

answered Nov 21, 2016 at 9:22

d3p4n5hud3p4n5hu

4114 silver badges9 bronze badges

A for loop actually repeats a block of code n times, you syntax is right buy your semantic is wrong: initializes a counter in 0 and add i to the counter in each step as the other people say. Also, if is not compulsory to use a for, remember that the sum of first n natural numbers is n(n+1)/2, so no loop is actually needed

answered Nov 21, 2016 at 9:32

Print 1 to 100 using for loop in php

How can I print 1 to 100 numbers in PHP?

Print 1....
Print the numbers 1… ... .
For multiples of 3, print “Mpasho” instead of the number..
For multiples of 5, print “Star” instead of the number..
For multiples of 3 and 5, print “MpashoStar” instead of the number..

How do you print even numbers in PHP while loop?

php $end=50; $even= "Even Numbers Are : "; $odd="
Odd Numbers Are : "; for($i=1;$i<=$end;$i++) { if($i%2==0) { $even.
=$i.","; }else $odd. =$i.","; } echo $even.

What are the 4 PHP loop types?

PHP Loops.
while - loops through a block of code as long as the specified condition is true..
do... ... .
for - loops through a block of code a specified number of times..
foreach - loops through a block of code for each element in an array..

Can we use for loop in PHP?

PHP for loop can be used to traverse set of code for the specified number of times. It should be used if the number of iterations is known otherwise use while loop. This means for loop is used when you already know how many times you want to execute a block of code.