TheTAZZone - Internet Chaos

Tutorial – PHP for Newbies 2

Tutorial – PHP for Newbies 2

In this section I’ll be discussing PHP loops.

Prerequisites

To follow along in this tutorial, it may be beneficial to install a text editor somewhat more tailored to coding than the basic bland text editor that comes stock with your computer. It will take a bit of research to determine what you like best, but you should look for an editor that numbers lines to make finding the source of errors easier, highlights different tags based on syntax, and of course is something you feel comfortable with.

Introduction to Loops

A “loop” in PHP causes the same block of code to be executed multiple times until it reaches a predefined point. One practical example might be when you select how many “posts” or “images” or “somethings” you want to display per page while viewing a web page. A PHP loop continues to load blog posts, images, or forum threads until it reaches that threshold.

Various Loops

1) The “while” loop will continue to execute a block of code until a particular condition is met.

Example:

Code:
<html>
<body>
<?php
$counter=1;
while($counter<=5)
{
echo “Number “ . $counter . “<br />”;
$counter++;
}
?>
</body>
</html>

In the above example, I created the variable $counter and set its value to 1. I then created the while loop which checks the value of $counter, and if it is less than or equal to 5, it initiates the code between the { and }. The code block (everything contained within {}) outputs the string “Number “, concatenates (joins) the current value of $counter, and adds (using concatenation again) the HTML <br /> tag to create a line break on the web page. Finally, it increases the value of $counter by one. Note that the { and } tags create blocks of code that are executed simultaneously instead of one at a time. Save the above into a .php file and you should receive the output:

Number 1
Number 2
Number 3
Number 4
Number 5

2) The “do…while” loop performs essentially the same function but can be written differently.

Example:

Code:
<html>
<body>
<?php
$counter=1;
do
{
$counter++;
Echo “Number “ . $counter . “<br />”;
}
while ($counter<=5);
?>
</body>
</html>

The result will be the same on the web page as the previous example.

3) The “for” loop has a more specific syntax. Inside the parentheses after “for” you must first specify the initial value of the counter, then the condition that is evaluated each interation, and finally the amount to increment the initial value. Each of these is separated by a “;”.

Syntax:

for (init; condition; increment)
{
Code;
}

Example:

Code:
<html>
<body>
<?php
For ($counter=1; $counter<=5; $counter++)
{
echo “Number “ . $counter . “<br />”;
}
?>
</body>
</html>

As you can see, this is a quicker way to accomplish the same end. Also note that the $counter variable can be whatever you like it to be. If you changed the variable to $lolcat it would perform the exact same function in the above scenario. It might be more efficient to use a single letter variable such as $x.

4) The “foreach” loop is used in conjunction with arrays. For example, if you simply wanted to display (in order) each value saved in an array, you could use this loop.

Example:

Code:
<html>
<body>
<?php
$x=array(“1”,”2”,”3”);
Foreach ($x as $y)
{
Echo $y . “<br />”;
}
?>
</body>
</html>

In the above example, I defined an array ($x), populated it with the values 1, 2, 3 (notice that they are in quotes, which causes them to be text strings instead of numeric values), and then in the foreach loop assigned the current value of $x to $y. I then echo’d the current value of $y and inserted a line break. The resulting output is:
1
2
3

That’s it for the basic loops!

In the next tutorial I’ll start covering the variety of ways one can pass information from one page to another.

ORIGINALLY POSTED BY KEEZEL FOR THETAZZONE/TAZFORUM HERE

Do not use, republish, in whole or in part, without the consent of the Author. TheTAZZone policy is that Authors retain the rights to the work they submit and/or post…we do not sell, publish, transmit, or have the right to give permission for such…TheTAZZone merely retains the right to use, retain, and publish submitted work within it’s Network

One Response to Tutorial – PHP for Newbies 2

  1. Taste January 30, 2010 at 10:49 pm

    Thanks for the song “Color Him Father”. I love music and I’m taking a African American Music course. I’m learning about our music and a lot of the music my parents had at home and I never knew the meaning behind the songs, but now I am appreciating it more.

Leave a Reply

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

Advertise

If you'd like to advertise on The Mutt ( aka TheTAZZone.com ) feel free to contact us at: administration[at]thetazzone.com

TheTAZZone is a non-commercial entity. We do not sell any products or services ourselves. Our revenue comes from advertising and donations only.

We appreciate your support! Your advertising revenue ( or donations ) helps us to continue to upgrade, improve, and offset the costs of maintaining this site.

Donations can be made through the page ' Donate '.