Jump to content


- - - - -

[PHP] - [Loops] - [Gio]


  • Please log in to reply
10 replies to this topic

#1 _*Gio_*

_*Gio_*
  • Guests

Posted 09 September 2004 - 03:43 PM

Loop's are pretty simple once you get to know them, if you don't, read ahead:

<?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:

<?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.

#2 _*Dabu_*

_*Dabu_*
  • Guests

Posted 09 September 2004 - 04:15 PM

you know that was actualy helpful to me :). 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 <_<

#3 _*Andy_*

_*Andy_*
  • Guests

Posted 09 September 2004 - 04:23 PM

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

#4 _*Gio_*

_*Gio_*
  • Guests

Posted 09 September 2004 - 05:02 PM

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!

#5 _*Jay_*

_*Jay_*
  • Guests

Posted 10 September 2004 - 04:54 PM

the code will echo
number 0
number 1
number 2
number 3
number 4
number 5
etc. etc. until it gets to 12

#6 _*Gio_*

_*Gio_*
  • Guests

Posted 10 September 2004 - 09:43 PM

Sorry Jay when I saw your post asking me to edit mine you had already posted.

#7 _*rc69_*

_*rc69_*
  • Guests

Posted 17 September 2004 - 10:10 AM

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)

#8 _*Gio_*

_*Gio_*
  • Guests

Posted 17 September 2004 - 12:32 PM

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.

#9 MillerTime

MillerTime

    Young Padawan

  • Members
  • Pip
  • 69 posts

Posted 31 December 2004 - 05:13 PM

im kinda confused on what will happen..

#10 Gio

Gio

    Jedi In Training

  • Members
  • PipPip
  • 317 posts

Posted 01 January 2005 - 11:28 PM

It will print

1
2
3
4
5

and so on until it reaches 12.

#11 Apache

Apache

    P2L Jedi

  • Twodded Staff
  • PipPipPip
  • 778 posts
  • Location:London, UK

Posted 23 March 2005 - 06:14 PM

Hehe always good to review the basics. Thanks for reminding me :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users