

We will learn about arrays in detail in upcoming tutorial. Approach 1: Using for Loop, a decrement for loop can be initiated in order to access the elements in the reverse order. Decrementing null values has no effect too, but incrementing them results in 1. Arrays, objects, booleans and resources are not affected. Note: The increment/decrement operators only affect numbers and strings. This loop only works with arrays and you do not have to initialise any loop counter or set any condition for exiting from the loop, everything is done implicitly(internally) by the loop. In the above example, we are printing the numbers from 1 to 10.For that, we first have initialized a variable i with value 1 and in the condition parameter we check the condition i < 10 and if the condition is true the block of the for loop will execute, and after the execution, the increment/decrement parameter will be executed and in that the value of the variable i. PHP supports C-style pre- and post-increment and decrement operators. The foreach loop in PHP is used to access key-value pairs of an array. Here is a simple example of nested for loops. We can also use a for loop inside another for loop. This example decrements a variable value on each iteration of the loop and the counter increments until it reaches 10 when the evaluation is false and the loop. increment/decrement: Here we increment or decrement the loop counter as per the requirements.Īgain, lets try to print numbers from 1 to 10, this time we will be using the for loop. which will be done by three parts initial value, condition, increment or decrement based this only while loop is working.If the condition returns true, then only the loop is executed. condition: Here we define the condition which is checked after each iteration/cycle of the loop.initialization: Here we initialize a variable with some value.The parameters used have following meaning: The for loop in PHP doesn't work like while or do.while loop, in case of for loop, we have to declare beforehand how many times we want the loop to run. To understand what are loops and how they work, we recommend you to go through the previous tutorial. In this tutorial we will learn about for and foreach loops which are also used to implement looping in PHP.
