Getting Started
Basics
Arrays
Loop/Branch
Functions
OOP
Libraries
Files
MySQL
SQL
PHP/MySQL
Regex
Email
Date/Time
Web
Forms
Creating Control Structures:
Looping and Branching
Comparing Values
Using "if," "else" and "elseif"
Using "switch"
Setting Up a "for" Loop
Configuring "while" and "do while" Loops
Using a "foreach" Loop
Branching with "continue," "break" and "goto"
LAB: Using Loops to Display Rows of Data on a Web Page
Comparing Values
What is the importance of comparing values?
Comparing values enables your PHP program to make decisions
What is a comparison operator?
A comparison operator is a symbol which represents
"equal," "greater than," "less than," etc.
What is a comparison expression?
A comparison expression is a series of one or more comparison operations,
often involving (parentheses), which result in a value of TRUE or FALSE
What is the difference between "==" and "==="?
$a == $b checks to see if the
values
match,
whereas $a === $b checks both the value and the data type
Using "if," "else" and "elseif"
How can you use "if" ?
You use "if" and a comparison expression to make a decision in your PHP script
What about "else" ?
"if" along with "else" are used when you need an either/or decision
Why use "elseif" ?
"elseif" is used when you need to add an additional clause to your decision
How are if / else/ elseif considered "branching"?
if / else / elseif allow your program to "flow" in
different directions, much like the branches of a river
How would if / else/ elseif be used in a website?
if / else / elseif allow your program to do different things based on user input
and different conditions which exist on the website at that time
Using "switch"
What is the difference between "switch" and if / else / elseif ?
"switch" is logically the same as if / else / elseif
When is "switch" preferred to if / else / elseif ?
You would use "switch" when you find you are adding too many "elseif" clauses to an "if" statement
How would "switch" be used in a website?
"switch" is used in a website when you have a series of decisions which need to be made on the same criterion
Setting Up a "for" Loop
What does a "for" loop do?
A "for" loop is used to execute a set of commands a fixed number of times
What is the syntax of a "for" loop?
for ( initialization; condition; increment) { // block of commands; }
How would a "for" loop be used in a website?
One popular use of the "for" loop is when you need to
create a drop down list in an HTML form
Configuring "while" and "do while" Loops
What is a "while" loop?
A "while" loop is much like a "for" loop, except that the block of commands are
executed
while
the comparison expression is TRUE
What is a "do while" loop?
A "do while" loop is like a "while" loop, except that the block of commands are
executed
before
the condition (comparison expression) is checked
Why would you use a "while" or "do while" loop instead of a "for" loop?
"while" and "do while" loops are when you need to loop through
commands which operate on an unknown quantity of data
How would "while" and "do while" loops be used in a website?
"while" and "do while" loops are used in a website most often when operating on files or database queries
Using a "foreach" Loop
What is a "foreach" loop?
A "foreach" loop is used to scan ("iterate") through an array from beginning to end
Why would you use a "foreach" loop instead of the other types of loops?
"foreach" loops are tailor made for use with arrays.
The other types of loops could be used with arrays, but are more generic in nature.
How can you access keys and values in a "foreach" loop?
Use this syntax: "foreach ($array as $key => $value) { // block of commands; }
How would a "foreach" loop be used in a website?
"foreach" loops could be used in a website to render rows of data,
or to iterate through an array in order to produce a drop-down "select" field
Branching with "continue," "break" and "goto"
What is the "continue" command?
"continue" is used when you need to skip a block of code but continue inside a loop
What is the "break" command?
"break" is used when you need to skip a block of code and to break out of a loop
What is the "goto" command?
"goto" is used when you need to force a jump to a specific place in the program
How would "continue," "break," and "goto" be used in a website?
These three commands can sometimes simplify your code
Is there controversy over the use of these three commands?
There are many critics of these three commands. Many claim that the need for these commands could be alleviated using "structured programming" principles. Much debate continues to this day.
LAB: Using Loops to Display Rows of Data on a Web Page
From the WorkingFiles folder for this chapter ...
In your editor open the file looping-branching_project_products.php
Also open the file htdocs/sweetscomplete/products.php
Copy the array $products from the first file into products.php
Add a variable $maxProducts ...
$maxProducts = count($products);
Use a "for()" loop to generate a list of products ...
Below <h2>Our Products</h2> notice how the blocks of products repeat ...
Delete all <li> ... </li> blocks except for one
Insert a PHP "for($x = 0; $x < $maxProducts; $x++) {"
before the remaining <li> ... </li> block
Insert a PHP "}" after the remaining <li> ... </li> block
Modify the <img src and alt attributes using
$products[$x]["link"] and $products[$x]["title"]
Modify the <p class="name"> block using products[$x]["title"]
Test in your browser
http://localhost/sweetscomplete/products.php
+
o
-