Scaler
scaler.com › topics › php-tutorial › while-loop-in-php
While Loop in PHP
April 24, 2023 - A while loop in PHP is a control structure that allows a program to execute a block of code repeatedly as long as a certain condition remains true. The condition is evaluated before each iteration of the loop, and if it's true, the loop body is executed. The loop continues until the condition ...
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › while loop in php
While Loop in PHP | How While Loop works in PHP with examples & code?
March 17, 2023 - Execution of statements inside the while loop continues until the value of the variable becomes 6. Once the value becomes 5 and the condition evaluates to be false (5 > 5), the while loop terminates, and the echo statement inside the while loop will not execute. Printing the sum of digits of a given number. ... <!DOCTYPE html> <html> <head> <title>PHP while Loop Example 2</title> </head> <body> <?PHP $number = 107; $sum=0; $rem=0; while((int)$number != 0) { $rem=$number; $sum = $sum + $rem; $number=$number/10; } echo "The Sum of digits of number given 107 is $sum"; ?> </body> </html>
Call +917738666252
Address Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
While Loop using PHP with a MySQL server - Stack Overflow
I have a database (SQL) with the table "Staff" with two records in it. I am required to display the contents of these records on a web page using PHP. CES St... More on stackoverflow.com
PHP While Loop - How to update webpage / browser every irritation
You're probably running into an issue where the browser isn't outputting anything until a certain amount of data has been downloaded. The threshold in Chrome is 1KB (I think). So an example that could work in your case, could look like this: "; // Added buffer sleep(1); // visual aid ob_flush(); flush(); $counter++; } echo $buffer."All done!"; // more buffer ob_end_flush(); You're code isn't really doing anything wrong, it's just your client that impacts your results. More on reddit.com
Is it bad practice to use foreach instead of while?
Always use FOREACH when iterating over a whole array, or at least from the top of an array to a BREAK condition. This way you will never be bitten by an "off by one" error. Always use FOR when you know (or can easily determine) exactly how many times you need to execute the code in the loop. This is also useful for iterating parts of an array where you aren't always going to be starting from the head. Use a DO-WHILE when the number of times to have to execute the loop is indeterminate, but you know the code must execute at least once. Use a WHILE loop when the looping condition could be false as you enter the loop. In other words, there could be conditions where you never execute the loop. More on reddit.com
Using variable inside WHILE looop
As long as $row is not falsey it will loop. Each loop iteration it checks what $row is and stops if there are no additional rows. There are some standards that avoid this syntax (declaring a variable in an if or loop) but it works. It is often a mistake or seen as a mistake as you usually use == in the same context, but the alternative to this would uses more lines of code. It's the same as: while ( true ) { $row = ...; if ( ! $row ) { break; } } More on reddit.com
Videos
05:03
PHP while loops explained - YouTube
16:40
While Loops Php Example - Lottery Slip - YouTube
09:27
PHP For Loops and PHP While Loops - YouTube
11:38
While Loops in php | PHP Tutorial #12 - YouTube
04:50
17: What Is a While Loop in PHP | PHP Tutorial | Learn PHP ...
10:27
While Loops | PHP | Tutorial 24 - YouTube
Tizag
tizag.com › phpT › whileloop.php
PHP Tutorial - While Loop
Learn how to use a while loop in PHP in Tizag.com's PHP While Loops lesson.
W3Schools
w3schools.com › php › php_looping_while.asp
PHP while Loop
json_decode() json_encode() json_last_error() PHP Keywords · abstract and as break callable case catch class clone const continue declare default do echo else elseif empty enddeclare endfor endforeach endif endswitch endwhile extends final finally fn for foreach function global if implements include include_once instanceof insteadof interface isset list namespace new or print private protected public require require_once return static switch throw trait try use var while xor yield yield from PHP Libxml
PHP
php.net › manual › en › control-structures.while.php
PHP: while - Manual
Like with the if statement, you can group multiple statements within the same while loop by surrounding a group of statements with curly braces, or by using the alternate syntax: ... <?php /* example 1 */ $i = 1; while ($i <= 10) { echo $i++; /* the printed value would be $i before the increment (post-increment) */ } /* example 2 */ $i = 1; while ($i <= 10): echo $i; $i++; endwhile; ?>
Tutorialspoint
tutorialspoint.com › home › php › php while loop
PHP While Loop
May 26, 2007 - The echo statement in the loop body prints the current iteration number, and increments the value of x, so that the condition will turn false eventually. <?php $x = 1; while ($x<=10) { echo "Iteration No.
Codecademy
codecademy.com › learn › learn-php › modules › php-loops › cheatsheet
Learn PHP: Loops in PHP Cheatsheet | Codecademy
Otherwise, the loop terminates. The third is evaluated after each iteration. ... In PHP, while loops repeat execution of their code block as long as their conditional statement is true.
Simplilearn
simplilearn.com › home › resources › software development › guide to understand loop in php
PHP Loop: For, ForEach, While, Do While [With Example] | Simplilearn
September 9, 2025 - Loop in PHP is an Iterative Control Structure to be executed until a certain condition is met. Learn more about PHP For, ForEach, While, Do While loop Now!
Address 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
Jobtensor
jobtensor.com › Tutorial › PHP › en › While-Loop
PHP while Loop - Syntax, Examples | jobtensor
Below is an example of the while loop. The code outputs the numbers from 1 to 10. <?php $i = 1; while($i <= 10) { echo " $i <br>"; $i++; }
KoderHQ
koderhq.com › tutorial › php › iteration-control-do-while-foreach-for
PHP Loops (while, do-while, foreach & for) Tutorial | KoderHQ
We can think of a for loop as a condensed version of the while loop. A for loop has the counter and increment/decrement built-in as expressions. ... This may seem somewhat confusing at first, let’s consider an example and then do a break down of what happens. ... <?php // counter condition counter increment/decrement for ($i = 1; $i <= 10; $i++) { echo $i .
Home and Learn
homeandlearn.co.uk › php › php5p4.html
php tutorials: while loops
Instead of using a for loop, you have the option to use a while loop. The structure of a while loop is more simple than a for loop, because youre only evaluating the one condition. The loop goes round and round while the condition is true. When the condition is false, the programme breaks ...
GeeksforGeeks
geeksforgeeks.org › php › php-loops
PHP Loops - GeeksforGeeks
April 10, 2025 - PHP for loop is used when you know exactly how many times you want to iterate through a block of code. It consists of three expressions: Initialization: Sets the initial value of the loop variable. Condition: Checks if the loop should continue. Increment/Decrement: Changes the loop variable after each iteration. ... Example: Printing numbers from 1 to 5 using a for loop. ... The while loop is also an entry control loop like for loops.
Top answer 1 of 7
16
mysql_fetch_array() only retrieves a single row from the database. You need to call it inside your while loop to retrieve all rows. The $looping increment is unnecessary here, since mysql_fetch_array() returns false when no more rows are available.
Copywhile ($row = mysql_fetch_array($result))
{
echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}
2 of 7
2
I'll do...
Copywhile ($row = mysql_fetch_assoc($result)) {
// print $row;
}
Studytonight
studytonight.com › php › php-while-and-dowhile-loop
PHP While Loop and Do While Loop | Studytonight
Let's take another example where even if the condition is false, still the loop will be executed once. <?php $a = 11; do { echo $a; $a++; // incrementing value of a by 1 } while($a <= 10) ?>