Do You Know How to use For Loops in PHP - Coding Cyber

Do You Know How to use For Loops in PHP

Do You Know How to use For Loops in PHP

Other Articles in PHP Loops Section

Quickly Navigate to Other Sections in PHP Tutorials

PHP Basics
PHP Data Types
PHP Advanced Data Types
PHP Logical Statements
PHP Operators
PHP Loops
User Defined Functions in PHP
Useful PHP Functions

for() Loop in PHP, for loop executes the code inside the block for a specified number of times.

Syntax of for() Loop in PHP

for (init counter; test counter; increment counter) {
code to be executed;
}

Example code of for() Loop in PHP
Here in this example, we are initiating variable i as 1, condition to check is if variable i is less than or equal to 10. And increment i till the condition fails.

If i = 1, i is less than 10, Value to be displayed is 1. then var i value increments by one.
If i = 2, i is less than 10, Value to be displayed is 2. then var i value increments by one.
If i = 3, i is less than 10, Value to be displayed is 3. then var i value increments by one.
If i = 4, i is less than 10, Value to be displayed is 4. then var i value increments by one.
If i = 5, i is less than 10, Value to be displayed is 5. then var i value increments by one.
If i = 6, i is less than 10, Value to be displayed is 6. then var i value increments by one.
If i = 7, i is less than 10, Value to be displayed is 7. then var i value increments by one.
If i = 8, i is less than 10, Value to be displayed is 8. then var i value increments by one.
If i = 9, i is less than 10, Value to be displayed is 9. then var i value increments by one.
If i = 10, i is equal to 10, Value to be displayed is 10. then var i value increments by one.
If i = 11, i is not equal to 10 and not less than 10, here for loop breaks out. And continue to execute next block of code after for loop.

for ($i = 1; $i <= 10; $i++) { echo $i; }

Related Posts
No related posts for this content
Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: