php Tutorials
Creating An RSS Feed With PHP And MySQL
Retrieving XML With Curl and SimpleXML
Using curl to Query Remote Servers
MySQL Basics
PHP mySQL Voting Booth
File Download Security
PHP Fundamentals

PHP Fundamentals

Loops

for

For loops have three settings initial loop variable, contition to meet, amount to increment loop variable. for ($a=0;$a>=10;$a++) sets $a to 0 then runs through the loop and increments $a before running the loop again until the condition is met.

<? 
for ($a=0;$a>=count($array);$a++) {
  echo $array[$a];
}
 ?>

Here we threw in count which we'll get in depth with shortly. Count returns the total number of values in the variable $array.

foreach

Foreach is almost a for loop for arrays. Foreach takes each variable of an array and outputs it as another variable set in the foreach statement.

<? 
$days=array("Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday","Sunday");
foreach ($days $day) {
    echo "$day";
}
 ?>

This is for simple arrays. Below is for associative arrays.

<? 
$months=array("01" => "January",
"02"=>"February",
"03"=>"March",
"04"=>"April",
"05"=>"May",
"06"=>"June",
"07"=>"July",
"08"=>"August",
"09"=>"September",
"10"=>"October",
"11"=>"November",
"12"=>"December");
foreach ($months as $key => $value) {
    echo "$key - $value";
}
 ?>

While

While loops execute until a certain condition is met. The major difference between this and for loops is that while loops don't adjust the condition variables. While loops are great for use with getting information from mysql queries.

<? 
$a=0;
while ($a<=10) {
    echo "$a";
    $a++;
}
 ?>
Switch ... Case Flow Control <<  1 2 3 4 5 6  >> Includes and Functions
New Content