PHP for Loops With Stone River eLearning

In order to understand PHP For Loops, we must quickly recollect our knowledge of programming languages and how the flow control statements are executed in the basic programming languages. PHP For Loops follow the same structure for the execution of For Loops as is in the case of the other programming languages like C or C++.  

‘For loops’ are particularly useful when a programmer intends to execute an operation or a command for some specific number of times. The syntax for PHP for loops primarily consists of three expressions in the declarations phase. 

The first expression initialises the counter to a specific value. In easier terms, this expression is the assignment of a value to a variable which is to be used in the next expressions. 

The second expression is the condition expression which evaluates a certain specified condition on the first expression and performs the operation if it holds true.

The third expression deals with assignment and updating of loop counter at the end of each cycle. This expression can be incrementing or decrementing the counter values, as explained in the video tutorial.


	for ($num=1;$num<=10;$num++){
		echo 'This<br>';

In the above example of PHP For Loops, also shown in the video tutorial, a for loop is initiated inside the PHP mainframe and the operation to be performed is specified. It is important to note that the operation to be performed by any For Loop must be included inside the brackets just after specifying the ‘for’ keyword.

	echo 'This<br>'; /*The operation of printing ‘This’ is included inside the brackets*/

Note: In case the brackets are not used and there are more than one line in the operation phase; then the compiler will only execute the first line of command and will neglect the other lines as part of the For Loop.

Inside the ‘for loop’, three expressions are specified. The first expression assigns the value 1 to the variable $num. It is to be noted that in PHP, variables are declared by using the ‘$’ symbol before the name of the variable. The second expression, which is ‘$num<=10’; specifies the condition for the execution of this PHP For Loop. According to it, the operation will get executed until the value of the $num variable becomes more than 10. The third and final expression shows the value of $num variable incremented by 1. Using ‘$num++’ is the same as writing $num=$num +1.

This For Loop performs the action of printing the word ‘This’ 10 times after the execution of the code. It is important to note that the third expression in the for loop which is performing the duty of incrementing the $num variable in the example specified above, will be functional only after the PHP For Loop operation has been executed once. For instance, in the above example, after the $num variable is specified to the value 1 and the condition ‘$num<=10’ is checked and satisfied, the operation of printing the word ‘This’ will be executed and then the value of $num variable will be incremented by 1. The same process will continue until the value of $num variable reaches the value 11 and the condition is not satisfied for the operation to be executed. 

Instead of incrementing the $num variable, a programmer can also use the decrement function. However; it goes without saying that if the decrement function is used, the first two expressions of the ‘for loop’ have to be adjusted accordingly. For instance, in the above example, the same result can be obtained by adjusting the expressions in this way:

	for ($num=10;$num>;=1;$num--){
		echo 'This<br>';

PHP For Loops are particularly useful in making the code smaller, more organised and easier to understand. Further, For Loops, just like other flow control statements are useful in obtaining desired results for real time applications of any programming code; as depicted in the video tutorial by obtaining the factors of the number 2 and 10. 

Online Technology Courses Stone River eLearning


(Visited 23 times, 1 visits today)