PHP Loops

PHP Loops For Loop

Syntax:
for (expr1, expr2, expr3)
statement;

expr1 is only evaluated at the very beginning of the loop once.
expr2 is evaluated at the beginning of every loop.
expr3 is what happens at the end of each and every loop.

Syntax restated:
for (initial, test, each)
statement;

initial: is the initial value we are giving where we are initializing something.
test: is the test we are performing each time and as long as it is true the loop will continue.
each: is the action that will be performed at the end of every loop before looping back to the top and doing the test again.



Syntax:
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}

Example:
for ($count=0; $count <= 10; $count++) {
echo "Count is: " . $count . ",
";
}

Output:
Count is: 0,
Count is: 1,
Count is: 2,
Count is: 3,
Count is: 4,
Count is: 5,
Count is: 6,
Count is: 7,
Count is: 8,
Count is: 9,
Count is: 10,



Foreach Loop - used on arrays.
Syntax:
foreach ($array as $value) {
code to be executed;
}

Unlike the "for" statement that works on a boolean, the "foreach" only works on an array.
foreach looks at each item in the $array and if there are items still in the array, each one will become the $value.
The $value is a temporary variable that can be referenced in the statement. It is created on the fly using the "as" keyword.

Example:
$ages = array(4, 8, 15, 16, 23, 42);
// using each value
foreach ($ages as $age) {
echo "Age is " . $age;
}

Output:
Age is 4
Age is 8
Age is 15
Age is 16
Age is 23
Age is 42



Foreach Loop - used on arrays and associative arrays.

Using foreach loops on an associative array.
foreach can take each array as a key:value pair.

Syntax:
foreach ($array as $key => $value){
code to be executed;
}
The $key is the $position of the object in the $array. In the array below, the number 4 ($value) is in position zero, 8 ($value) is in position one, etc.

Example:
$ages = array(4, 8, 15, 16, 23, 42);
foreach($ages as $position => $value) {
echo $position . ": " . $value;
}

Output:
0: 4
1: 8
2: 15
3: 16
4: 23
5: 42


Another example with strings and integers in the associative array:
$prices = array("Brand New Computer" => 2000, "1 month in Lynda.com Training Library" => 25, "Learning PHP" => "priceless");
foreach($prices as $key => $value) {
if (is_int($value)) { //is_int looks for integers.
echo $key . ": $" . $value . ".00";
}
else {
echo $key . ": " . $value; //If not an integer then just print it.
}
}

Output:
Brand New Computer: $2000.00
1 month in Lynda.com Training Library: $25.00
Learning PHP: priceless

Multi-Dimensional Arrays
  print_r($rows);
Array
  (
      [0] => Array
          (
              [TABLE_NAME] => vw_customer_display_nodocs
              [IS_UPDATABLE] => YES
          )

      [1] => Array
          (
              [TABLE_NAME] => vw_ffl_book1
              [IS_UPDATABLE] => YES
          )

      [2] => Array
          (
              [TABLE_NAME] => vw_ffl_book2
              [IS_UPDATABLE] => YES
          )

      [3] => Array
          (
              [TABLE_NAME] => vw_ffl_bustax_docs
              [IS_UPDATABLE] => YES
          )

      [4] => Array
          (
              [TABLE_NAME] => vw_ffl_license_docs
              [IS_UPDATABLE] => YES
          )

      [5] => Array
          (
              [TABLE_NAME] => vw_ffl_ntn_inv_num
              [IS_UPDATABLE] => YES
          )

      [6] => Array
          (
              [TABLE_NAME] => vw_ffl_weapondocs
              [IS_UPDATABLE] => YES
          )

      [7] => Array
          (
              [TABLE_NAME] => vw_fflbook_transaction_number
              [IS_UPDATABLE] => YES
          )

      [8] => Array
          (
              [TABLE_NAME] => vw_ffl_not_disposed
              [IS_UPDATABLE] => YES
          )

      [9] => Array
          (
              [TABLE_NAME] => vw_ffl_disposed
              [IS_UPDATABLE] => YES
          )
  )


  echo $rows[0]['TABLE_NAME'] . "\r\n";


  foreach ($rows[0] as $key[0] => $value) {
      echo "$key[0]: $value\n";
  }


  function myfunction($value,$key)
    {
      echo "The key $key has the value $value.\r\n";
    }
    $i=count($rows);
    for($x = 0; $x < $i; $x++){
      array_walk($rows[$x],"myfunction");
  }


  $i=count($rows);
  for($x = 0; $x < $i; $x++){
    foreach($rows[$x] as $key => $value){
      echo "$key: $value \r\n";
    }
    }


  $i=count($rows);
  for($x = 0; $x < $i; $x++){
    echo $rows[$x]['TABLE_NAME'] . " " . $rows[$x]['IS_UPDATABLE'] . "\r\n";
  }



Do While Loop.

Syntax:
do {
code to be executed;
}
while (condition is met);

Example:
$x = 0;
do {
echo "The number is: $x";
$x++;
}
while ($x <= 10);

Output:
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
The number is: 10



While Loop.

Syntax:
while($x <= 100) {
code to be executed;
}
$x+=10;

Example:
$x = 0;
while($x <= 100) {
echo "The number is: $x";
$x+=10;
}

Output:
The number is: 0
The number is: 10
The number is: 20
The number is: 30
The number is: 40
The number is: 50
The number is: 60
The number is: 70
The number is: 80
The number is: 90
The number is: 100