Print 1 to 100 using for loop in php
Show 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.
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 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? 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. 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. Now script execution will stop after 2 seconds More on PHP Maximum Execution time Let us try with some simple PHP examples. 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:
More about Update If you want your structure it can be as follows:
And it will output
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.
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
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:
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:
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
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.
|