
- •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)
Numeric data types are an important part of any programming lan-
guage and are particularly useful for arithmetic calculations. PHP
supports two numeric data types: integers and floating-point num-
bers. Integers are positive and negative numbers and zero, with no
decimal places. The numbers −250, −13, 0, 2, 6, 10, 100, and 10,000
are examples of integers. The numbers −6.16, −4.4, 3.17, .52, 10.5, and
2.7541 Are not integers; they are floating-point numbers. A floating-
point number contains decimal places or is written in exponential
notation. Exponential notation, or scientific notation, is a short-
ened format for writing very large numbers or numbers with many
decimal places. Numbers written in exponential notation are repre-
sented by a value between −10 and 10 that is multiplied by 10 raised
to some power. The notation for “times ten raised to the power” is an
uppercase or lowercase E. For example, the number 200,000,000,000
can be written in exponential notation as 2.0e11, which means “two
times ten to the power eleven.”

CHAPTER
1
Getting
Started with PHP
To
create a script that assigns integers and exponential numbers to
variables
and displays the values:
1.
2.
32
3.
Create
a new document in your text editor.
Type
the <!DOCTYPE>
declaration,
<html>
element,
header
information,
and <body>
element.
Use the strict DTD and
“Display
Numbers” as the content of the
Add the following standard PHP script delimiters to the docu-
ment body:
<?php
?>
4.
Add the following lines to the script section; they declare an
integer variable and a floating-point variable:
$IntegerVar = 150;
$FloatingPointVar = 3.0e7; // floating-point
// number 30000000
5.
Finally, to display the values of the variables, add the following
statements to the end of the script section:
echo "<p>Integer variable: $IntegerVar<br />";
echo "Floating-point variable: $FloatingPointVar</p>";
6.
Save the document as DisplayNumbers.php in the Chapter
directory for Chapter 1, upload the document to the Web
server, and validate the document with the W3C XHTML
Validator.
Open the DisplayNumbers.php file in your Web browser
by entering the following URL: http://<yourserver>/
PHP_Projects/Chapter.01/Chapter/DisplayNumbers.php.
The integer 150 and the number 30000000 (for the exponen-
tial expression 3.0e7) should appear in your Web browser
window, as shown in Figure 1-15.
7.
Figure 1-15
Output of DisplayNumbers.php
8.
Close your Web browser window.

Working
with Data Types
Boolean
Values
A
Boolean value is a value of “true” or “false”. (You can also
think of
a
Boolean value as either “yes” or “no”, or “on” or “off
”.) Boolean values
are
most often used for deciding which parts of a program should
execute
and for comparing data. In programming languages other
than
PHP, you can use the integer value 1 to indicate a Boolean value
of
TRUE
and
0 to indicate a Boolean value of FALSE.
In
PHP program-
ming,
however, you can only use the words TRUE
or
FALSE
to
indicate
Boolean
values. PHP then converts the values TRUE
and
FALSE
to
the
integers
1 and 0. For example, when you attempt to use a Boolean
variable
of TRUE
in
a mathematical operation, PHP converts the vari-
able
to an integer value of 1. The following shows a simple example
of
a variable that is assigned the Boolean value of TRUE.
Figure
1-16
shows
this output in a Web browser. Notice that the Boolean value of
TRUE
is
displayed as the integer 1.
$RepeatCustomer
= TRUE;
echo
"<p>Repeat customer: $RepeatCustomer</p>";
33
Figure
1-16
Output
of a Boolean value
Arrays
An
array is a set of data represented by a single variable name. You
can
think of an array as a collection of variables contained within a
single
variable. You use arrays when you want to store groups or lists
of
related information in a single, easily managed location. Lists of
names,
courses, test scores, and prices are typically stored in arrays.
Figure
1-17 conceptually shows how you can store the names of the
Canadian
provinces using a single array named $Provinces[].
Array
names
are often referred to with the array operators ([
and
])
at the
end
of the name to clearly define them as arrays. You can use the
array
to refer to each province without having to retype the names
and
possibly introduce syntax errors through misspellings.
CHAPTER
1
The
identi-
fiers you use
for an array
name must
follow the
same rules
as identifiers for vari-
ables: Array names must
begin with a dollar sign,
can include uppercase
and lowercase letters,
can include numbers or
underscores (but not as
the first character after
the dollar sign), cannot
include spaces, and are
case sensitive.
Getting Started with PHP
34
Figure 1-17
Conceptual example of an array
Declaring and Initializing Indexed Arrays
In PHP, you can create numerically indexed arrays and associative
arrays. In this chapter, you will study numerically indexed arrays. You
will learn how to use associative arrays in Chapter 6.
An element refers to a single piece of data that is stored within an
array. By default, the numbering of elements within a PHP array
starts with an index number of zero (0). (This numbering scheme can
be very confusing for beginners.) An index is an element’s numeric
position within the array. You refer to a specific element by enclosing
its index in brackets at the end of the array name. For example, the
first element in the $Provinces[] array is $Provinces[0], the sec-
ond element is $Provinces[1], the third element is $Provinces[2],
and so on. This also means that if you have an array consisting of
10 elements, the 10th element in the array has an index of 9.
You create an array using the array() construct or by using the array
name and brackets. The array() construct uses the following syntax:
$array_name = array(values);
The following code uses the array() construct to create the
$Provinces[] array:
$Provinces = array("Newfoundland and Labrador", "Prince
Edward Island", "Nova Scotia", "New Brunswick", "Quebec",
"Ontario", "Manitoba", "Saskatchewan", "Alberta", "British
Columbia");
The following code shows another example of the preceding array
declaration, but this time with line breaks to make it more readable:
$Provinces = array(
"Newfoundland and Labrador",
"Prince Edward Island",
"Nova Scotia",

Working
with Data Types
"New
Brunswick",
"Quebec",
"Ontario",
"Manitoba",
"Saskatchewan",
"Alberta",
"British
Columbia"
);
Note
that the
final element
in the array
does not have
a comma
following the value.
Inserting a comma after
the final element will
cause a syntax error.
35
To create a script that declares and initializes an array using the
array() construct:
1.
2.
Create a new document in your text editor.
Type the <!DOCTYPE> declaration, <html> element, header
information, and <body> element. Use the strict DTD and
“Central Valley Civic Center” as the content of the
<title>element.
Add the following elements, text, and standard PHP script
delimiters to the document body:
<h1>Central Valley Civic Center</h1>
<h2>Summer Concert Season</h2>
<?php
?>
3.
4.
Add the following lines to the script section to declare and
initialize an array named $Concerts[]:
$Concerts = array("Jimmy Buffett", "Chris Isaak",
"Bonnie Raitt", "James Taylor", "Alicia Keys");
5.
Save the document as Concerts.php in the Chapter directory
for Chapter 1.
You can also use the following syntax to assign values to an array by
using the array name and brackets:
$Provinces[]
$Provinces[]
$Provinces[]
$Provinces[]
$Provinces[]
$Provinces[]
$Provinces[]
$Provinces[]
$Provinces[]
$Provinces[]
=
=
=
=
=
=
=
=
=
=
"Newfoundland and Labrador";
"Prince Edward Island";
"Nova Scotia";
"New Brunswick";
"Quebec";
"Ontario";
"Manitoba";
"Saskatchewan";
"Alberta";
"British Columbia";
Unlike in variables, the preceding statements in arrays do not over-
write the existing values. Instead, each value is assigned to the
$Provinces[] array as a new element using the next consecutive
index number.

CHAPTER
1
Getting
Started with PHP
To
add more elements to an array using statements that include the
array
name and brackets:
1.
2.
36
Return
to the Concerts.php document in your text editor.
Add
the following statements immediately after the statement
containing
the array()
construct:
$Concerts[]
= "Bob Dylan";
$Concerts[]
= "Ryan Cabrera";
3.
Save
the Concerts.php document.
Most
programming languages require that all elements in an array be
of
the exact same data type. However, in PHP, the values assigned to
different
elements of the same array can be of different data types. For
example,
the following code uses the array()
construct
to create an
array
named $HotelReservation,
which
stores values with different
data
types in the array elements:
$HotelReservation
= array(
"Don
Gosselin", // guest name (string)
2,// # of nights (integer)
89.95,// price per night (floating-point)
true);// nonsmoking room (Boolean)
Accessing Element Information
You access an element’s value the same way you access the value of
any variable, except you include brackets and the element index. For
example, the following code displays the value of the second ele-
ment (“Prince Edward Island”) and fifth element (“Quebec”) in the
$Provinces[] array. Figure 1-18 shows the output.
echo "<p>Canada's smallest province is
$Provinces[1].<br />";
echo "Canada's largest province is $Provinces[4].</p>";
Output of elements in the
$Provinces[] array
Figure 1-18
To find the total number of elements in an array, use the count()
function. You pass to the count() function the name of the array

Working
with Data Types
whose
elements you want to count. The following code uses
the
count()
function
to display the number of elements in the
$Provinces[]
array
and the $Territories[]
array.
Figure 1-19
shows
the output.
$Provinces
= array("Newfoundland and Labrador", "Prince
Edward
Island", "Nova Scotia", "New Brunswick",
"Quebec",
"Ontario",
"Manitoba", "Saskatchewan", "Alberta",
"British
Columbia");
$Territories
= array("Nunavut", "Northwest Territories",
"YukonTerritory");
echo
"<p>Canada has ", count($Provinces), "
provinces and ",
count($Territories),
" territories.</p>";
37
Figure 1-19
Output of the count() function
To add statements that use the count() function to display the num-
ber of scheduled concerts and the names of each performer:
1.
2.
Return to the Concerts.php document in your text editor.
Add the following output statements to the end of the code
block, but above the closing ?> delimiter:
echo "<p>The following ", count($Concerts),
" concerts are scheduled:</p><p>";
echo "$Concerts[0]<br />";
echo "$Concerts[1]<br />";
echo "$Concerts[2]<br />";
echo "$Concerts[3]<br />";
echo "$Concerts[4]<br />";
echo "$Concerts[5]<br />";
echo "$Concerts[6]</p>";
3.
Save and upload the Concerts.php document and then vali-
date it with the W3C XHTML Validator.
Open the Concerts.php file in your Web browser by enter-
ing the following URL: http://<yourserver>/PHP_Projects/
Chapter.01/Chapter/Concerts.php. Your Web browser should
appear similar to Figure 1-20.
4.

CHAPTER
1
Getting
Started with PHP
38
Figure
1-20
A
looping
statement
provides a
more efficient
method for
displaying all the elements
of an array. You will learn
about looping statements
in Chapter 2.
Output of Concerts.php
5. Close your Web browser window.
PHP includes the print_r(), var_export(), and var_dump() func-
tions, which you can use to display or return information about
variables. These functions are most useful with arrays because they
display the index and value of each element. You pass to each func-
tion the name of an array (or other type of variable). The following
print_r() function displays the index and values of each element
in the $Provinces[] array. Figure 1-21 shows the output. Notice in
the figure that the 10 Canadian provinces are assigned to elements 0
through 9 in the $Provinces[] array.
print_r($Provinces);
Figure 1-21 Output of the $Provinces[]
array with the print_r() function
Working
with Data Types
The
print_r()
function
does not include any XHTML formatting
tags,
so the array elements are displayed as a continuous string of
text.
To display the array elements on individual lines instead, place
the
print_r()
function
between echo
statements
for opening and
closing
XHTML <pre>
tags.
echo
"<pre>";
print_r($Provinces);
echo
"</pre>";
39
Modifying
Elements
You
modify values in existing array elements in the same fashion as
you
modify values in a standard variable, except that you include
the
index for an individual element of the array. The following
code
assigns values to the first three elements in an array named
$HospitalDepts[]:
$HospitalDepts
= array(
"Anesthesia",//
first element (0)
"Molecular Biology", // second element (1)
"Neurology");// third element (2)
After you have assigned a value to an array element, you can change it
later, just as you can change other variables in a script. To change the
first array element in the $HospitalDepts[] array from “Anesthesia”
to “Anesthesiology,” you use the following statement:
$HospitalDepts[0] = "Anesthesiology";
To modify the second and third elements in the $Concerts[] array
from Bonnie Raitt and James Taylor to Joe Cocker and Van Morrison:
1.
2.
Return to the Concerts.php document in your text editor.
Add the following statements above the first echo statement:
$Concerts[2] = "Joe Cocker";
$Concerts[3] = "Van Morrison";
3.
Save and upload the Concerts.php document and then vali-
date it with the W3C XHTML Validator.
Open the Concerts.php file in your Web browser by entering
the following URL: http://<yourserver>/PHP_Projects/Chap-
ter.01/Chapter/Concerts.php. The concert list should include
Joe Cocker and Van Morrison instead of Bonnie Raitt and
James Taylor.
Close your Web browser window.
4.
5.

CHAPTER
1
Getting
Started with PHP
Avoiding
Assignment Notation Pitfalls
In
this section, you have learned three different assignment syntaxes.
Each
does something completely different, and it is easy to get them
confused.
40
This
statement assigns the string “Hello” to a variable named $list.
$list
= "Hello";
This
statement assigns the string “Hello” to a new element appended
to
the end of the $list
array.
$list[]
= "Hello";
This
statement replaces the value stored in the first element (index 0)
of
the $list
array
with the string “Hello”.
$list[0]
= "Hello";
Short
Quiz
1.
Explain
why you do not need to assign a specific data type to
a
variable when it is declared.
Positive
and negative numbers and 0 with no decimal places
belong
to which data type?
Explain
how you access the value of the second element in an
array
named $signs.
What
function can be used to determine the total number of
elements
in an array?
Illustrate
the value of using the print_r()
function
to return
information
about an array variable.
2.
3.
4.
5.
Building
Expressions
Variables
and data become most useful when you use them in an
expression.
An expression is a literal value or variable (or a combina-
tion
of literal values, variables, operators, and other expressions) that
can
be evaluated by the PHP scripting engine to produce a result. You
use
operands and operators to create expressions in PHP. Operands
are
variables and literals contained in an expression. A literal is a
static
value such as a string or a number. Operators are symbols,
Building
Expressions
such
as the addition operator (+)
and multiplication operator (*),
which
are used in expressions to manipulate operands. You have
worked
with several simple expressions so far that combine operators
and
operands. Consider the following statement:
$MyNumber
= 100;
This
statement is an expression that results in the literal value 100
being
assigned to $MyNumber.
The operands in the expression are the
$MyNumber
variable
name and the integer value 100. The operator is
the
equal sign (=).
The equal sign is a special kind of operator, called
an
assignment operator, because it assigns the value 100 on the right
side
of the expression to the variable ($MyNumber)
on the left side of
the
expression. Table 1-2 lists the main types of PHP operators. You
will
learn more about specific operators in the following sections.
Type
Array
Arithmetic
Assignment
Comparison
Logical
Special
String
Table
1-2
Description
Performs
operations on arrays
Performs
mathematical calculations
Assigns
values to variables
Compares
operands and returns a Boolean value
Performs
Boolean operations on Boolean operands
Performs
various tasks; these operators do not fit within
other
operator categories
Performs
operations on strings
PHP
operator types
41
This
is not a
comprehen-
sive list of all
supported
PHP operator
types. Several complex
operator types are
beyond the scope of this
book and are not included
in this list.
You study
string
operators in
Chapter 3
and arrays in
Chapter 6.
PHP operators are binary or unary. A binary operator requires an
operand before and after the operator. The equal sign in the statement
$MyNumber = 100; is an example of a binary operator. A unary oper-
ator requires a single operand either before or after the operator. For
example, the increment operator (++), an arithmetic operator, is used
for increasing an operand by a value of 1. The statement $MyNumber++;
changes the value of the preceding $MyNumber variable to 101.
Next, you will learn more about the different types of PHP operators.
The operand
to the left of
an operator is
known as the
left operand,
and the operand to the
right of an operator is
known as the right
operand.
Arithmetic Operators
Arithmetic operators are used in PHP to perform mathematical cal-
culations, such as addition, subtraction, multiplication, and division.
You can also use an arithmetic operator to return the modulus of a
calculation, which is the remainder left when you divide one number
by another number.
