For Loops in PHP

(0 votes, average 0 out of 5)

To create a for loop in php, follow these simple steps:

  1. Set a counter variable to some initial value.
  2. Check to see if the conditional statement is true.
  3. Execute the code within the loop.
  4. Increment a counter at the end of each iteration through the loop.

The following is pseudo php code for a for loop:

1
2
3
for(init counter; conditional statement; increment counter){
	inner code;
}

As you can see, all of the above steps are taken care of within the for loop, making the syntax short and easy to understand. 

Here is an example of a php for loop:

1
2
3
4
5
6
7
8
9
10
11
$price = 5;
 
echo "\"0\">";echo "";echo "";for( $count = 0; $count <= 10; $count++){	echo "";	echo "";}echo "
Quantity Total Price
".$count." ".$price * $count."
"
;

The code above will produce the following output:

Quantity Total Price
0 0
1 5
2 10
3 15
4 20
5 25
6 30
7 35
8 40
9 45
10 50

 

 

 

 

 

 

 

 

 

 

 

Partner Links:
Last Updated on Tuesday, 29 June 2010 07:32  
Related Articles

» How to Shutdown or Restart Windows From the Command Line

To shutdown or restart windows from the command line or using a shortcut, you must use the Shutdown.exe program. To use this program, open up a command prompt window. This can be done by navigating to Start -> Run and typing in 'cmd'.Once the command prompt is open, use the following command:shutdown [-i | -l | -s | -r | -a] [-f] [-m computername] [-t xx] [-c "comment"] [-d up:xx:yy]Arguments:  No args Display this message (same as -?)-i Display GUI interface, must be the...

» Merge Two Versions of A Table Together in a Database

When keeping multiple versions of a website, or perhaps migrating a website to a new environment, it is often necessary to merge any changes between the two databases. The databases have the same structure, but each has new, unique content which needs to be added to the other. The following tutorial uses syntax for MySQL, however it should be applicable to other major database schemas. The following example assumes that the master database where we want all of the changes to reside is called...

» SQL SELECT DISTINCT Statement

To select information from a database without listing duplicates in the same column, use the SELECT DISTINCT statement in SQL. Syntax: 1SELECT DISTINCT <column name(s)> FROM <table name> For the column name(s) field, enter the names of the columns you would like to pull the distinct data from in the table.For the table name, enter the name of the table from which you are accessing the data. Examples:Suppose we have the following dataset in our database within a table...