There are four types of loops in PHP: while
, for
, foreach
, and do-while
.
The while
loop will execute a block of code as long as the specified condition is true.
main.php57 chars4 lines
Example:
main.php51 chars6 lines
Output:
main.php6 chars2 lines
The for
loop is used when you know beforehand how many times the script should run.
main.php83 chars4 lines
Example:
main.php45 chars4 lines
Output:
main.php6 chars2 lines
The foreach
loop is used to iterate over arrays.
main.php67 chars4 lines
Example:
main.php91 chars6 lines
Output:
main.php13 chars2 lines
The do-while
loop will always execute the block of code at least once, even if the condition is false.
main.php62 chars4 lines
Example:
main.php55 chars6 lines
Output:
main.php6 chars2 lines
gistlibby LogSnag