Help - Search - Members - Calendar
Full Version: [PHP] - [Loops] - [Gio]
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Gio
Loop's are pretty simple once you get to know them, if you don't, read ahead:

CODE
<?php

// Simple for now
$number = 12; // Any number for instance
for($x=0; $number>$x; $x++)
{
echo "Number: $x<br>";
}

?>


The Low down:

for(
Start a 'for' function, which means, while the inside of the brackets () are happening you do what is inside the brackets {}

$x=0;
So $x is equal to 0 now, so we know where we start!

$number>$x;
While $number is bigger than $x (12 is bigger than 0) we do what follows

$x++)
We now add 1 onto the $x, so 0 is now 1, and so forth

{
echo "Number: $x<br>";
}
So while $number is bigger than $x (12 bigger than 0++) we echo the number! And once 12 is no longer bigger than 0, and $x = $number it stops the code and stops adding onto $x!

Alittle more complex:

CODE
<?php
// Symbolising, begin the code

// Let's start basic:
$number = 12; // Any number for instance
for($x=0; $number>$x; $x++)
{
echo "Number: $x<br>";
}
else
{
echo "The allotted number was achieved!<br>Restart?<br><a href="$PHP_SELF">Yes</a>"; }
// End the code now:
?>


else
{
Now that the $x = $number we use this command instead

echo "The allotted number was achieved!<br>Restart?<br><a href="$PHP_SELF">Yes</a>";
We echo that the number was achieved and if they wish to start again

}
That's it! We ended the new loop


I understand the tutorial is lacking in greatness, but I rushed it and well, I am assuming that most of you can pick this code up and adapt it to suit your needs.
Dabu
you know that was actualy helpful to me victory.gif. My dumbass self never took the time to learn stuff that I did not have to so I have always been using whilestatements with lots o extra code to do this same thing. Thanks smile.gif
Andy
yea... i understnad that tutorial actually, just about all of it without the help, which is funny cos i didnt tihnk i know muc habout php.... not saying its 'adavanced' heh.

So, what woudl that code exactally.. do? the number would go up everytime the page was viewed or... the number would go up one every second? or... i dunno :S

Also, what could a code like this be used for?

Andy
Gio
The code is kind of like a mini count down type deal however it is not really used for that, just whipped up something quick. Glad you understood this one!
Jay
the code will echo
number 0
number 1
number 2
number 3
number 4
number 5
etc. etc. until it gets to 12
Gio
Sorry Jay when I saw your post asking me to edit mine you had already posted.
rc69
so basicly the for loop is exactly like a mySQL LIMIT statement?

either way, i have looked at loop tutorials before, the only ones ive see are while and for, so if you get another chance, i hear there are like 5 loops, and i understood your tutorial for this, mind writing some tutorials for the other 4? (not including while)
Gio
I might write some, all depends on the time I have, because I joined another development team, for a new forum software. Still working for p2l though.
MillerTime
im kinda confused on what will happen..
Gio
It will print

1
2
3
4
5

and so on until it reaches 12.
Apache
Hehe always good to review the basics. Thanks for reminding me victory.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.