Count 1 to 100 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 "<br>";
}
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

Count 1 to 100 php


How does this program work?

  • In this program we are going to learn about how to display 1to 99 numbers in 5 rows sequentially using PHP.
  • In this program we are using two loops, while and for loop to display numbers.
  • While loop is used to print 100 numbers and to print 5 numbers on each line by using for loop.

Here is the code

//To display 1 to 99 numbers in 5 rows sequentially
<html>
<head>
<title>PHP Program To display 1 to 99 numbers in 5 rows sequentially</title>
</head>
<body>
<?php
$number = 1;
echo ("Numbers from 1 to 100: ");
//While loop, that will print numbers
//From 1 to 100
while($number <= 100)
{
//For loop to print 5 numbers in a line
for ( $i = 1;$i <= 5;$i++)
{
//To printing the numbers
echo " ".$number;
//Increasing loop counter by 1
$number++;
}
echo " ";
}
return 0;
?>
</body>
</html>

Count 1 to 100 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:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php 
        for ($i = 1; $i <= 10; $i++){
            $sul = $i + $i + $i + $i + $i + $i + $i + $i + $i + $i;
            echo "$i + $i + $i + $i + $i + $i + $i + $i + $i + $i = $sul<br>";

        };

    ?>
</body>

Hope you can help me thanks :)

asked Nov 21, 2016 at 9:15

8

This code should help you:

<?php
$sum = 0;
for($i = 1; $i<=10; $i++) {
    $sum = $sum + $i;
}
echo $sum;    
?>

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:

<?php
$sum = 0;
$str = '';
for($i = 1; $i<=10; $i++) {
    $sum = $sum + $i;
    $str .= $i == 10 ? $i." = " : $i." + ";
}
echo $str.$sum;
?>

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

Count 1 to 100 php

Murad HasanMurad Hasan

9,5052 gold badges20 silver badges40 bronze badges

0

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

<?php
$answer = 0;    
for ($i = 1; $i <= 10; $i++){
         $answer = $i + $answer;
         if ($i == 10) {
           echo $i." = ".$answer;
         }
         else {
           echo $i." + ";
         }       
    };
?>

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

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
        $n = 10;
        $items = range(1, $n);
        $sum = array_sum($items);
        echo implode('+', $items) . ' = ' . $sum;
    ?>
</body>

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

Count 1 to 100 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 to loop number php?

The for loop - Loops through a block of code a specified number of times..
init counter: Initialize the loop counter value..
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends..
increment counter: Increases the loop counter value..

How can I print only odd numbers in php?

Odd numbers are those which are not divisible by 2. Numbers Like 1, 3, 5, 7, 9, 11, etc are odd..
<html>.
<body>.
<form method="post">.
Enter a number:.
<input type="number" name="number">.
<input type="submit" value="Submit">.
</form>.
</body>.

What is loop explain for loop in php?

Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops. Loops are used to execute the same block of code again and again, as long as a certain condition is true.