Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-12-2008, 07:13 AM PHP Tutorial [ 4 ]
Novice Talker

Posts: 8
Name: romeo
Trades: 0
Welcome to Tutorial #4 in my PHP sega.

In this tutorial you will learn all about looping, how they work, and how to use them effectivly and properly.

- While -
=-=-=-=-=
The while function will always execute the code inside of it as long as it remains true.

Example:

PHP Code:
<?php
//set up our counting variable
$x 0;
while(
$x <= 10){
echo 
"Our While Function Is Looping Through The Number: $x
"
;
//Add a value of 1 to the variable $x
$x++;
}
?>
This should produce something like this:

Our While Function Is Looping Through The Number: 0
Our While Function Is Looping Through The Number: 1
Our While Function Is Looping Through The Number: 2
Our While Function Is Looping Through The Number: 3
Our While Function Is Looping Through The Number: 4
Our While Function Is Looping Through The Number: 5
Our While Function Is Looping Through The Number: 6
Our While Function Is Looping Through The Number: 7
Our While Function Is Looping Through The Number: 8
Our While Function Is Looping Through The Number: 9
Our While Function Is Looping Through The Number: 10


So as you can see it does it 11 times, this is because PHP parses 0 as a number so we go from 0 to 10.
Now the way I represented it wouldnt be very useful but it could come in handy for other things. Trust me i've needed it in the past.

- FOR -
=-=-=-=

The For Function is used for the times when you know how many times you wish to execute a block of code.

Example:

PHP Code:
<?php
for($i=0$i <= 10$i++[IMG]http://forums.digitalpoint.com/images/smilies/wink.gif[/IMG]{
echo "Our For Function Is Looping Through The Number: $i";
}
?>
This Again should produce something like this:

Our For Function Is Looping Through The Number: 0
Our For Function Is Looping Through The Number: 1
Our For Function Is Looping Through The Number: 2
Our For Function Is Looping Through The Number: 3
Our For Function Is Looping Through The Number: 4
Our For Function Is Looping Through The Number: 5
Our For Function Is Looping Through The Number: 6
Our For Function Is Looping Through The Number: 7
Our For Function Is Looping Through The Number: 8
Our For Function Is Looping Through The Number: 9
Our For Function Is Looping Through The Number: 10

Simple, Short and Sweet, 3 lines of PHP, 1 line less than our while function.

- Do While -
=-=-=-=-=-=-=

The 'Do While' Function loops through a block of code once, and then repeats the loop as long as the specified condition is true.

Example:
PHP Code:
<?php
$x 
0;
do{
echo 
"Our Do While Function is Looping Through The Number: $x";
}
while(
$x <= 10);
?>
That will produce something like this:

Our Do While Function is Looping Through The Number: 0
Our Do While Function is Looping Through The Number: 0
Our Do While Function is Looping Through The Number: 1
Our Do While Function is Looping Through The Number: 2
Our Do While Function is Looping Through The Number: 3
Our Do While Function is Looping Through The Number: 4
Our Do While Function is Looping Through The Number: 5
Our Do While Function is Looping Through The Number: 6
Our Do While Function is Looping Through The Number: 7
Our Do While Function is Looping Through The Number: 8
Our Do While Function is Looping Through The Number: 9
Our Do While Function is Looping Through The Number: 10

The while function is mostly used for file managers as it saves a lot of time writing everything out, and saves you anticipating what files are in the current dir etc...

- ForEach -
=-=-=-=-=-=
The Foreach Function Loops Through Each Element of an array and executes a block of code.

Example:

PHP Code:
<?php
echo "The Admins Of DarkMindZ.com are:
"
;
$names = array("RoMeO""The Reaper""Ac1d""Ingelo");
foreach(
$names as $name){
echo 
$name;
echo 
"
"
;
}
?>
That would output:

The Admins Of DarkMindZ.com are:
RoMeO
The Reaper
Ac1d
Ingelo

As you can see its useful to save space and not having to repeat the same sentance over and over.
Foreach is mostly used for Mysql Data Handling.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


Well That About Wraps this Tutorial Up.
I hope you look out For Other tutorials, articles and me personally on the website.

Enjoy.

Nusquam Est Impossible.
Ac1d

--
Source: http://www.darkmindz.com/article_read.dmz?id=9
--
DarkMindZ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-14-2008, 11:54 PM Re: PHP Tutorial [ 4 ]
phpknowhow's Avatar
Skilled Talker

Posts: 83
Name: Colin
Location: USA
Trades: 0
If you like writing these tutorials, please consider writing some content for Wikiversity.

http://en.wikiversity.org/wiki/Topic:PHP

I think it would be more beneficial to have it on the wiki instead of just a forum. Plus, you could write them on the wiki and post the link to it in the forum to continue your tutorial series.
__________________

Please login or register to view this content. Registration is FREE
| Freelance PHP solutions for small to midsized projects |
Please login or register to view this content. Registration is FREE
phpknowhow is offline
Reply With Quote
View Public Profile Visit phpknowhow's homepage!
 
Reply     « Reply to PHP Tutorial [ 4 ]
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.48540 seconds with 12 queries