Home arrow PHP arrow Mastering WHILE Loops for PHP and MySQL

Mastering WHILE Loops for PHP and MySQL

Do you want to learn how to handle PHP WHILE loops? WHILE loops are one of the most powerful features as well as the easiest loop available to any PHP/MySQL developer. They enable us to shorten repetitive tasks for a highly useful application. This tutorial gives examples of WHILE loops in PHP/MySQL that beginner and novice developers can use as a quick reference for building similar loops in their applications.

TABLE OF CONTENTS:
  1. Mastering WHILE Loops for PHP and MySQL
  2. Using WHILE Loops as Counters
  3. Generating Unique Random Numbers Using WHILE Loops
  4. Constructing Trigonometric Tables Using WHILE Loops
  5. Retrieving All Rows in a MySQL Table Using WHILE Loops
By: Codex-M
Rating: starstarstarstarstar / 9
October 27, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The early part of this article will focus on both basic and advanced WHILE looping techniques entirely written for PHP. The last part will be devoted to the use of WHILE Loop in displaying MySQL queries.

There are different types of loops in PHP that suit different applications. Seeing actual examples and real applications will enable a developer to use this information to determine which loop script is similar to his intended application; thus, he can use it to write derivative programs. This marks the first step towards mastery in writing loop statements in PHP/MySQL.

If you are interested, keep reading.

Basics: Loop to output increasing or decreasing sequence of numbers

This is basic; a lot of applications depend on outputting an increasing or decreasing sequence of numbers. See the examples below:

Example #1: Output numbers from 1 to 10 in HTML

<?php

$i=1;

while ($i<=10) {

echo $i++;

echo '<br />';

}

?>

What if you need to produce numbers starting from 51 and continuing to 202? All you need to do is customize using the format (bold):

<?php

$i=51;

while ($i<=202) {

echo $i++;

echo '<br />';

}

?>

Example #2: Output a decreasing sequence of numbers: 10 to 1 in HTML

A variation of this can be formulated but using $ió

<?php

$i=10;

while ($i>=1) {

echo $i--;

echo '<br />';

}

?>

The situation is somewhat reversed from what is shown in Example 1.

Example #3: Output even numbers in HTML:

We even use $i++ to produce even numbers; see below. The code will produce an even number of 2, 4, 6, 8, 10.Instead of using 10 as the last number, 8 is used, since the loop will add twice.

<?php

$i=0;

while ($i<=8) {

$i=($i++)+2;

echo $i;

echo '<br />';

}

?>

The same principle applies to outputting odd numbers or any type of number sequences.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: