Printing number from 1 to 50 by for loop

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

Questions



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
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







sana

08-09-2015

How to print for loop from 1 to 100
smo1234

12-09-2015

Change
for( $i=1; $i<=100; $i++ )

15-06-2020

how to print 5 number line

27-12-2020

How to print a for loop from 100-1

20-12-2022

How to print for loop from 1 to 100 in shortcode in dynamic way i.e. putting the number of anyone desire




PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer