"Looping Like a Pro with PHP !!

Опубликовано: 21 Март 2025
на канале: DevVault
20
0

Loops are used to execute the same block of code again and again, as long as a certain condition is met. The basic idea behind a loop is to automate the repetitive tasks within a program to save the time and effort. PHP supports four different types of loops.

while — loops through a block of code as long as the condition specified evaluates to true.
do…while — the block of code executed once and then condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true.
for — loops through a block of code until the counter reaches a specified number.
foreach — loops through a block of code for each element in an array.

PHP while Loop:
The while statement will loops through a block of code as long as the condition specified in the while statement evaluate to true.

PHP do…while Loop:
The do-while loop is a variant of while loop, which evaluates the condition at the end of each loop iteration. With a do-while loop the block of code executed once, and then the condition is evaluated, if the condition is true, the statement is repeated as long as the specified condition evaluated to is true.

Difference Between while and do…while Loop:

The while loop differs from the do-while loop in one important way — with a while loop, the condition to be evaluated is tested at the beginning of each loop iteration, so if the conditional expression evaluates to false, the loop will never be executed.

With a do-while loop, on the other hand, the loop will always be executed once, even if the conditional expression is false, because the condition is evaluated at the end of the loop iteration rather than the beginning.

Github:https://github.com/Umii010?tab=reposi...
Quora: https://www.quora.com/profile/UmerSha...