Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

PHP - Part 6

#1
How to Program PHP
Tutorial, Part 6

Welcome to part 6 of this series of tutorials! In the last tutorial, we finally introduced some concepts that allowed you to interact with a PHP script on the server. (That was a lot of tutorials to get to something so simple, eh? ) In this tutorial, we will introduce the concept of the loop, which will be an extremely useful concept that we will use in later tutorials. These concepts will become especially important when we start using databases. We're still a good way off from that, but this is nevertheless a very important concept.

Basically, a loop does two very simple things. 1) It executes a block of code over and over again, and 2) it checks some sort of condition each time before it executes. The condition is very important because otherwise, without a condition, a loop would literally execute forever. In PHP, we aren't quite as worried about crashing a server because typically a PHP script is cut off after about 30 seconds of execution time (unless your webhost has an unconventional PHP configuration), but in other programming languages, the implications of "bad loops" could be a lot worse. Tongue

The first "loop" that will be demonstrated is the "while" loop, which looks like this:

Code:
<?php
$count = 0;

while ($count < 11)
{
echo $count. "<br>";
$count = $count + 1;
}

?>

The above code is actually very simple. A "while" loop simply executes a block of code over and over again, as long as a specific condition is met each time. If you run this script, you will see the script count from 0 to 10 in your browser. What makes it stop at 10?

Notice how the condition of the while statement is "($count < 11)". Basically, the condition is that the $count variable is less than 11. If it is equal to 11 or more, this "while" statement will not execute. If it is less than 11, the while statement will execute, and will continue to execute as long as $count remains less than 11. Now of course it won't increment count by itself, so we'll have to make sure that we do that in our loop to prevent an "endless loop" that runs eternally. Tongue

Notice these lines:

Code:
echo $count. "<br>";
$count = $count + 1;

The second line is what actually increments $count by 1. Every time this while loop executes, it will increment $count by 1 because this line is "in" the loop. The line before it simply echoes the value of the $count variable, so you will see it in your browser. It also adds a <br> tag so that each number is on each line, so don't be confused by that part of the statement. What you'll end up seeing in your browser when you run this script will be:

Quote:0
1
2
3
4
5
6
7
8
9
10

As you can see, the while loop is pretty simple. It is an extremely useful loop in PHP, and it will be important to have a solid understanding of how it works. Of course the while loop isn't the only kind of loop that PHP has to work with. There are several variants, including the do-while loop, which is a variant of the while loop where first the block of code will be executed once, and then it will check against a condition before it runs the loop any additional times. What's different about the do-while loop is that a do-while loop will always execute at least once. A while loop won't even execute at all if the condition is never met, but a do-while loop is guaranteed to run at least once, even if no conditions are ever met.

The do-while loop isn't as common as the while loop, so I won't cover it specifically in this tutorial, but if you would like to learn more about how it works, click here for the official explanation from the online PHP manual.

There are of course several other loops in PHP, but none of them are as essential. One loop that I will cover later on is the for loop. The for loop is like the while loop, but it increments the variable automatically instead of requiring you to make a statement to increment a variable. (this sounds useful, but all it really does is save you one extra line of code. ) There is nothing that can be done with a for loop that cannot be done with a while loop, so this isn't very essential for us to glance over yet. If you would like to gain a better understanding of a for loop now, feel free to look at this page from w3schools.

This has been a fairly short tutorial. In my post below, I will give a more "interactive" example of the while loop, and in future tutorials, we'll continue to dive into more complex concepts. Big Grin

Reply


Messages In This Thread
PHP - Part 6 - by Darth-Apple - November 8th, 2013 at 2:33 AM
RE: How to program PHP Tutorials - part 6 - by Darth-Apple - November 8th, 2013 at 2:34 AM
RE: How to program PHP Tutorials - part 6 - by Damian B. - November 12th, 2013 at 5:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP - Part 5 Darth-Apple 5 10,581 August 28th, 2013 at 10:10 PM
Last Post: Darth-Apple
  PHP - Part 2 Darth-Apple 2 6,750 August 19th, 2013 at 9:26 PM
Last Post: Darth-Apple
  PHP - Part 1 Darth-Apple 4 9,644 August 19th, 2013 at 7:40 PM
Last Post: Damian B.
  PHP - Part 4 Darth-Apple 0 4,208 August 13th, 2013 at 4:29 PM
Last Post: Darth-Apple



Users browsing this thread: 1 Guest(s)

Dark/Light Theme Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2024
Forum design by Makestation Team © 2013-2024