It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum Script-archive

For Loop Example

Share your completed scripts.

Moderator: Malcolm

For Loop Example

Postby HotNoob on Tue Sep 21, 2010 9:56 pm

This is just a quick bit to show how to use a for loop in PHP

Code: Select all
<?php
$string = "Hello World";
$length = strlen($string);

for($i = 0; $i < $length; $i++)
{
    echo $string[$i].'<br />';
}

?>


The For Loop Syntax.
It is actually really simple
Code: Select all
for(VARIABLE DECLARATION; CONDITION; INCREMENT)

What happens during the code execution is that at the beginning, it runs the variable declaration code first, then it checks the condition statement( like an if statement), and if it is true, than it runs through the block of code(whats in between the brackets), and finally it calls the increment code.

Unfortunately, in PHP the for loop is LESS efficient than the foreach loop, which is the complete opposite when compared to most programming languages, such as c, c++, c#, and so on.

well i hope that shows you guys some of the basics :D
HotNoob
100+ Club
 
Posts: 169
Joined: Sun May 02, 2010 1:38 am

Who is online

Users browsing this forum: No registered users and 0 guests