Php-mysql Course

The for() is a repetitive instruction. It is used when you wish to perform a command, or a block of code several times.
In PHP there are two different types of for loops: for() and foreach().

For loop

The for loop is used when you know how many times the script should run.
  - Syntax:
for (init; condition; increment) {
  // php instructions
}
init - is used to set a variable as a counter (but can be any code to be executed once at the beginning of the loop).
condition - is evaluated for each loop iteration. If it evaluates to TRUE, the loop continues and executes its PHP instructions. If it evaluates to FALSE, the loop ends.
increment - is used to increment (or decrement) a counter (but can be any code to be executed at the end of each loop iteration).
  - Example:
<?php
for ($i=0; $i<5; $i++) {
  echo '<br/> The counter is: '. $i;
}
?>
The first time this loop is run, the $i variable (the counter) is set to the value of 0, ($i=0). Then the condition $i<5 checks to determine whether the code loop should be executed (when the condition is True).
After each execution of the code loop, the statement $i++ is run. This expresion defines how the testing variable (the counter) should be altered at the end of each iteration (here "$i++" increments the value of "$i" by 1). Then the condition is checked, and so forth.
This process continues until the condition is False ($i=5).
This script will display the following result:
The counter is: 0
The counter is: 1
The counter is: 2
The counter is: 3
The counter is: 4

Inside the for instruction you can introduce other "for()" or any conditional statements.

Foreach loop

The foreach loop is used to iterate through every element in Arrays. It has two syntaxes:
  1) - Syntax, to access every array element:
foreach ($array as $value) {
  // Do something with $value.
}
The foreach() loop will iterate through every element in $array, assigning the value of each element to the $value variable.

  2) - Syntax, to access both the keys and values:
foreach ($array as $key => $val) {
  // Do something with $key and /or $val.
}
This construct will place the key and value of the current $array element into their own variables: $key and $val.

  - Example:
<?php
$arr = array('n1'=>'one', 'n2'=>'two', 'n3'=>'three');
foreach ($arr as $key => $val) {
  echo "<br /> The value at $key is $val";
}
?>
For every loop iteration, the key and value of the current $arr array element are assigned to $key and $val variables (and the array pointer is moved by one). On the next loop iteration, the "foreach" will be looking at the next array element, and so forth till the last element of the $arr array.
Output:
The value at n1 is one
The value at n2 is two
The value at n3 is three

You can learn about Arrays in the next lesson.

End the For loops with the break instruction

If you want to end a "for" (or "foreach") loop before the condition is False, you can use the break instruction after a conditional "if" inside the loop (break ends execution of the current loop or switch structure).
  - Syntax:
for (init; condition; increment) {
  if(end_condition) break;
  // code to be executed
}
If the end_condition is True, the code after the "break" will not be executed at all, and the execution of "for" loop ends.
  - Example:
<?php
for ($i=0; $i<5; $i++) {
  if ($i==2) break;
  echo '<br/> The counter is: '. $i;
}
?>
- Output:
The counter is: 0
The counter is: 1
- As you can see, eaven the condition ($i<5) is True, the loop ends when $i==2.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
For and Foreach Loops

Last accessed pages

  1. Disable button and Enable it after specified time (17615)
  2. Laravel Basic Architecture (1330)
  3. Select in MySQL, Output results in HTML Table (19579)
  4. Node.js Course (2884)
  5. Vue JS - Events (238)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (535)
  2. CSS cursor property - Custom Cursors (74)
  3. Read Excel file data in PHP - PhpExcelReader (50)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (46)
  5. PHP Unzipper - Extract Zip, Rar Archives (46)