Php-mysql Course

Loops are used when you want to execute a block of code a specified number of times, or while a specified condition is True.

While loop

A while loop runs code repeatedly as long as a condition remains TRUE.
The while statement includes the condition in parentheses, and it is followed by a block of statements within braces.
  - Syntax:
while (condition) {
  // code to be executed
}
If the condition starts out as false, the statements won’t execute at all.
  - Example:
<?php
$i = 0;
while ($i<5) {
  echo '<br /> i = '. $i;
  $i++;
}
?>
- First it's declared a variable "$i" with the value of 0. The "while" statement checks the condition ($i<5), which it's True and permits the execution of block code inside the brackets. The "$i++" increments the value of "$i" by 1 each time the loop runs, and check again the condition. The loop will stop when the "$i" reach 5.
- This script displays the following results:
i = 0
i = 1
i = 2
i = 3
i = 4

do ... while loop

The do...while loop is a variant of the "while" loop. The major difference between these two loops is that the "do while" construct will execute the code at least once, then checks the condition.
  - Syntax:
do {
  // code to be executed
}
while (condition)
First will be executed the block of code, then checks the condition, and it will repeat the loop as long as the specified condition is TRUE.
This loop will always be executed at least once.
  - Example:
<?php
$x = 8;
do {
  echo '<br /> x = '. $x;
  $x++;
}
while ($x<5)
?>
- This example display "x = 8".
As you can notice, although the condition is False ($x<5), the code within braces is still executed once.

End the While loops with the break instruction

If you want to end a "while" (or "do while") 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:
while (condition) {
  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 "while" loop ends.
  - Example:
<?php
$i = 0;
while ($i<5) {
  if ($i==2) break;
  echo '<br /> i = '. $i;
  $i++;
}
?>
- Output:
i = 0
i = 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 HTML element can be used to embed a SWF flash content?
<object> <div> <script>
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250">
 <param name="src" value="file.swf" />
 Your browser not support SWF.
</object>
Which CSS pseudo-class adds a style to an input form field that has keyboard input focus?
:active :focus :hover
input:focus {
  background-color: #88fe88;
}
Click on the instruction which converts a JSON string into a JavaScript object.
JSON.stringify(javascript_object) object.toString() JSON.parse(json_string)
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}';
var obj = JSON.parse(jsnstr);
alert(obj.url);
Indicate the PHP function which can be used to create or write a file on server.
fopen() file_put_contents() file_get_contents()
if (file_put_contents("file.txt", "content")) echo "The file was created";
else echo "The file can not be created";
While Loops

Last accessed pages

  1. Using v-model in form input fields (1051)
  2. jQuery UI draggable - Drag elements (11445)
  3. Display data from PHP Array, or MySQL in HTML table (26980)
  4. Redirects (4978)
  5. jsSHA - SHA Hashes and HMAC in JavaScript (3519)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (310)
  2. The Mastery of Love (48)
  3. CSS cursor property - Custom Cursors (36)
  4. Read Excel file data in PHP - PhpExcelReader (35)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (31)