Introduction to PHP while loops

Introduction to PHP while loops

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on March 24, 2009

This is just a quick introduction for beginners getting started with PHP and using while loops. You will quickly find that while loops are used regularly to cycle through specific data.

So here’s a little code snippet of a simple while loop in action.

<?php
$number = 0;
while ($number <= 10)
{
echo $number;
$number++;
}
?>

Lets run through the code to see whats happening. we start by using a php delimiter tag to declare that we are using php script. all your php code needs to be wrapped in these tags. e.g. <?php ‘code goes here’ ?>. The next line we’re setting the variable $number and giving it a value of ’0′, we then set the while loop and in the brackets set the ‘while clause’ in our case we’re saying that while $number is less than or equal to 10 then do whats in the curly brackets. In the brackets we’re just echoing the variable $number and after that we re-set the value of $number adding one to it.

If you want to increase a number by one each time simply put ++ after the variable. for example.

<?php
$i=2;
$i++;
echo $i;
?>

in the example above $i will be 3.


There are many uses for while loops, this is a basic introduction for those who are new to php. If you have any questions or comments please leave them below.

demo

More tutorials from Papermashup
Comments
One Response about Introduction to PHP while loops
  1. Rocky says:

    $query = mysql_query(“SELECT * FROM `my_table`”);

    while($row = mysql_fetch_assoc($query)){
    echo $row['value'];
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for a registry cleaner to speed up your PC and show a full diagnostics?
Faster surfing with Dish Network High Speed Internet

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.