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/
TestBankBell.com: Your Ultimate Source for Test Banks and Academic Resources
Keywords:
test bank, academic resources, study guides, test preparation, testbankbell, download test
bank, textbook solutions
Contact Information:
Visit us at: https://testbankbell.com - For inquiries, email us:
testbankbell.com@gmail.com
Important Links:
Download Test Banks: https://testbankbell.com/
https://testbankbell.com/product/test-bank-for-php-programming-with-
mysql-the-web-technologies-series-2nd-edition/
testbankbell.com
https://testbankbell.com/product/solution-manual-for-introduction-to-
javascript-programming-with-xml-and-php-0133068307/
testbankbell.com
https://testbankbell.com/product/test-bank-for-introduction-to-
javascript-programming-with-xml-and-php-0133068307/
testbankbell.com
Solution Manual for Programming the World Wide Web 7/E 7th
Edition Robert W. Sebesta
https://testbankbell.com/product/solution-manual-for-programming-the-
world-wide-web-7-e-7th-edition-robert-w-sebesta/
testbankbell.com
Solution Manual for Programming with Microsoft Visual
Basic 2015, 7th Edition
https://testbankbell.com/product/solution-manual-for-programming-with-
microsoft-visual-basic-2015-7th-edition/
testbankbell.com
https://testbankbell.com/product/solution-manual-for-an-introduction-
to-programming-with-c-8th-edition/
testbankbell.com
https://testbankbell.com/product/solution-manual-for-web-development-
and-design-foundations-with-html5-10th-edition-terry-felke-morris/
testbankbell.com
https://testbankbell.com/product/solution-manual-for-programming-with-
microsoft-visual-basic-2017-8th-by-zak/
testbankbell.com
https://testbankbell.com/product/test-bank-for-introduction-to-
geospatial-technologies-2nd-edition/
testbankbell.com
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
Oven Top 1 00
Oven Back 1 25
Oven Door 1 00
This stove is
something
entirely new
and unique. It is
a gas machine,
a cook stove
and an
incandescent
lamp combined.
The stove is
constructed with
a powerful
generator which
is located near
the front of the
stove and is
independent of
all the burners.
This generator can be started quickly, without smoke, and can be
operated at a nominal cost, furnishing gas for all the burners and the
light. The light can be used at any time and independent of the
burners by simply starting the generator. The burners are large, with
removable sawed caps that spread the heat evenly over the bottom
of the cooking vessel. After the generator has been heated there is
no further delay in lighting any of the burners. The “Standard” Ther-
Lite is very simple and easy to operate. It is a safe, efficient and
economical cook stove and, with the additional feature of
incandescent illumination, is the most practical and useful gasoline
stove ever made. The stove can be ordered either with or without the
light. The light attachment can be put on or removed at any time.
No. 520. “Standard” Ther-Lite Gasoline
Stove.
Price $12.00.
The above cut shows the No. 520L Standard Ther-Lite with light
attachment. Can be ordered either with or without the light. Has a
safety tank which is removed for filling. An oven can be placed on top
of stove for baking.
No. 521. “Standard” Ther-Lite Gasoline
Stove.
Price $14.00.
The No. 521L shown in cut has three large burners and can be
furnished either with or without the light attachment. Has a safety
tank which must be removed for filling. An oven can be placed on top
of stove for baking.
No. 525. “Standard” Ther-Lite Gasoline
Stove.
Price $18.00.
The No. 525L shown in cut is a very convenient and suitable stove
for an ordinary family. Has two burners on top of stove and one on
the step for the oven. It is provided with removable safety tank. All
Ther-Lite Stoves can be ordered either with or without the light
attachment. The light can be added to or removed from the stove at
any time.
No. 526. “Standard” Ther-Lite Gasoline
Stove.
Price $20.00.
The No. 526L is a stove well suited to the use of a large family. It
has three burners on top of stove and one on the step for the oven.
Has safety tank and all the conveniences offered on the other sizes of
Ther-Lite Stoves. It is a veritable gas machine, gas stove and
incandescent lamp combined. Its field of usefulness is unlimited.
The “Standard” Giant Burner Gasoline
Stove.
The Nos. 151 and 152 “Standard” Giant Burner Low Juniors are
built on the same principle throughout as our other Giant Burner
stoves. Have the improved “Standard” burners and removable safety
tank. An oven can be used on top of stove for baking. They are very
efficient stoves at a low price. The No. 151 has two burners and the
No. 152 has three, all provided with our improved smokeless lighter.
No. 160. “Standard” Giant Burner.
Price $10.00.
Code Word “Red.”