Luigi Arlotta, in his third article continues
with his introduction to PHP programming. In this article Luigi
explains one more conditional statement called the while
loop. He then proceeds and explains one of the most important
constructs of any programming language - functions.
while
Loops - An Important Conditional Statement
A
while loop is a little less sophisticated than a for
loop, but it approximately carries out the same function. It
is composed of a body containing some instructions and an exit
condition. At the beginning of the cycle and each successive
iteration, all the instructions in the body are executed, the
exit condition consisting of a boolean expression, is checked
up. The loop ends when the exit condition returns a false value.
The syntax of a while loop is as follows
|
while
(condition)
{
BodyOfLoop;
}
|
The condition is the control condition of the loop. The
first check of the condition's validity takes place at the beginning
of the cycle, before the first iteration. Also in this case,
it can happen that the instructions included in the loop's body
are never executed. This happens when the exit condition immediately
returns a false value. As with the for loops, even with
the while loops there is the danger to create a never
ending loop. This happens when the exit condition never returns
a false value.
The
example below, prints numbers from 1 to 10 (Just like the example
of for loops). I have shown only a part of the program.
A more complete and complex example is shown later.
|
$k
= 1;
while($k <=10){
echo("$k
<BR>");
$k++;
}
|
Observe
that in this case it is necessary to supply to the increment
of the control variable $k, by adding an increase instruction
($k++) in the loop's body. The output of the program is shown
below
I have also taken up the example that displays the cell along
with the line-cell numbers. This time I have generated the same
output using the while loop instead of the for
loop.
<HTML>
<BODY>
<?
$j=1;
echo ("<TABLE ALIGN=CENTER BORDER=1 CELLSPACING=1
CELLPADDING=5 >");
while ($j<=5) {
echo
("<TR>");
$k=1;
while ($k<=3) {
echo
("<TD> Line $j, Cell $k </TD>");
$k++;
}
echo("</TR>");
$j++;
}
echo ("</TABLE>");
?>
</BODY>
</HTML>
|
The
output of this program is similar to the one using the for
loop. The table below is the output.
| Line
1, Cell 1 |
Line
1, Cell 2 |
Line
1, Cell 3 |
| Line
2, Cell 1 |
Line
2, Cell 2 |
Line
2, Cell 3 |
| Line
3, Cell 1 |
Line
3, Cell 2 |
Line
3, Cell 3 |
| Line
4, Cell 1 |
Line
4, Cell 2 |
Line
4, Cell 3 |
| Line
5, Cell 1 |
Line
5, Cell 2 |
Line
5, Cell 3 |
PHP
also provides an alternative syntax for while loops,
too. It is possible to avoid the use of the brackets "{" and
"}" by enclosing the instructions that constitute the body of
the loop with the endwhile keyword. The syntax of a while
loop using the endwhile keyword follows
|
while
(condition)
BodyOfLoop;
endwhile;
|
Just
as in the if-then-else statement, we can set up a while
loop intermingled with HTML code. The syntax is as follows
|
<?
while (condition) : ?>
HTML
instructions
<?
endwhile ?> |
The
table below serves as a quick reference for the syntax of for
loops and while loops. Both these return the same result.
You could use any of them.
|
Comparisons
of for and while loops
|
|
for
Loop
|
while
loop
|
|
for
($i=0; $i<=10; $i++) {
echo("Iteration
no. $i<BR>");
}
|
$i=0;
while ($i<=10) {
echo
("Iteration no. $i<BR>");
$i++;
} |
Functions
Functions
are blocks of instructions used by the main program in order
to achieve its goal. A function generally executes a basic and
complete operation. Many such functions together would carry
out the main objective of the program.
Every function has a name that identifies it. A function can
get as input an arbitrary number of parameters (variables),
which can be used within that function. Also if necessary, an
output value can be returned to the main program from the function.
This value can be saved and used when necessary. Functions are
a basic instrument in every programming language. This allows
software developers to spilt the application's main objective
into many smaller blocks each concerned with a specific job.
This simplifies the application's building, testing, debugging
and editing process. This technique leads to what is called
modular programming since the entire program is divided into
separate modules (functions).
A function is constituted by a head and a body. In PHP, functions
are defined as follows
|
function
FunctionName($param1, $param2, ..., $paramN){
FunctionBody;
} |
FunctionName
is the name of the function. $param1 , $param2
..., $paramN are the N parameters that the function
receives. The parameters sent to a function do not necessarily
have to be primitive data types (integers, char, floats, etc.),
they can be any type of objects defined by the software developer.
Between the brackets ' { ' and ' } ' is the function's body,
containing the instructions to be executed when the function
is called. Once these are executed, a value called function's
output can be returned to the program that called this function.
Now
we want to define a function that displays some text in a HTML
document using a paragraph TAG. This text is passed as parameter.
We can write a function like the following one
|
function
writeString ($str)
{
echo("<FONT
FACE=\"Comic Sans MS\" SIZE=3><P ALIGN=\"JUSTIFY\">$str</P></FONT>");
}
|
To
use the writeString() function, we will write the following
line inside the PHP code
| writeString("Text
to be formatted and displayed by the function"); |
The
PHP code of the page in which the writeString() function
is used is shown below
<HTML>
<BODY>
<?
function writeString ($str)
{
echo("<FONT
SIZE=+1><P ALIGN=\"JUSTIFY\">$str</P></FONT>");
}
writeString ("Text to be formatted and displayed by
the function");
?>
</BODY>
</HTML>
|
The
code will generate the following output
| Text
to be formatted and displayed by the function |
The next example is quite a complex example. The PHP script
consists of a function that creates a beautiful checkered board
using only HTML tags. The number of checkered squares on the
board can be passed to the function as input parameters.
<HTML>
<BODY>
<?
function createBoard ($lines, $cols)
{
$j=1;
echo ("<TABLE ALIGN=CENTER BORDER=1 CELLSPACING=0>");
while ($j<=$lines) {
echo
("<TR>");
$k=1;
while ($k<=$cols) {
if
(($j+$k)%2>0)
echo
("<TD WIDTH=30 HEIGHT=30 BGCOLOR=#000000>
</TD>");
else
echo
("<TD WIDTH=30 HEIGHT=30 BGCOLOR=#FFFFFF>
</TD>");
$k++;
}
echo("</TR>");
$j++;
}
echo ("</TABLE><BR><BR>");
}
createBoard(8,8);
createBoard(4,4);
?>
</BODY>
</HTML>
|
The
output of the program shown above would be the following
The
createBoard(8,8) function call creates the 8x8 checkered board.
And the next line createBoard(4,4) creates the smaller 4x4 checkered
board. The most important part of the program is the nested
for loops (one for loop inside another for
loop). The inner for loop would run complete number of
times for each iteration of the outer for loop. So if the outer
for loop has 10 iterations and the inner for loop
has 5 iterations. And suppose there is an echo() statement
within the inner for loop, this echo() statement
would be called a total of 10x5 times - 50 times.
I hope you liked this article. In my next article I shall explain
the different datatypes in PHP. Also I shall deal with strings
and explain their usage.

|
©
Copyright by Luigi Arlotta. All rights reserved. Contact
the author for permissions.
|