Register for your free account! | Forgot your password?

Go Back   elitepvpers > Blogs > manulaiko3.0
You last visited: Today at 14:25

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Rate this Entry

Programming in PHP - Control Structures (part 2)

Posted 02/04/2015 at 22:50 by manulaiko3.0

Copy and paste from my website:

The last time we talk about control structures that choose between 1, 2 and more options, now we’re going to talk about loops.
What is a loop?

A loop is a piece of code that repeats X times, the times that the code is repeated is given by a condition, there are 2 (actually 4, I will explain 3 because I’ve never used the last one and I don’t like it) control structures that lets you loop through the code:
  • For
  • While

For

This is one of the most powerfull control structures (all the control structures are necessary and powerfull) and it looks like this:
for (iterator declaration; condition; iterator incrementation/decrementation)
//do something..
Well, lets explain this.
As every other control structure it starts with the keyword for and is followed by 3 instructions between parenthesis:
  • Iterator declaration
  • Condition
  • Iterator incrementation/decrementation

Iterator declaration

This instruction can be omited, but I really recommend to don’t do it. Bassically you make a new variable and assings it a value.
The most used is an integer variable called i (short of iterator) and it starts in 0.
Condition

This is easy, it’s a comparation, the loop won’t stop until that comparation is true.
Iteration incrementation/decrementation

If the iterator never changes, then the condition will never be true, and that means that the loop won’t finish till the end of the universe (or the end of computer [soon if you’re running in a Slackware with 760MB ram and a Pentium 4 like me :P]), for that reason, the last instruction between the parenthesis is dedicated to change the iterator.
Here you bassically asing a new value based in the last value of the iterator (+1, -2, +4, *134…)

Let’s see how does this look in PHP:

<?php for($i = 0; $i < 10; $i++) { //Iterator declaration: $i = 0 //Condition: $i < 10; //Iterator incrementation: $i++, it's the same as $i = $i + 1 echo $i; } //The script should print "0123456789" And that’s all about the for loop

While

This is more simple than the for loop, it looks something like this:
while (condition)
//do something…
The keyword is while and is followed by a condition between parenthesis, untill that condition isn’t true, the loop won’t end.
This loop needs to declare the iterator before the while keyword (in the upper line) and must be modified inside its block, let’s look at the code:

<?php $i = 0; //declare the iterator while($i < 10) { echo $i; //Increment the iterator $i = $i + 1; //same as $i++ } //The script should print "0123456789" And that’s all about while loop. No more shit untill next chapter: Arrays
See you!
Posted in Uncategorized
Views 507 Comments 0 Email Blog Entry
« Prev     Main     Next »
Total Comments 0

Comments

 

All times are GMT +1. The time now is 14:26.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.