Home / Learn / PHP Basics / PHP Loops

PHP Loops

Learn how to define loop in PHP with for loop, foreach loop, do-while loop, and while loop.

3 years ago

93

Code Example

<?php

# for loop
for ($i = 0; $i < 5; $i++) {
	echo $i;
}

#foreach loop
$numbers = [1, 2, 3];

foreach($numbers as $number) {
	echo $number;
}

# while loop
$hungry = true;

while ($hungry) {
	echo 'eat something';

	$hungry = false;
}

# do-while loop
$thirsty = true;

do {
	echo 'drink something';
	$drink = false;
} while ($drink);
                    

Code Snippets

Collection of Laravel, AlpineJS, VueJS, Tailwind CSS, Flutter snippets and more. New snippets added every day.
View Snippets

Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your web projects
View Components

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this