Solution Manual for PHP Programming with MySQL The Web Technologies Series, 2nd Editioninstant download
Solution Manual for PHP Programming with MySQL The Web Technologies Series, 2nd Editioninstant download
http://testbankbell.com/product/solution-manual-for-php-
programming-with-mysql-the-web-technologies-series-2nd-edition/
http://testbankbell.com/product/test-bank-for-php-programming-with-
mysql-the-web-technologies-series-2nd-edition/
http://testbankbell.com/product/solution-manual-for-introduction-to-
javascript-programming-with-xml-and-php-0133068307/
http://testbankbell.com/product/test-bank-for-introduction-to-
javascript-programming-with-xml-and-php-0133068307/
http://testbankbell.com/product/test-bank-for-fundamentals-of-java-ap-
computer-science-essentials-4th-edition-by-lambert/
Test Bank for The Complete Textbook of Phlebotomy 5th
Edition by Hoeltke
http://testbankbell.com/product/test-bank-for-the-complete-textbook-
of-phlebotomy-5th-edition-by-hoeltke/
http://testbankbell.com/product/test-bank-for-south-western-federal-
taxation-2015-individual-income-taxes-38th-edition/
http://testbankbell.com/product/cases-in-finance-3rd-edition-demello-
solutions-manual/
http://testbankbell.com/product/test-bank-for-sensation-and-
perception-second-edition/
http://testbankbell.com/product/advanced-financial-accounting-baker-
christensen-cottrell-9th-edition-test-bank/
Test Bank for Principles of Healthcare Reimbursement 5th
Edition by Casto
http://testbankbell.com/product/test-bank-for-principles-of-
healthcare-reimbursement-5th-edition-by-casto/
PHP Programming with MySQL, 2nd Edition 2-1
Chapter 2
Functions and Control Structures
At a Glance
Table of Contents
Chapter Overview
Chapter Objectives
Instructor Notes
Quick Quizzes
Discussion Questions
Key Terms
PHP Programming with MySQL, 2nd Edition 2-2
Lecture Notes
Chapter Overview
In this chapter, students will study how to use functions to organize their PHP code. They will
also learn about variable scope, nested control structures, and conditional coding: if, if…else,
and switch statements and while, do…while, for, and foreach looping statements.
Students will also be introduced to the include and require statements.
Chapter Objectives
In this chapter, students will:
Instructor Notes
In PHP, groups of statements that you can execute as a single unit are called functions. Functions
are often made up of decision-making and looping statements. These are two of the most
fundamental statements that execute in a program.
Defining Functions
To begin writing a function, you first need to define it. The syntax for the function definition is:
Parameters are placed within the parentheses that follow the function name. The parameter
receives its value when the function is called. When you name a variable within parentheses, you
do not need to explicitly declare and initialize the parameter as you do with a regular variable.
Following the parentheses that contain the function parameters is a set of curly braces, called
function braces, which contain the function statements. Function statements are the statements
that do the actual work of the function and must be contained within the function braces. For
example:
function displayCompanyName($Company1) {
echo "<p>$Company1</p>";
}
Remind your students that functions, like all PHP code, must be contained with
<?php...?> tags. Stress that the function name should be descriptive of the
task that the function will perform.
Ask students to add this to their list of PHP “Best Coding Practices.”
Ask students to add this to their list of PHP “Best Coding Practices.”
PHP Programming with MySQL, 2nd Edition 2-4
Calling Functions
A function definition does not execute automatically. Creating a function definition only names
the function, specifies its parameters (if any), and organizes the statements it will execute.
Teaching Refer to the textbook example on page 77, which defines a function and calls it
Tip passing a literal value to the function argument.
Returning Values
Some functions return a value, which may be displayed or passed as an argument to another
function. A calculator function is a good example of a return value function. A return statement
is a statement that returns a value to the statement that called the function.
Use the textbook example on page 78 to illustrate how a calling statement calls a
Teaching function and sends the multiple values as arguments of the function. The
Tip function then performs a calculation and returns the value to the calling
statement.
Usually, the value of a variable is passed as the parameter of a function, which means that a local
copy of the variable is created to be used by the function. When the value is returned to the
calling statement, any changes are lost. If you want the function to change the value of the
parameter, you must pass the value by reference, so the function works on the actual value
instead of a copy. Any changes made by the function statements remain after the function ends.
Teaching Explain to students that a function definition should be placed above any calling
Tip statements. As of PHP4, this is not required, but is considered a good
programming practice.
Ask students to add this to their list of PHP “Best Coding Practices.”
PHP Programming with MySQL, 2nd Edition 2-5
When you use a variable in a PHP program, you need to be aware of the variable's scope—that
is, you need to think about where in your program a declared variable can be used. A variable's
scope can be either global or local. Global variables are declared outside a function and are
available to all parts of the programs. Local variables are declared inside a function and are only
available within the function in which they are declared.
Unlike many programming languages that make global variables automatically available to all
parts of your program, in PHP, you must declare a global variable with the global keyword
inside a function for the variable to be available within the scope of that function. When you
declare a global variable with the global keyword, you do not need to assign a value, as you
do when you declare a standard variable. Instead, within the declaration statement you only need
to include the global keyword along with the name of the variable, as in:
global $variable_name;
Teaching Demonstrate the syntax of declaring a global variable within a function using the
Tip textbook example on page 83.
Quick Quiz 1
1. In PHP, groups of statements that you can execute as a single unit are called
_________________________.
ANSWER: functions
Making Decisions
In any programming language, the process of determining the order in which statements execute
in the program is called decision making or flow control. The special types of PHP statements
used for making decision are called decision-making statements or control structures.
if Statements
You should insert a space after the conditional keyword if before the opening
parenthesis of the conditional expression. This will help you see a visual
difference between a structure and a function. Using a line break and
indentation to enter the statements to execute makes it easier for the
programmer to follow the flow of the code.
Ask students to add the space and indentation to their list of PHP “Best Coding
Teaching Practices.”
Tip
Explain to students that if there is only one statement, they do not need to use
curly braces. If there are multiple statements, the statements should be
enclosed within beginning and ending curly braces. ({...}).
Ask students to add the use of curly braces to their list of PHP “Best Coding
Practices.”
You can use a command block to construct a decision-making structure using multiple if
statements. A command block is a group of statements contained within a set of braces, similar to
the way function statements are contained within a set of braces. When an if statement
evaluates to TRUE, the statements in the command block execute.
if...else Statements
Should you want to execute one set of statements when the condition evaluates to FALSE, and
another set of statements when the condition evaluates to TRUE, you need to add an else
clause to the if statement.
PHP Programming with MySQL, 2nd Edition 2-7
if (conditional expression) {
statements; //condition evaluates to 'TRUE'
}
else {
statements; //condition evaluates to 'FALSE'
}
switch Statements
The switch statement controls program flow by executing a specific set of statements,
depending on the value of the expression. The switch statement compares the value of an
expression to a value contained within a special statement called a case label. A case label
represents a specific value and contains one or more statements that execute if the value of the
case label matches the value of the switch statement’s expression. The syntax for the switch
statement is a follows:
PHP Programming with MySQL, 2nd Edition 2-8
switch (expression) {
case label:
statement(s);
break;
case label:
statement(s);
break;
...
default:
statement(s);
break;
}
Another type of label used within switch statements is the default label. The default
label contains statements that execute when the value returned by the switch statement does
not match a case label. A default label consists of the keyword default followed by a
colon. In a switch statement, execution does not automatically end when a matching label is
found. A break statement is used to exit a control structures before it reaches the closing brace
(}). A break statement is also used to exit while, do...while, and for looping
statements.
The break statement after the default case is optional; however, it is good
Teaching programming practice to include it.
Tip
Ask the students to add this to their list of PHP “Best Coding Practices.”
PHP Programming with MySQL, 2nd Edition 2-9
Quick Quiz 2
1. The process of determining the order in which statements execute in a program is called
_________________________.
ANSWER: decision-making or flow control
Repeating Code
Conditional statements allow you select only a single branch of code to execute and then
continue to the statement that follows. If you need to perform the same statement more than
once, however, you need a use a loop statement, a control structure that repeatedly executes a
statement or a series of statements while a specific condition is TRUE or until a specific
condition becomes TRUE. In an infinite loop, a loop statement never ends because its conditional
expression is never FALSE.
while Statements
The while statement is a simple loop statement that repeats a statement or series of statements
as long as a given conditional expression evaluates to TRUE. The syntax for the while statement
is as follows:
Each repetition of a looping statement is called an iteration. The loop ends and the next
statement, following the while statement executes only when the conditional statement
evaluates to FALSE. You normally track the progress of the while statement evaluation, or
any other loop, with a counter. A counter is a variable that increments or decrements with each
iteration of a loop statement.
PHP Programming with MySQL, 2nd Edition 2-10
do...while Statements
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:
do {
statements(s);
} while (conditional expression);
for Statements
The for statement is used for repeating a statement or series of statements as long as a given
conditional expression evaluates to TRUE. The syntax of the for statement is as follows:
foreach Statements
The foreach statement is used to iterate or loop through the elements in an array. With each
loop, a foreach statement moves to the next element in an array. The basic syntax of the
foreach statement is as follows:
Quick Quiz 3
3. The foreach statement is used to iterate or loop through the elements in a(n)
_________________________.
ANSWER: array
Discussion Questions
When is it better to use a global variable in programming? When is it better to use a local
variable?
Key Terms
break statement: Used to exit control structures.
case label: In a switch statement, represents a specific value and contains one or more
statements that execute if the value of the case label matches the value of the switch
statement’s expression.
command block: A group of statements contained within a set of braces, similar to the way
function statements are contained within a set of braces.
counter: A variable that increments or decrements with each iteration of a loop statement.
decision making (or flow control): The process of determining the order in which
statements execute in a program.
default label: Contains statements that execute when the value returned by the switch
statement expression does not match a case label.
do…while statement: Executes a statement or statements once, then repeats the execution
as long as a given conditional expression evaluates to TRUE.
for statement: Used for repeating a statement or series of statements as long as a given
conditional expression evaluates to TRUE.
function definition: Line of code that make up a function.
functions: Groups of statements that execute as a single unit.
global variable: A variable declared outside a function and available to all parts of the
program.
if statement: Used to execute specific programming code if the evaluation of a conditional
expression returns a value of TRUE.
if…else statement: if statement with an else clause that is implemented when the
condition returns a value of FALSE.
infinite loop: A loop statement never ends because its exit condition is never met.
iteration: The repetition of a looping statement.
local variable: A variable declared inside a function and only available with the function in
which it is declared.
loop statement: A control structure that repeatedly executes a statement or a series of
statements while a specific condition is TRUE or until a specific condition becomes TRUE.
nested decision-making structures: One decision-making statement contained within
another decision-making statement.
parameter: A variable that is passed to a function from the calling statement.
return statement: A statement that returns a value to the statement that called the function.
switch statement: Controls program flow by executing a specific set of statements,
depending on the value of an expression.
variable scope: The context in which a variable is accessible (such as local or global).
while statement: Repeats a statement or a series of statements as long as a given conditional
expression evaluates to TRUE.
PHP Programming with MySQL, 2nd Edition 2-13
The Little Cove (in Franklin county) and the Big and Little
Conolloways being the only places remaining to be visited, as this
was on the borders of Maryland, the magistrates declined going
there, and departed for their homes.
About the year 1740 or 1741, one Frederick Star, a German, with
two or three more of his countrymen, made some settlements at the
place where we found William White, the Galloways, and Andrew
Lycon, on Big Juniata, situate at the distance of twenty miles from
the mouth thereof, and about ten miles north of the Blue Hills,—a
place much esteemed by the Indians for some of their best hunting
ground; which (German settlers) were discovered by the Delawares
at Shamokin to the deputies of the Six Nations as they came down
to Philadelphia in the year 1742, to hold a treaty with this
government; and they were disturbed at, as to inquire with a
peculiar warmth of Governor Thomas if these people had come there
by the orders or with the privilege of the government; alleging that,
if it was so, this was a breach of the treaties subsisting between the
Six Nations and the proprietor, William Penn, who in the most
solemn manner engaged to them not to suffer any of the people to
settle lands till they had purchased from the Council of the Six
Nations. The governor, as he might with great truth, disowned any
knowledge of those persons' settlements; and on the Indians
insisting that they should be immediately thrown over the
mountains, he promised to issue his proclamation, and, if this had
no effect, to put the laws in execution against them. The Indians, in
the same treaty, publicly expressed very severe threats against the
inhabitants of Maryland for settling lands for which they had
received no satisfaction, and said that if they would not do them
justice they would do justice to themselves, and would certainly
have committed hostilities if a treaty had not been under foot
between Maryland and the Six Nations, under the mediation of
Governor Thomas; at which the Indians consented to sell lands and
receive a valuable consideration for them, which put an end to the
danger.
At that time none had presumed to settle at a place called the Big
Cove—having this name from its being enclosed in the form of a
basin by the southernmost range of the Kittochtinny Hills and
Tuscarora Hills; which last end here, and lose themselves in other
hills. This Big Cove is about five miles north of the temporary line,
and not far west of the place where the line terminated. Between
the Big Cove and the temporary line lies the Little Cove,—so called
from being likewise encircled with hills; and to the west of the Little
Cove, toward Potowmec, lie two other places, called the Big and
Little Conollaways, all of them situate on the temporary line, and all
of them extended toward the Potowmec.
In the year 1741 or 1742 information was likewise given that people
were beginning to settle in those places, some from Maryland and
some from this province. But as the two governments were not then
on very good terms, the governor did not think proper to take any
other notice of these settlements than to send the sheriff to serve
his proclamation on them, though they had ample occasion to
lament the vast inconveniences which attend unsettled boundaries.
After this the French war came on, and the people in those parts,
taking advantage of the confusion of the times, by little and little
stole into the Great Cove; so that at the end of the war it was said
thirty families had settled there; not, however, without frequent
prohibitions on the part of the government, and admonitions of the
great danger they run of being cut off by the Indians, as these
settlements were on lands not purchased of them. At the close of
the war, Mr. Maxwell, one of the justices of Lancaster county,
delivered a particular message from this government to them,
ordering their removal, that they might not occasion a breach with
the Indians, but it had no effect.
Richard Peters.
July 2, 1750.
Petitions were sent to the Council from the residents of the Coves, in
which it was set forth that they did not wish to be either in the
province of Maryland or Virginia, and prayed permission to remain,
until the boundary of the provinces was determined, on the lands
purchased from the Indians.
But even the lands west of the Alleghany Mountains were not sacred
to the Indians, mountainous as they were and unfertile as they were
deemed; for westward the squatter went, gradually encroaching
upon the red men's last reserve, until he finally settled in their midst.
These aggressions were followed by the usual proclamations from
the government, but they had little or no effect in preventing the
bold adventurers from crossing the Alleghany Mountains and staking
out farms in the valley of the Conemaugh. This continued for a
number of years, until the government, wearied by unavailing efforts
to keep settlers from Indian lands, caused a stringent law to be
passed by Council in February, 1768, when it was enacted "that if
any person settled upon the unpurchased lands neglected or refused
to remove from the same within thirty days after they were required
so to do by persons to be appointed for that purpose by the
governor or by his proclamation, or, having so removed, should
return to such settlement, or the settlement of any other person,
with or without a family, to remain and settle on such lands, or if any
person after such notice resided and settled on such lands, every
such person so neglecting or refusing to remove, or returning to
settle as aforesaid, or that should settle after the requisition or
notice aforesaid, being legally convicted, was to be punished with
death without the benefit of clergy."
There is no evidence on record that the provision of this act was
ever enforced, although it was openly violated. It was succeeded by
laws a little more lenient, making fine and imprisonment the
punishment in lieu of the death-penalty "without the benefit of
clergy." Neither does the record say that the coffers of the provincial
treasury ever became plethoric with the collection of fines paid by
trespassers.
The first settlements of the upper portion of the valley were not
effected until between 1765 and 1770. True, there was here and
there an isolated family, but the danger of being so near the
Kittaning Path was deemed too hazardous. It was in the upper part
of the valley, too, that most of the massacres took place between
1776 and 1782, as the lower end of it was too thickly populated and
too well prepared for the marauders to permit them to make
incursions or commit depredations.
CHAPTER III.
Still, the island became settled at an early day. The roving Shawnees
pushed their way westward, and the prejudices of those who took
their place were probably overcome by presents of guns,
ammunition, tobacco, and fire-water.
The Rev. David Brainerd, a devout and pious missionary, visited the
island in 1745, in the spring while going up the river, and in the fall
while returning. His object was to convert the Indians, which he
found quite as hopeless a task as did Heckwelder and Loskiel, who
preceded him with the same object in view. During his
peregrinations Brainerd kept a journal, which, together with his life,
was published by the American Tract Society. From this journal we
extract the following, in order to give his views of savage life, as well
as an interesting account of what he saw and heard at the island:—
Sept. 20.—Visited the Indians again at Juneauta Island, and found
them almost universally very busy in making preparations for a great
sacrifice and dance. Had no opportunity to get them together in
order to discourse with them about Christianity, by reason of their
being so much engaged about their sacrifice. My spirits were much
sunk with a prospect so very discouraging, and specially seeing I
had this day no interpreter but a pagan, who was as much attached
to idolatry as any of them, and who could neither speak nor
understand the language of these Indians; so that I was under the
greatest disadvantages imaginable. However, I attempted to
discourse privately with some of them, but without any appearance
of success; notwithstanding, I still tarried with them.
In the evening they met together, nearly one hundred of them, and
danced around a large fire, having prepared ten fat deer for the
sacrifice. The fat of the inwards they burnt in the fire while they
were dancing, which sometimes raised the flame to a prodigious
height, at the same time yelling and shouting in such a manner that
they might easily have been heard two miles or more. They
continued their sacred dance nearly all night; after which they ate
the flesh of the sacrifice, and so retired each one to his own lodging.
Lord's-day, Sep. 21.—Spent the day with the Indians on the island.
As soon as they were well up in the morning, I attempted to instruct
them, and labored for that purpose to get them together, but soon
found they had something else to do; for near noon they gathered
together all their powaws, or conjurors, and set about half a dozen
of them playing their juggling tricks and acting their frantic,
distracted postures, in order to find out why they were then so sickly
upon the island, numbers of them being at that time disordered with
a fever and bloody flux. In this exercise they were engaged for
several hours, making all the wild, ridiculous, and distracted motions
imaginable; sometimes singing, sometimes howling, sometimes
extending their hands to the utmost stretch and spreading all their
fingers: they seemed to push with them as if they designed to push
something away, or at least to keep it off at arm's-end; sometimes
stroking their faces with their hands, then spouting water as fine as
mist; sometimes sitting flat on the earth, then bowing down their
faces to the ground; then wringing their sides as if in pain and
anguish, twisting their faces, turning up their eyes, grunting, puffing,
&c.
This looks more like idolatry than sacrificing ten fat deer and dancing
by the light of their burning fat. Yet, if curing disease by
powwowing, incantation, or the utterance of charms, can be
considered idolatry, we are not without it even at this late day. We
need not go out of the Juniata Valley to find professing Christians
who believe as much in cures wrought by charms as they do in Holy
Writ itself.
Very likely they did not; but is it not most singular that a man with
the reputation for piety and learning that Brainerd left behind him
should arm himself with a Bible to spoil the spirit of the Indians, in
case their incantations should raise the demon of darkness, which, it
would really appear, he apprehended? In speaking of the Shawnee
Indians, or "Shawanose," as they were then called, he stigmatizes
them as "drunken, vicious, and profane." What their profanity
consisted of he does not say. According to all Indian historians, the
Indians had nothing in their language that represented an oath.
Brainerd goes on to say of the Shawnees:—
Soon after this extraordinary feat, Mr. Hulings arrived, and he, in
turn, became alarmed at the absence of his wife; but he soon saw
her making a signal on the other side, and, immediately unmooring
a canoe at the mouth of the Juniata, he got into it and paddled it
over. It was the only canoe in the neighborhood,—an old one left by
Baskin when he fled. Hulings had scarcely rejoined his wife before
he saw the flames shooting up from the old log ferry-house, and the
savages dancing around it, brandishing their weapons; but they
were out of harm's way, and succeeded in reaching Paxton the same
day. In a year or so they returned, and ended their days on the
island.
After this period but few of the roving bands or war-parties ever
came down either the Susquehanna or the Juniata as far as the
island. The massacre of the Conestoga Indians inspired the up-
country savages with so much terror that they deemed it certain
death to go near the settlement of the Paxton boys.
By the time the Revolution commenced, the neighborhood of the
mouth of the Juniata was thickly populated, and the inhabitants had
within their reach ample means of defence; so that the savages in
the employ of the British prudently confined their operations to the
thickly-settled frontier.
CHAPTER IV.
[For the facts on which the two chapters following are based we are
indebted to a gentleman named Andrew Banks, an old resident of Lost
Creek Valley, Juniata county. He was born near York, and settled
near his late place of residence in 1773, and was nearly eighty-nine
years of age when we called upon him early in December, 1855. We
found him enjoying the evening of a long and well-spent life, with
his sense of hearing somewhat impaired, but his intellect and
memory both good. He was a man of considerable intelligence, and
we found him quite willing to give all he knew of the past worthy of
record. He died about the last of the same month.]
testbankbell.com