Left Quote    If at first you don't succeed, redefine success.
- Anon.    
Right Quote
[login] | [Register]

Simple PHP Tutorial

by: Jester
Page: 5 of 9
(View All)

Control Structures

For those of you who know Perl, you will see that PHP has borrowed alot from Perl. Infact the first PHP interpreter was written using Perl, the syntax is very similar in alot of ways.


The foreach function is for use with arrays. It allows you to go through each item in an array.

Go through each value in $fruits and call it $fruit. The above code would output:


The first parameter in the foreach() function tells PHP which array to go through. The second parameter tells PHP which variable to assign the value to as it goes through them. So as foreach() handles the first array item, $fruit contains "apple". As foreach() handles the second array item, $fruit contains "pear". This repeats until each item in the array has been processed.

Foreach is a very useful control structure. In the above script, if, on its travels, foreach finds an array value that is equal to "apple" the if statement is true and it will print out that the array contains "apple". It will go through each array item and check if the conditional statement is true.


the while() function has the syntax:

while(condition) {
// PHP code here

This function does exactly what it says. WHILE the condition is true it will execute the PHP code contained within the curly braces.

WHILE 1 is equal to 1, print out "1 is equal to 1, duh!". 1 will always be equal to 1, so this while() function would loop through over and over, forever. The while() function will execute until the condition within the brackets is false.

Will output:

loop finished

The while() function checks "is the value stored in $number less than 10?" it contains 0, so it is less than 10. The statements between the curly braces are then executed. We print out the value stored in $number and then increment it (add one to it). $number is now 1, the function checks is it still less that 10? Yes, it prints it out and increments it again, this repeats until the value stored in $number is no longer less than ten. When $number is equal to nine, it is printed out and then incremented to ten, it is then no longer less than 10, the loop exits and the script continues.


We could also get the same results as above using the for() function. Here is the format:

for(intialise; condition; action) {
// PHP code

Firstly we intialise, then we enter under which condition to execute the for loop, then we define an action to be executed each time we go through the loop.

for($number = 0; $number < 10; $number++) { echo "$numbern"; } echo 'loop finished';

This would output:

loop finished

$number = 0; - Firstly we intialise the variable $number with a value of 0.

$number < 10; - Then we tell the for function to keep executing the code in the curly braces until the conditional statement $number < 10 is no longer true.

$number++ - Lastly we define the action, in this case we tell the for function to increment $number each time it goes through a loop.

Thus we get the results above, set $number to zero. While $number is less than 10, execute the code in the curly braces, at the start of each loop add 1 to $number. This results in the for function printing out the value stored in $number then looping through and adding 1 to $number until $number is not less than 10, then the loop exits and the script continues.

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  
Next »

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

1 Total Comments
3.25 Rating of 5 (4 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope

"" Copyright © 2002-2019; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank