You are on page 1of 26

Week 006

PHP CONSTRUCTS
Objectives
• Understand the concept of loops and apply it in PHP codes.
• Apply the syntax of the following loops.
• FOR Loop
• WHILE Loop
• DO WHILE Loop
• FOR EACH Loop
• Understand the concept of reading, writing files in PHP.
• Display data into a table using file handling.
PHP loops
• PHP for loops execute a block of code a specified number of time.
• The PHP for Loop
• The for loop is used when you know in advance how many times
the script should run.
• Syntax
• for (init counter; test counter; increment counter) {
code to be executed;
}
PHP loops
• Parameters:
• init counter: Initialize the loop counter value
• test counter: Evaluated for each loop iteration. If it
evaluates to TRUE, the loop continues. If it evaluates to
FALSE, the loop ends.
• increment counter: Increases the loop counter value
For Loop

• The example below displays the numbers from 0 to 10:


• <?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
For Loop

• For every loop iteration, the value of the current array element is
assigned to $value and the array pointer is moved by one, until it
reaches the last array element. The following example demonstrates a
loop that will output the values of the given array ($colors):
• <?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
The PHP while Loop
• PHP while loops execute a block of code while the specified condition is true.
• The while loop executes a block of code as long as the specified condition is true.

• Syntax
• while (condition is true) {
code to be executed;
}
• The example below first sets a variable $x to 1 ($x = 1). Then, the while loop
will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will
increase by 1 each time the loop runs ($x++):
• <?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
The PHP do...while Loop
• The do...while loop will always execute the block of code once, it will then
check the condition, and repeat the loop while the specified condition is true.
• Syntax
• do {
code to be executed;
} while (condition is true);
• The example below first sets a variable $x to 1 ($x = 1). Then, the do while
loop will write some output, and then increment the variable $x with 1. Then
the condition is checked (is $x less than, or equal to 5?), and the loop will
continue to run as long as $x is less than, or equal to 5:
• <?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
The PHP foreach Loop
• The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.
• Syntax
• foreach ($array as $value) {
code to be executed;
}
• For every loop iteration, the value of the current array element is assigned to
$value and the array pointer is moved by one, until it reaches the last array
element.
• The following example demonstrates a loop that will output the values of the
given array ($colors):
• <?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
For loop application
For loop application
32for_loop.html 32for_loop.php
While loop application
While loop application
33while_loop.html 33while_loop.php
While loop application

33while_loop.php
Reading Files

• PHP Open File - fopen()


• A better method to open files is with the fopen() function. The
fopen() function opens a file or URL. If fopen() fails, it returns
FALSE and an error on failure. You can hide the error output by
adding an '@' in front of the function name.
Reading Files
Create a file with the filename webdictionary.txt
Type the following items in the text file.

AJAX = Asynchronous JavaScript and XML


CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
Reading Files
<?php

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");


echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);

?>
Modes
Reading a file
34reading_file.php 34reading_file.php
Writing data into a file
Writing data into a file
35write_file.html 35write_file.php
Writing data into a file

35write_file.php
Displaying data into a table with file handling
Displaying data into a table with file handling
36display_data_in_a_table.html 36display_data_in_a_table.php
Displaying data into a table with file handling
36display_data_in_a_table.php
Machine Problem
• Continue with the previous machine
• The application should read the cities from a file that is displayed in
the guestbook
• Add code to the add_to_guestbook.php to append a single line of data
to guestbook.txt
• Create a new program, view_guestbook.php that will display all of the
names in the guestbook.txt file in an HTML table. Make sure the table
rowshave different alternate background colors.
• Add a link to the file that will open a view_guestbook.php in a new
window or tab.
• You will not change the Mortgage Calculator

You might also like