WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You

WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted Loops in PHP Fri Jan 07, 2011 1:52 am

Unknown Data

Unknown Data
Registered Member
Registered Member
Loops in PHP
Learn PHP - part 4


IF loops

This tutorial is much different from the other PHP tutorials. I'm talking about the IF structure, because we are going to learn about the loops.
Loops is blocks of commands/conditions which traversed and is controlled for their accuracy (true or false).
Another explanation could be IF this is correct THEN do this.

Here is a little example to illustrate it.
Code:
<?php
$x = 20;
if ($x != 10)
{
echo $x;
}
else
{
echo "The number is 10";
}
?>

The variable x will assign the value 20. The IF conditions are now controlling if the variable x is uneven from 10. In that case will the x be named 20. If the x was 10 will "The number is 10" getting returned.

The structure in a IF sentence will always be equal. IF are followed by a assignment that are placed in parentheses. And that's why the part we wish to perform, is surrounded by curled parentheses.
In our example have we added a else condition that will work if the variable x was 10. Remember that this isn't necessary.

Loops with "for"

Let's start with a example.
Code:
<?php
for($i = 0; $i < 10; $i++) {
  echo "This text is written for the $i. time
";
}
?>
It will show up like

This text is written for the 0. time
This text is written for the 1. time
This text is written for the 2. time
This text is written for the 3. time
This text is written for the 4. time
This text is written for the 5. time
This text is written for the 6. time
This text is written for the 7. time
This text is written for the 8. time
This text is written for the 9. time


And let's then look what the code is doing.
• Once the PHP parses the code will the first look at for, and then look for three terms in the parentheses which are separated by colons. The first declaration / statement sets variable i equal to 0 ($ i = 0).
• After that will it take a look at the middle statement to see if this is true. It looks for the variable i is lesser than 10 ($ i <10).
• Since we set the variable i to be 0 in the first statements, will php proceed to the third statement which says $ i + +, which indicates that the variable is conferred on the first.
• After that will it perform what there stand in the curly parentheses. This loop is traversed as many times as necessary until the variable is $ i <10. When the variable i is equal to 10, will it be the statement false and the loop will stop.

Loops with "while"
We starts with a look at the code again.

Code:
<?php
$y = 0;
while ($y < 10) {
  echo "The number is $y
";
  $y++;
}
?>

An it looks like.

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9


And let's again then look what the code is doing.
• Again will the variable y be 0.
• Now will the loop start and controls if the variable y is lesser than 10 ($y < 10).
• As long as the conditions is true and the variable is lesser than 10, will the text be returned to the curly parentheses.
• $y++ will count 1+ for each pass.
• The loop will end when the variable is 10.

Notice : This tutorial is copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

http://woops.dk

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum