Site Meter Web Dev Notes » Blog Archive » The magic of WHILE Loops

The magic of WHILE Loops

by

The WHILE loop is probably the most popular of the loop slaves because he is very intuitive to our needs as a programmer or web developer. Our WHILE loop keeps going while our statement which we give it to evaluate remains true. Very simple and efficient!

<?php
   $factorial = 1;
   $factorialThis = 5;
   while ($factorialThis > 0)
   {
      $factorial = factorial * factorialThis;
      $factorialThis--;
   }
?>

The code above is jibberish unless you know what a factorial is. I’ll take the assumption that if you are reading this you are at least moderately intelligent and thus will be able to figure out what it is by means of a simple example rather than a long explanation that should be read before going to bed if you wish to quickly fall asleep.

Five factorial, written commonly as 5! is equal to 5×4x3×2x1 = 120.

In the above example we compute the value of five factorial very quickly within our WHILE loop! The best part is, our code is very flexible thanks to the WHILE loop. Sure, we could have calculated the same thing using integer variables, but with the above code we can simply change the value of $factorialThis to compute the factorial of any number.

Really, this is magical and as a programmer, you must learn the spells if you hope to be a powerful wizard! Don’t tell people though that you are both a programmer and a powerfull wizard, as if you weren’t cool enough already.


One Response to “The magic of WHILE Loops”

  1. Web Dev Notes » Blog Archive » Introducing the different PHP Loops Says:

    [...] WHILE Loops [...]

Leave a Reply


About Web Dev Notes

Your one stop destination for anything and everything related to web development

Web Dev Notes Author(s)

Blogging Flair

New Media, Web 2.0 Channel Posts

  • 10 Ways to Make Sure Your SEO Goes Out of Its Way for You
    If they want to have success, companies should do everything they can to ensure that their SEO firm doesn't provide lousy service. Here are 10 tips to keep in mind: 1. Be realistic. Don't waste your [...]
  • Podcasting Transcription
    A Guest Post from Tishia Lee of Tishia Saves Time: When I first started offering transcription services as part of my Virtual Assistant business, transcribing podcasts was not something I [...]
  • Don't Dabble - Make A Commitment
    If you want to get the best bang for your buck, plan on podcasting for the long haul. Podcasters who “test” things out with one or two podcasts and then give up may think that they've given [...]
  • A Question of Podcasting Frequency
    This follows up on yesterday's post about not dabbling in podcasting but rather to make a commitment. Podcasting on a schedule is important when building a community and an audience. Don’t have [...]
  • Do You Twitter About Your Podcast?
    Whenever I release a new episode of Work at Home Moms Talk Radio is pop the link up on my Twitter and invite my followers to come check it out. This has brought me several first time listeners which [...]
  • Looking for Podcast Outsourcing?
    I mentioned having my podcast audio's transcribed in my last post. In case you wonder - no, I do not transcribe them myself. (Shudder the thought - I did enough transcription to last a lifetime in [...]
  • Business Podcasting Benefit: Be An Industry Thought Leader
    I often ask people 'Why haven't you started your podcast yet?' and a common reply is 'I don't feel like I'm enough of an expert to cover the topic I'm interested in.' Enough of an expert? [...]
  • Business Podcasting Benefit: More Content
    Building a business on the web requires that you generate a lot of content. Content on your website draws traffic both through search engines (people searching for your content) and through links [...]
  • Business Podcasting Benefit: Deeper Relationships
    A website visitor is just a website visitor. You can't really say that you have a relationship with someone who has only visited your website can you? But when the web visitor subscribes to [...]
  • Business Podcasting Benefit: Increased Market Exposure
    When you consider how many millions of people are walking around with iPods and other Mp3 players you have to wonder, wouldn't it be awesome if they came looking for you? Well they do. Ipod [...]

Hot Off The Press