
- •Initializing with Constructor Functions . . . . .
- •Into a Web page as a separate section. Although JavaScript code can
- •Is that standard php script delimiters are guaranteed to be available
- •In the block. Any text or lines between the opening /* characters and
- •2.7541 Are not integers; they are floating-point numbers. A floating-
- •Value 300
- •Is a value of 2.5, because 6 goes into 15 exactly 2.5 times. But if you
- •IsEven.Php.
- •Ing example,
- •Ing curly brace is on its own line following the function statements.
- •In php 3 and earlier, it was necessary to put a function definition
- •Is called an iteration. When the conditional expression evaluates
- •Including Files
- •13. Close your Web browser window.
- •Including Files
- •In php, you can also use two operators to combine strings. The first
- •Xhtml source code gen-
- •Input. Php provides several functions for manipulating the case of a
- •Is uppercase. If you need the reverse of ucfirst(), the lcfirst()
- •In some situations, you will need to find and extract characters and
- •Information Interchange, or ascii, which are numeric represen-
- •In comparison, the following preg_match() function returns a value
- •In the pattern is optional. The following code demonstrates how to
- •Values; any strings you validate against a regular expression must
- •Value of 1 because the top-level domain contains a valid value of .Com.
- •Is submitted using the “post” method, the form data is embedded in
- •Validating String Data
- •Xhtml tags or character entities. The message field is a text string
- •Value of the header element. For example:
- •Xhtml code within a php script section.
- •Is typically the person who created the resource. Otherwise, the net-
- •If even a single character of the Web page is sent prior to sending
- •Variables to the file_put_contents() function.
- •Xhtml hyperlink. To download a file from outside the xhtml
- •If...Else statement to display the appropriate version of the mes-
- •Iterating Through an Array
- •Iterating Through an Array
- •In Chapter 2, you learned how to use a foreach statement to iterate
- •Iterating Through an Array
- •Iterating Through an Array
- •In comparison, the following code declares and initializes
- •If ((!file_exists("MessageBoard/messages.Txt"))
- •Values from the array to create a thumbnail gallery of images in which
- •Introduction to Databases
- •Including php, allow you to create Web pages that can read and write
- •Introduction to Databases
- •Information that can be organized into ordered sets of data, and
- •Information. Each recipe in a recipe database, for instance, is a single
- •Introduction to Databases
- •Index, which identifies records in a database to make retrievals and
- •In a single table. However, you might want to break the information
- •Into multiple tables to better organize it into logical sets. Another
- •Information in one of the tables confidential and accessible only by
- •Is the employee information table from Figure 7-1. The related table
- •Is a payroll table that contains confidential salary and compensation
- •Information. Notice that each table contains an identical number of
- •Introduction to Databases
- •Introduction to Databases
- •In a junction
- •Introduction to Databases
- •In a relational format is called a relational database management
- •Is a standard data manipulation language among many dbmSs.
- •Into the query area at the top of the screen or by dragging tables and
- •It is important to understand that even though many dbmSs sup-
- •Introduction to Databases
- •If you ever
- •Is. In comparison, the bigint data type stores integer values between
- •5 Rows in set (0.00 sec)
- •Int);[enter ]
- •Important, these two tabs can cause you to lose all of the data in the
- •Internet Explorer to export the table, click the Save button in the File
- •Ifies the table being changed and the change to make.
- •It easier for you to write php code that can be used with a variety of
- •Information about queries that match one of the following formats:
- •Various types of actions, depending on the type of query.
- •Include fields for the date and time of the flight, flight number, and
- •In the ChineseZodiac folder and upload the file to the server. Open
- •Including white space,
- •Information on a Web server. When you start a new session, the
- •Introduction to Object-Oriented Programming
- •Introduction to Object-Oriented
- •Variables associated with an object are called properties or attributes.
- •In the Loan object example, a function that calculates the number of
- •Introduction to Object-Oriented Programming
- •Introduction to Object-Oriented Programming
- •Include instances of objects inherit the object’s functionality.
- •In this chapter, you will create the Web site for an online order form
- •In an online store application. The application includes information
- •Ity of building a working online store. Online store classes are very
- •Information and products. The OnlineStore class requires that store
- •Information is stored in a table containing six fields: storeId, name,
- •Information. Instead, the class simply uses session iDs to keep track
- •Variable and function as necessary, without bothering with all this
- •In a class
- •Is developed. Imagine what would happen if Microsoft distributed
- •Ing class is invalid because it does not include an access specifier:
- •If they will not be supported by future xhtml versions or are not
- •Xhtml standards. To review the guide of current w3c css specifi-
- •Information to remind yourself or others of what the code is doing. A
- •Xhtml document to the external style sheet. This link informa-
- •If you select Apache from the WampServer menu and select Service
- •Ing code uses the number_format() function to add comma separa-
- •In data that a user submits to a php script.
- •Value of “On” and the display_startup_errors directive is assigned
- •Instead. By looking at the source code, you could see that the value of
- •Ing engine can even help locate logic errors.
- •In Chapter 8, along with the equivalent mssql_* functions, where
- •Inline styles, 632
- •Xhtml, 620–635 (continued)
The conditional expression in the while statement is enclosed within
parentheses following the keyword while. As long as the conditional
expression evaluates to TRUE, the statement or command block that
follows executes repeatedly. Each repetition of a looping statement
Is called an iteration. When the conditional expression evaluates
to FALSE, the loop ends and the next statement following the while
statement executes.
A while statement keeps repeating until its conditional expression
evaluates to FALSE. To ensure that the while statement ends after
performing the desired tasks, you must include code that tracks the
progress of the loop and changes the value produced by the condi-
tional expression. You can track the progress of a while statement,
or any other loop, with a counter. A counter is a variable that incre-
ments or decrements with each iteration of a loop statement.
The following code shows a simple script that includes a while state-
ment. The script declares a variable named $Count and assigns it an
initial value of 1. The $Count variable is then used in the while state-
ment conditional expression ($Count <= 5). As long as the $Count
variable is less than or equal to 5, the while statement loops. Within
the body of the while statement, the echo statement displays the
value of the $Count variable, then the $Count variable increments by a
value of 1. The while statement loops until the $Count variable incre-
ments to a value of 6.

Repeating
Code
$Count
= 1;
while
($Count <= 5) {
echo
"$Count<br />";
++$Count;
}
echo "<p>You have displayed 5 numbers.</p>";
The preceding code displays the numbers 1 to 5, with each number
representing one iteration of the loop. When the counter reaches 6,
the message “You have displayed 5 numbers.” appears, thus demon-
strating that the loop has ended. Figure 2-5 shows the output of this
simple script.
97
Figure 2-5
Output of a while statement using an increment operator
You can also control the repetitions in a while loop by decrementing
(decreasing the value of ) counter variables. Consider the following
script:
$Count = 10;
while ($Count > 0) {
echo "$Count<br />";
--$Count;
}
echo "<p>We have liftoff.</p>";
In this example, the initial value of the $Count variable is 10, and the
decrement operator (--) is used to decrease the value of the $Count
variable by 1. When the $Count variable is greater than zero, the
statement within the while loop displays the value of the $Count vari-
able. When the value of $Count is equal to zero, the while loop ends,
and the statement immediately following it displays. Figure 2-6 shows
the script output.

CHAPTER
2
Functions
and Control Structures
98
Figure
2-6
Output
of a while
statement
using a decrement operator
There
are many ways to change the value of a counter variable and to
use
a counter variable to control the repetitions of a while
loop.
The
following
example uses the *=
assignment
operator to multiply the
value
of the $Count
variable
by 2. When the $Count
variable
reaches a
value
of 128 (the first multiple of 2 greater than 100), the while
state-
ment
ends. Figure 2-7 shows the script output.
$Count
= 1;
while
($Count <= 100) {
echo
"$Count<br />";
$Count *= 2;
}
Figure 2-7
Output of a while statement using the assignment operator *=

Repeating
Code
To
ensure that the while
statement
will eventually end, you must
include
code within the body of the while
statement
that changes
the
value of the conditional expression. For example, you may have a
while
statement
that displays even numbers between 0 and 100. You
need
to include code within the body of the while
statement
that
ends
the loop after the last even number (100) displays. If you do not
include
code that changes the value used by the conditional expres-
sion,
your program will be caught in an infinite loop. In an infinite
loop,
a loop statement never ends because its conditional expression
is
never FALSE.
Consider the following while
statement:
$Count
= 1;
while
($Count <= 10) {
echo
"The number is $Count";
}
99
Although the while statement in the preceding example includes a
conditional expression that checks the value of a $Count variable,
there is no code within the while statement body that changes the
$Count variable value. The $Count variable will continue to have a
value of 1 through each iteration of the loop. This means that the text
string “The number is 1” will be displayed repeatedly until the user
closes the Web browser window.
To modify the DiceRoll.php script to evaluate five rolls using a while
statement:
1.
2.
You can
use the
continue
statement to
halt a looping
statement and restart the
loop with a new iteration.
Return to the DiceRoll.php document in your text editor.
Immediately after the declaration of the $Dice array, declare
and initialize two new variables: $DoublesCount and
$RollNumber.
$DoublesCount = 0;
$RollNumber = 1;
3.
After the new variable declarations, create a while loop by
adding the code shown in bold. Also, revise the echo state-
ment by making the change shown in bold.
while ($RollNumber <= 5) {
$Dice[0] = rand(1,6);
$Dice[1] = rand(1,6);
$Score = $Dice[0] + $Dice[1];
echo "<p>";
echo "The total score for roll $RollNumber was
$Score.<br />";
$Doubles = CheckForDoubles($Dice[0],$Dice[1]);
DisplayScoreText($Score, $Doubles);
echo "</p>";
if ($Doubles)
++$DoublesCount;
++$RollNumber;
} // End of the while loop

CHAPTER
2
Functions
and Control Structures
4.
Add
the following line after the while
loop
to display the
number
of times doubles were rolled:
echo
"<p>Doubles occurred on $DoublesCount of the
five
rolls.</p>";
5.
100
6.
Save
and upload the DiceRoll.php document.
Open
the DiceRoll.php file in your Web browser by enter-
ing
the following URL: http://<yourserver>/PHP_Projects/
Chapter.02/Chapter/DiceRoll.php.
Figure 2-8 shows how the
program
appears in a Web browser.
Figure
2-8
Output
of DiceRoll.php after adding a while
statement
7.
Close
your Web browser window.
do
. . . while Statements
Another
PHP looping statement, similar to the while
statement,
is
the
do
. . . while statement.
The do
. . . while statement
executes a
statement
or statements once, then repeats the execution as long as
a
given conditional expression evaluates to TRUE.
The syntax for the
do
. . . while statement
is as follows:
Repeating
Code
do
{
statement(s);
} while (conditional expression);
As you can see in the syntax description, the statements execute
before a conditional expression is evaluated. Unlike the simpler while
statement, the statements in a do . . . while statement always execute
once before a conditional expression is evaluated.
The following do . . . while statement executes once before the con-
ditional expression evaluates the count variable. Therefore, a single
line that reads “The count is equal to 2” appears. After the conditional
expression ($Count < 2) executes, the $Count variable is equal to 2.
This causes the conditional expression to return a value of FALSE, and
the do . . . while statement ends.
$Count = 2;
do {
echo "<p>The count is equal to $Count</p>";
++$Count;
} while ($Count < 2);
101
Note that the preceding example includes a counter within the body
of the do . . . while statement. As with the while statement, you need
to include code that changes the conditional expression to prevent an
infinite loop.
In the following example, the while statement never executes
because the count variable does not fall within the range of the con-
ditional expression:
$Count = 2;
while ($Count < 2) {
echo "<p>The count is equal to $Count</p>";
++$Count;
}
The following script shows an example of a do . . . while statement
that displays the days of the week, using an array:
$DaysOfWeek = array("Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday");
$Count = 0;
do {
echo $DaysOfWeek[$Count], "<br />";
++$Count;
} while ($Count < 7);
In the preceding example, an array is created containing the days of
the week. A variable named $Count is declared and initialized to zero.
(Remember, the first subscript or index in an array is zero.) Therefore,
in the example, the statement $DaysOfWeek[0]; refers to Monday.
The first iteration of the do . . . while statement displays “Monday”

CHAPTER
2
Functions
and Control Structures
102
and
then increments the count
variable
by 1. The conditional expres-
sion
in the while
statement
then checks to determine when the last
element
of the array has been displayed. As long as the count is less
than
seven (which is one number higher than the index of the largest
element
in the $DaysOfWeek[]
array),
the loop continues. Figure 2-9
shows
the output of the script in a Web browser.
Figure
2-9
Output
of days of week script in a Web browser
Next,
you will replace the while
statement
in the DiceRoll.php script
with
a do
. . . while statement.
Because the two types of statements
are
so similar, there is little benefit in replacing the while
statement.
You
will add a do
. . . while statement
to the script for practice.
To
use a do
. . . while statement:
1.
2.
Return
to the DiceRoll.php document in your text editor.
Change
the while
statement
to a do
. . . while statement,
as
follows:
do
{
$Dice[0]
= rand(1,6);
$Dice[1] = rand(1,6);
$Score = $Dice[0] + $Dice[1];
echo "<p>";
echo "The total score for roll $RollNumber was
$Score.<br />";
$Doubles = CheckForDoubles($Dice[0],$Dice[1]);
DisplayScoreText($Score, $Doubles);
echo "</p>";
if ($Doubles)
++$DoublesCount;
++$RollNumber;
} while ($RollNumber <= 5); /* End of the do . . .
while loop */
Repeating
Code
3.
4.
Save
and upload the DiceRoll.php document.
Open
the DiceRoll.php file in your Web browser by enter-
ing
the following URL: http://<yourserver>/PHP_Projects/
Chapter.02/Chapter/DiceRoll.php.
The output should still
appear
as shown in Figure 2-8.
Close
your Web browser window.
103
5.
for
Statements
So
far, you have learned how to use the while
and
the do
. . . while
statements
to repeat, or loop through, code. You can also use the
for
statement
to loop through code. Specifically, the for
statement
is
used for repeating a statement or series of statements as long as a
given
conditional expression evaluates to TRUE.
The for
statement
performs
essentially the same function as the while
statement:
if a
conditional
expression within the for
statement
evaluates to TRUE,
the
for
statement
executes and continues to execute repeatedly until
the
conditional expression evaluates to FALSE.
A
primary difference between while
and
for
statements
is that,
in
addition to a conditional expression, the for
statement
can also
include
code that initializes a counter and changes its value with each
iteration.
This is useful because it provides a specific place for you to
declare
and initialize a counter, and to update its value, which helps
prevent
infinite loops. The syntax of the for
statement
is as follows:
for
(counter declaration and initialization; condition;
update
statement) {
statement(s);
}
When the PHP interpreter encounters a for loop, the following steps
occur:
1.
The counter variable is declared and initialized. For example,
if the initialization expression in a for loop is $Count = 1;,
a variable named $Count is declared and assigned an initial
value of 1. The initialization expression is only started once,
when the for loop is first encountered.
The for loop condition is evaluated.
If the condition evaluation in Step 2 returns a value of TRUE,
the for loop statements execute, Step 4 occurs, and the pro-
cess starts over again with Step 2. If the condition evaluation
in Step 2 returns a value of FALSE, the for statement ends and
the next statement following the for statement executes.
2.
3.

CHAPTER
2
You
can omit
any of the
three parts of
the for state-
ment, but you
must include the semico-
lons that separate each
section. If you omit a
section, be sure you
include code within the
body that will end the
for statement, or your
program might get
caught in an infinite loop.
Functions and Control Structures
4.
The update statement in the for statement is executed. For
example, the $Count variable may increment by 1.
The following script shows a for statement that displays the contents
of an array:
$FastFoods = array("pizza", "burgers", "french fries",
"tacos", "fried chicken");
for ($Count = 0; $Count < 5; ++$Count) {
echo $FastFoods[$Count], "<br />";
}
104
As you can see in this example, the counter is initialized, evaluated,
and incremented within the parentheses. You do not need to include
a declaration for the $Count variable before the for statement, nor do
you need to increment the $Count variable within the body of the for
statement. Figure 2-10 shows the output of the fast foods script.
Figure 2-10
Output of the fast foods script
Using a for statement is more efficient because you do not need as
many lines of code. Consider the following while statement:
$Count = 1;
while ($Count <= 5) {
echo "$Count<br />";
++$Count;
}
You could achieve the same flow control more efficiently by using a
for statement as follows:
for ($Count = 1; $Count <= 5; ++$Count) {
echo "$Count<br />";
}
The following code shows an example of the “days of the week”
script you saw earlier. This time, however, the script includes a
Repeating
Code
for
statement
instead of a do
. . . while statement.
Notice that the
declaration
of the $Count
variable,
the conditional expression, and
the
statement that increments the $Count
variable
are now all con-
tained
within the for
statement.
Using a for
statement
instead of a
do
. . . while statement
simplifies the script somewhat because you
do
not need as many lines of code.
$DaysOfWeek
= array("Monday", "Tuesday", "Wednesday",
"Thursday",
"Friday", "Saturday", "Sunday");
for
($Count = 0; $Count < 7; ++$Count) {
echo
$DaysOfWeek[$Count], "<br />";
}
105
To replace the do . . . while statement in DiceRoll.php with a for
statement:
1.
2.
Return to the DiceRoll.php document in your text editor.
Change the do . . . while statement to a for statement, as
follows:
for ($RollNumber = 1; $RollNumber <= 5;
++$RollNumber) {
$Dice[0] = rand(1,6);
$Dice[1] = rand(1,6);
$Score = $Dice[0] + $Dice[1];
echo "<p>";
echo "The total score for roll $RollNumber was
$Score.<br />";
$Doubles = CheckForDoubles($Dice[0],$Dice[1]);
DisplayScoreText($Score, $Doubles);
echo "</p>";
if ($Doubles)
++$DoublesCount;
} // End of the for loop
3.
4.
Save and upload the DiceRoll.php document.
Open the DiceRoll.php file in your Web browser by enter-
ing the following URL: http://<yourserver>/PHP_Projects/
Chapter.02/Chapter/DiceRoll.php. The output should still
appear as shown in Figure 2-8.
Close your Web browser window.
5.
foreach Statements
The foreach statement is used to iterate or loop through the ele-
ments in an array. With each loop, a foreach statement moves to the
next element in an array. Unlike other types of looping statements,
you do not need to include any sort of counter within a foreach
statement. Instead, you specify an array expression within a set of

CHAPTER
2
Functions
and Control Structures
parentheses
following the foreach
keyword.
The basic syntax for the
foreach
statement
is as follows:
foreach
($array_name as $variable_name) {
statement(s);
}
106
During
each iteration, a foreach
statement
assigns the value of the
current
array element to the $variable_name
argument
specified in
the
array expression. You use the $variable_name
argument
to access
the
value of the element that is available in an iteration. For example,
the
following code declares the same $DaysOfWeek[]
array
you’ve
seen
a few times in this chapter. During each iteration, the expression
in
the foreach
statement
assigns the value of each array element to
the
$Day
variable.
An echo
statement
within the foreach
statement’s
braces
displays the value of the current element.
You
will
receive an
error if you
attempt to
use a
foreach statement with
any variable types other
than arrays.
$DaysOfWeek = array("Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday");
foreach ($DaysOfWeek as $Day) {
echo "<p>$Day</p>";
}
The foreach statement in the preceding code simply displays the days
of the week to the Web browser.
The more advanced form of the foreach statement allows you to
retrieve both the index (or key) and the value of each array element.
foreach ($array_name as $index_name => $variable_name) {
statement(s);
}
This form of the foreach statement works almost exactly the same
as the basic form. The only difference is that the index of the current
array element is stored in the $index_name variable. For example, the
following code declares the $DaysOfWeek[] array again. During each
iteration, the expression in the foreach statement assigns the index
value of each array element to the $DayNumber variable and the value
of each array element to the $Day variable. An echo statement within
the foreach statement’s braces displays the index and value of the
current element. Figure 2-11 shows the output of this version.
$DaysOfWeek = array("Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday");
foreach ($DaysOfWeek as $DayNumber => $Day) {
echo "<p>Day $DayNumber is $Day</p>";
}

Repeating
Code
107
Figure
2-11
Output
of the foreach
script
with index values
To
create a final version of DiceRoll.php that displays all possible
out-
comes
of rolling two dice:
1.
2.
Return
to the DiceRoll.php document in your text editor.
Immediately
after the declaration of the $FaceNamesSingular
and
$FaceNamesPlural
arrays,
declare a new array named
$FaceValues,
as follows:
$FaceValues
= array( 1, 2, 3, 4, 5, 6);
3.
Delete
the declaration of the $Dice
array
and add a new dec-
laration
for a variable named $RollCount,
as follows:
$RollCount
= 0;
4.
Create
a new array called $ScoreCount
and
initialize it using
the
following for
loop:
$ScoreCount
= array();
for
($PossibleRolls = 2; $PossibleRolls <= 12;
++$PossibleRolls)
{
$ScoreCount[$PossibleRolls]
= 0;
}
CHAPTER
2
Functions
and Control Structures
5.
Replace
the for
statement
with the following two nested
foreach
statements:
foreach
($FaceValues as $Die1) {
foreach
($FaceValues as $Die2) {
6.
108
Delete the two calls to rand(1,6) and the $Score variable
assignment, and insert the following lines in their place:
++$RollCount;
$Score = $Die1 + $Die2;
++$ScoreCount[$Score];
7.
Modify the call to the CheckForDoubles() function to use
$Die1 and $Die2 instead of $Die[0] and $Die[1].
$Doubles = CheckForDoubles($Die1,$Die2);
8.
Replace the single closing brace for the for loop with two
closing braces for the two foreach loops:
} // End of the foreach loop for $Die2
} // End of the foreach loop for $Die1
9.
Modify the echo statement that displays the doubles count
with an echo statement that displays the doubles count and
the roll count.
echo "<p>Doubles occurred on $DoublesCount of the
$RollCount rolls.</p>";
10. Finally, add a foreach loop to display the number of times
each score occurred. Use the second form of the foreach
statement to get the array index, which is the score.
foreach ($ScoreCount as $ScoreValue => $ScoreCount) {
echo "<p>A combined value of $ScoreValue
occurred $ScoreCount of $RollCount
times.</p>";
}
11. Save and upload the DiceRoll.php document.
12. Open the DiceRoll.php file in your Web browser by enter-
ing the following URL: http://<yourserver>/PHP_Projects/
Chapter.02/Chapter/DiceRoll.php. The output should appear
as shown in Figure 2-12.
