![]() ![]() Let’s perform the same operation with FOR loop but with a decrement method. The for loop in php is the most complex loop in PHP that is used when the user knows how many times the block needs to be executed. Now on comparison the condition stands false because 6th is not equal to 5 or less than it so the loop reaches termination point and the control is sent out of loop body. When $i is 5 then the condition stands true and loop completes its fifth run and increment $i to 6th. 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. It typically increments or decrements the counter variable. PHP supports C-style pre- and post-increment and decrement operators. In the same way it performs the same operation unless the $i value reached 5. Increment/Decrement: The increment parameter updates the loop variable after each iteration. When the control reaches the closing braces, it returns to the condition and compares the $i value, which is now 2. When the code block starts to run, the control picks the loop initial value of $i which is 1 and compare it with the condition ($i <= 5 – checking value less or equal to 5) which stands true because $i current value is 1 so the loop completed its first run and then print the one line and increment $i value by 1. ![]() ***** // $i value is 5 - 5th loop - Condition true PHP divides the operators in the following groups: Arithmetic operators. ***** // $i value is 4 - 4th loop - Condition true Operators are used to perform operations on variables and values. In this example, the value of the variable is decremented in each iteration and displayed in an HTML div. ***** // $i value is 3 - 3rd loop - Condition true Using the decrement operator in while loop example. ***** // $i value is 2 - 2nd loop - Condition true ***** // $i value is 1 - 1st loop - Condition true - start ![]()
0 Comments
Leave a Reply. |