Matthew M.

Matthew M.

Nested Conditional Control Structures

Slide Duration:

Table of Contents

Course Introduction

12m 13s

Intro
0:00
What Is PHP?
0:12
PHP Hypertext Preprocessor
0:13
Why Learn PHP?
1:09
Why Learn PHP?
1:10
Web Application: Educator Store
2:25
Web Application: Educator Store
2:26
Example of Web Application
3:18
PHP in the Educator Store
7:12
Dynamic Content Generation
7:22
Ease of Website Maintenance
7:55
Form Input Processing and Access to Advanced Functionality
9:00
What You Will Learn
9:36
What You Will Learn
9:37
Who Is This Course For?
10:56
Who Is This Course For?
10:57
How PHP & The Web Work

15m 32s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
Client-Server Model
0:53
Client-Server Model
0:54
HTTP Protocol
2:15
Definition of Protocol
2:16
Hypertext Transfer Protocol (HTTP)
2:37
Uniform Resource Locators
3:46
Uniform Resource Locators (URLs)
3:47
Form of URLs
4:13
Accessing Webpages with URLs
5:13
Serving Webpages
6:14
Serving Webpages, Client Machine, and Server Machine
6:15
Static vs. Dynamic Webpages
8:30
Static Webpage
8:31
Dynamic Webpage
8:55
Server-Side Scripting
9:54
Server-Side Scripting
9:55
Static and Dynamic Webpage Coding Example
11:17
Static and Dynamic Webpage Coding Example
11:18
Serving Dynamic Webpages
13:07
Serving Dynamic Webpages
13:08
Setting Up Your Development Environment

33m 11s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
Development Environments
1:04
Development Environments
1:05
Our Default Development Environment: Window 7
1:54
Remote Development
4:04
Development Machine & Remote Server
4:05
Local Development
6:54
Development Machine
6:55
Software Used In This Course
9:41
Firefox Web Browser & Firebug Add-On
9:42
XAMPP
12:15
PSPAD Text Editor
13:16
XAMPP Installation
13:49
XAMPP Installation
13:50
Verify XAMPP Install
16:26
Verify XAMPP Install
16:27
localhost
19:08
localhost and 127.0.0.1 'loopback' IP Address
19:09
Document Root
21:16
Document Root and Directory Name
21:17
Document Root for Apache in XAMPP: htdocs & Example
22:13
Text Editor Spectrum
26:12
Text Editor Spectrum: Barebones to IDE
26:13
PSPad & Example
27:02
Finding Help
30:26
Web Resources
30:27
Homework Challenge
31:36
Homework Challenge
31:37
Homework Challenge (cont.)
32:38
Homework Challenge (cont.)
32:39
Your First PHP Script

12m 41s

Intro
0:00
Lesson Overview
0:19
Lesson Overview
0:20
.php To HTML
1:00
.php To HTML
1:01
PHP Delimiters
2:20
PHP Delimiters: Opening & Closing PHP Tags
2:21
'Hello, World!' Example
4:34
Echo Statement & PHP Tags
4:35
Adding Second Heading
7:34
Homework Challenge
9:56
Homework Challenge
9:57
Basic PHP Syntax

40m 24s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
PHP Delimiters
0:38
Long and Script Form
0:39
Short and ASP Style
1:33
Example
2:01
php.ini: PHP's Configuration
3:40
php.ini
3:41
Configuration Directives
3:48
Short and ASP Style: Enabled/Disabled
4:13
phpinfo()
7:58
Statements
14:28
PHP Statements
14:29
Example: PHP Statements
14:55
Comments
16:53
PHP Comments
16:55
Single-line Comments
17:37
Multi-line Comments
18:13
Example: PHP Comments
18:47
Coding Conventions
24:26
Coding Conventions
24:27
Example: PHP Coding Conventions
26:19
Homework Challenge #1
33:51
Homework Challenge #1
33:52
Homework Challenge #1 (cont.)
35:41
Homework Challenge #1 (cont.)
35:42
Homework Challenge #2
36:09
Homework Challenge #2
36:10
Homework Challenge #2 (cont.)
38:07
Homework Challenge #2 (cont.)
38:08
Variables & Numeric Data Types

16m 38s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:12
Working With Data
0:48
8 Types of Data for PHP
0:49
Identifiers
1:40
Identifiers: Definition and Example
1:41
Variables
2:47
Variables Definition
2:48
Variables Syntax
3:06
Integer Data Type
4:44
Integer Data Type
4:45
Integer Literals
5:08
Examples
5:30
Float Data Type
6:26
Float Data Type
6:27
Float Literals
7:00
Example
7:21
Example: PHP Code Declaring Variables
8:06
Example: PHP Code Declaring Variables
8:07
var_dump() Function
9:59
var_dump()
10:00
Example: Code and Output
10:30
Example: var_dump() Function
11:19
Coding Conventions: Variables
12:32
Lower Camel Case Notation
12:33
Variable Name Lengths
13:54
Homework Challenge
14:54
Homework Challenge
14:55
String Data Type

18m 6s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
String Data Type
0:29
String Data Type
0:30
Specifying String Literals
1:03
Single-Quoted Strings
1:53
Single-Quoted Strings
1:54
Escape Sequences
2:31
Escape Sequences
2:32
Example
2:46
Escape Sequences for Commonly Used Special Characters
4:32
Double-Quoted Strings
6:04
Double-Quoted Strings
6:05
Variable Interpolation
6:44
Coding Conventions: Strings
7:54
Coding Conventions: Strings
7:55
Homework Challenge
8:54
Homework Challenge
8:55
Include Files & Web Application Introduction

38m 43s

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
include Statement
0:47
include Statement: Definition
0:48
Include Statement: Syntax
2:05
include Statement: Example
2:25
include Path
6:32
Absolute and Relative Path
6:34
Specified Path
7:15
Not Specified Path
7:55
Code Reuse
9:35
Code Reuse
9:36
Example
11:11
require Statement
12:56
require Statement: Definition
12:57
require Statement: Syntax
13:32
Include versus Require
13:52
Coding Conventions
16:33
Coding Conventions
16:34
Introduction to Our Web Application
20:32
Introduction to Our Web Application
20:33
Updating Web Application
21:14
Web Application Example
22:59
Homework Challenge
35:33
Homework Challenge
35:34
Homework Challenge (cont.)
37:38
Homework Challenge (cont.)
37:39
Arrays

34m

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
What is an Array?
0:42
What is an Array?
0:43
Arrays in PHP
1:44
Keys and Values
2:15
Types of Arrays
3:37
Indexed Arrays & Associative Arrays
3:38
array() Construct
6:47
Declaring Arrays
6:48
Defining Indexed Array
7:00
Defining Associative Arrays
7:43
Square Bracket Syntax
8:50
Square Bracket Syntax
8:51
Accessing Indexed Arrays
9:02
Accessing Associative Arrays
9:56
Arrays Example
10:41
Indexed Arrays Example
10:45
Associative Arrays Example
13:55
Multi-Dimensional Arrays
18:28
Multi-Dimensional Arrays
18:29
Multi-Dimensional Arrays Example
18:51
Multi-Dimensional Arrays in PHP File
20:34
Coding Conventions: Arrays
27:59
Coding Conventions: Arrays
28:00
Homework Challenge #1
29:20
Homework Challenge #1
29:21
Homework Challenge #2
30:38
Homework Challenge #2
30:39
Web Application Development

27m 38s

Intro
0:00
Versions
0:14
Version 3.0
1:23
Version 3.1
8:08
Version 3.2
11:42
Version 3.3
20:27
Homework Challenge
26:31
Homework Challenge
26:32
Script Input & The GET Method

30m 18s

Intro
0:00
Lesson Overview
0:15
Lesson Overview
0:16
Providing Input to PHP
0:39
GET Method, POST Method, and Cookies
0:40
Name/Value Pairs
1:22
GET Method
1:57
HTTP GET Method
1:58
Query String
2:52
GET Method Example
3:38
GET Method Example
3:39
Review of HTML Forms
8:16
HTML Forms
8:17
Input Control and Submitted Form
9:13
<form> Tag
10:07
<form> Tag
10:08
Method
10:34
Action
11:13
Input Control Examples
11:50
Input Control Examples
11:51
Common Input Controls
17:31
Common Input Controls
17:32
Query Strings
18:52
Query Strings
18:53
Query Strings Syntax
19:12
URL Encoding
20:01
URL Syntax
20:02
Examples
21:17
Simple Form Example
22:28
urlencode() Function
24:08
urlencode() Function
24:09
Example
25:03
urlEncoding Text Example
25:54
Simple Form Example
25:55
Homework Challenge
28:46
Homework Challenge
28:47
Accessing Form Data in PHP

32m 1s

Intro
0:00
Lesson Overview
0:17
Lesson Overview
0:18
$_GET Array
0:48
$_GET Array
0:49
Accessing the Value of a Name/Value Pair Submitted Via GET
1:04
Name Form Example
2:54
Name Form and the $_GET Array
2:55
Using Arrays with Input Controls
6:34
Using Arrays with Input Controls
6:35
Common Example
6:47
Indexed Array Form Example
8:16
Indexed Array Form Example
8:17
Associative Arrays with Input Controls
10:14
Associative Arrays with Input Controls
10:15
Associative Arrays Example
11:41
Associative Arrays Example
11:42
Echo Form Example
15:18
Echo Form Example
15:19
Outputting Arrays In String
23:42
Variable Interpolation
23:43
Outputting a Value in an Indexed Array Within a String
24:12
Simple' Syntax, 'Complex' Syntax, and 'Curly Brace' Syntax
25:00
Outputting Arrays In String Example
26:25
Outputting Arrays In String Example
26:26
Homework Challenge
29:39
Homework Challenge
29:40
Web Application Development

20m 20s

Intro
0:00
Lesson Overview
0:19
Lesson Overview
0:20
Version 3.3
0:38
Version 3.3
0:42
Version 4.0 Changelog
2:43
GET Query
2:45
Adding, Editing, and Removing
3:24
Version 4.0 Coding Example
3:55
item.php, itemID, and itemListing
4:00
Version 4.1 Changelog
10:36
Version 4.1 Changelog
10:37
Version 4.1 Coding Example
11:45
Adding Checkout and Thank You & Editing Footer and Store
11:46
Homework Challenge
18:45
Homework Challenge
18:46
Expression & Operators

31m 56s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
Expressions
0:41
Expressions Definition
0:42
Example: Literals
0:55
Example: Variables
1:05
Operators
1:44
Operators Definition
1:45
Unary, Binary, and Ternary Operators
2:07
Assignment Operators
2:52
Assignment Operators
2:53
Array Assignment Operator
3:47
Arithmetic Operators
6:15
Operators for Common Arithmetic Operations
6:16
Modulus Operator
7:41
Arithmetic Operators Example
8:25
Increment/Decrement Operators
10:48
Increment/Decrement Operators
10:49
Pre- and Post- Increment/Decrement
12:43
Coding Example
15:14
Combined Assignment Operators
16:44
Combined Assignment Operators
16:45
Combined Assignment Operators Examples
18:23
Coding Example
19:39
String Operators
20:28
Concatenation Operator, String Variables, and String Literals
20:29
String Operators Example
22:41
Precedence & Associativity
23:40
Precedence & Associativity
23:41
Expression Containing Multiple Operations
23:58
Expression Containing Two Operations of Equal Precedence
25:55
Using Parentheses to Force Precedence
26:52
Precedence & Associativity Review
28:57
Homework Challenge
31:08
Homework Challenge
31:09
Web Application Development

20m 51s

Intro
0:00
Lesson Overview
0:15
Lesson Overview
0:16
Version 4.1 Review
0:33
Version 4.1 Review
0:34
Version 5.0 Changelog
1:05
Version 5.0 Changelog
1:06
Version 5.0 Example
2:19
Adding View Cart & Editing Checkout, Footer and Store
2:20
Version 5.1 Changelog
15:36
Version 5.1 Changelog
15:37
Version 5.1 Coding Example
17:33
Forwarding Order Total to Thank You Page
17:34
Homework Challenge
20:09
Homework Challenge
20:10
Boolean & Null Data Types

20m 11s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Boolean Data Type
0:38
Boolean Data Type
0:39
Two Boolean Literals
1:24
Boolean Example
1:50
Boolean Example
1:51
Comparison Operators
4:00
Comparison Operators Definition
4:01
Common Comparison Operators
4:40
Comparison Operators Example
6:49
Comparison Operators (Cont.)
10:10
Identical and Not Identical
10:11
Example: Identical and Not Identical
11:24
Null Data Type
13:36
Null Data Type Definition
13:37
Null Literal
14:08
Variable and Null Data Type
14:30
'==' Operator
15:24
Null Data Type Example
15:59
Coding Convention
18:41
Coding Convention
18:42
Homework Challenge
19:17
Homework Challenge
19:18
Type Casting

22m 41s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Type Juggling
0:54
Type Juggling
0:55
Automatic Conversion
2:23
Type Casting
3:53
Type Casting
3:54
Implicit and Explicit Type Casting
4:00
Explicitly 'cast' a Variable Example
4:16
Type Casting (cont.)
6:48
PHP Allows the Following Explicit Type Casts
6:49
The settype () Function
8:18
Type Casting Coding Example
9:00
Explicit Type Casts
9:01
String Conversions
14:52
String Conversions
15:05
Common Conversions to String
15:55
Numeric Conversions
18:18
Numeric Conversions
18:19
Boolean Conversions
20:29
Boolean Conversions
20:30
Homework Challenge
21:38
Homework Challenge
21:39
Introduction to Functions

52m 20s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
What are Functions?
0:51
Definition of Faction
0:52
PHP and Function Call
1:53
Function Calls
2:42
Function Calls
2:43
Function Arguments
3:17
Return Values
4:56
Return Values
4:57
Function Chaining
6:29
Function Chaining
6:30
PHP.net Function Reference
8:23
PHP.net & Function Prototypes
8:24
PHP.net Function Reference Example
9:29
Optional Function Arguments
12:28
Optional Function Arguments
12:29
String Functions
14:57
strtoupper() and strtolower()
14:58
implode (), str_replace(), explode(), strpos(), substr(), and strlen()
18:31
Array Functions
25:48
count()
25:49
in_array() and array_key_exists()
26:06
sort() and ksort()
26:37
Example: count() and in_array()
27:50
Example: array_key_exists()
29:53
Example: sort() and ksort()
31:20
Date & Time Functions
33:38
date() and time()
33:39
getdate()
34:49
mktime()
35:01
Date & Time Functions
35:12
Example: date() and time()
35:58
Example: getdate()
42:15
Example: mktime()
43:15
Homework Challenge #1
44:31
Homework Challenge #1
44:32
Homework Challenge #1 (Cont.)
45:28
Homework Challenge #1 (Cont.)
45:29
Homework Challenge #2
46:34
Homework Challenge #2
46:34
Homework Challenge #2 (Cont.)
48:06
Homework Challenge #2 (Cont.)
48:07
Homework Challenge #2 (Cont.)
49:17
Homework Challenge #2 (Cont.)
49:18
Homework Challenge #3
50:08
Homework Challenge #3
50:09
Constants

19m 24s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Constants vs. Variables
0:55
Constants vs. Variables
0:56
Constant Identifiers
2:28
Constant Identifiers Definition and Examples
2:29
Declaring Constants
3:47
Two Ways of Declaring Constants
3:48
Syntaxes
4:10
Major Difference in the Two Forms
4:48
Using Constants Example
6:25
Using Constants Example
6:26
Coding Conventions
11:08
Constant Names
11:09
define () Function
11:42
Meaningful Names
12:00
$_SERVER Superglobal
12:23
$_SERVER
12:24
$_SERVER ['DOCUMENT_ROOT']
13:15
$_SERVER Superglobal Example
13:52
Homework Challenge
17:40
Homework Challenge
17:41
Web Application Development

26m 29s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
Version 6.0
1:13
Version 6.0 & Version 5.1 Review
1:14
Version 6.0 Changelog
11:24
Version 6.0 Changelog
11:25
Version 6.1 Changelog
12:00
Version 6.1 Changelog
12:01
Version 6.1 Coding Example
12:42
Version 6.1 Coding Example
12:43
Version 6.2 Changelog
15:18
Version 6.2 Changelog
15:19
Version 6.2 Coding Example
18:19
Version 6.2 Coding Example
18:20
Homework Challenge
25:24
Homework Challenge
25:25
Conditional Control Structures

18m 58s

Intro
0:00
Lesson Overview
0:26
Lesson Overview
0:27
Statement Groups
0:57
Statement Groups
0:58
Example
1:10
Conditional Control Structures
1:38
Conditional Control Structures
1:39
PHP Control Structures
1:56
if Statement
2:32
if Statement
2:33
if Statement (cont.)
3:49
if Statement Coding Example
3:50
else Statement
7:26
else Statement
7:27
if/else Statement Coding Example
8:50
isset() Construct
9:59
isset() Construct
10:00
isset() Construct Coding Example
12:00
Coding Conventions
15:13
Coding Conventions
15:14
Coding Conventions (Cont.)
16:39
Coding Conventions (Cont.)
16:40
Homework Challenge
17:25
Homework Challenge
17:26
Error Handling

19m 8s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Error Handling in PHP
0:41
Error Handling in PHP
0:42
Coding Example
1:45
error_reporting() Function
7:02
error_reporting() Function
7:03
Coding Example
8:04
Additional Error Directives
9:02
display_errors
9:13
log_errors
9:37
error_log
9:50
track_errors
10:12
Coding Examples
10:29
Error Control Operator
13:38
Error Control Operator & Coding Example
13:39
Homework Challenge
16:19
Homework Challenge
16:20
Homework Challenge (cont.)
17:58
Homework Challenge (cont.)
17:59
Logical & Ternary Operators

23m 22s

Intro
0:00
Lesson Overview
0:17
Lesson Overview
0:18
Logical Operators
0:49
Logical Operators Definition
0:50
NOT (!)
1:08
OR ( ||, or)
1:35
AND (&&, and)
2:08
XOR (xor)
2:30
Logical Operators (cont.)
2:54
The OR and AND Logical Operators
2:55
Precedence of Logical Operators
3:35
Logical Operators Coding Example
3:58
Logical Operators Coding Example
3:59
Short-Circuit Operators
9:54
Short-Circuit Operators
9:55
Coding Example
10:49
Ternary Operator
14:07
Ternary Operator
14:08
Syntax and Example
14:24
Coding Conventions
17:36
Coding Conventions
17:37
Homework Challenge
19:08
Homework Challenge
19:09
Homework Challenge (cont.)
20:26
Homework Challenge (cont.)
20:27
Web Application Development

19m 27s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Version 6.2 Review
0:26
Version 6.2 Review
0:27
Version 7.0 Changelog
2:39
Version 7.0 Changelog
2:40
Version 7.0 Coding Example
4:35
Version 7.0 Coding Example
4:36
Version 7.1 Changelog
12:43
Version 7.1 Changelog
12:44
Version 7.1 Coding Example
13:52
Version 7.1 Coding Example
13:53
Homework Challenge
17:42
Homework Challenge
17:43
More Conditional Control Structure

20m 49s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
elseif Statement
0:45
elseif Statement
0:46
elseif Statement Coding Example
1:22
Multiple elseif Statements
2:16
Multiple elseif Statements
2:17
Multiple elseif Statements Coding Example
3:07
Adding an else Statement
5:44
Adding an else Statement Overview
5:45
Adding an else Statement Coding Example
6:50
switch() Statement
8:07
switch() Statement
8:08
switch() Statement (Cont.)
9:14
switch() Statement (Cont.)
9:15
switch() Statement Coding Example
11:09
default Case
14:20
default Case
14:21
default Case Coding Example
15:13
Coding Conventions
15:57
Coding Conventions
15:58
Coding Conventions (cont.)
17:10
Coding Conventions (cont.)
17:11
Homework Challenge
18:06
Homework Challenge
18:07
Homework Challenge (cont.)
19:18
Homework Challenge (cont.)
19:19
Nested Conditional Control Structures

24m 49s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
Nested Control Structures
0:36
Nested Control Structures
0:37
Nested Control Structures Coding Example
2:08
Coding Conventions
6:34
Nested vs. Not Nested Control Structures
6:35
Debugging Control Structures
7:51
Debugging Control Structures
7:52
Incorrectly Specified Test Conditions and Forgetting a Break Statement
8:32
Incorrectly Placing an Opening or Closing Curly Brace
12:14
Debugging Tips
16:24
Tracing the Execution of Your Code
16:25
Adding Echo Statement
17:15
Homework Challenge
21:41
Homework Challenge
21:42
Homework Challenge (cont.)
23:08
Homework Challenge (cont.)
23:09
Web Application Development

28m 20s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
Version 7.1 Review
0:46
Version 7.1 Review
0:47
empty () Construct
1:38
empty () Construct
1:39
empty () Construct Coding Example
2:37
Version 8.0
5:32
Version 8.0 Overview
5:33
Version 8.0 Coding Example
7:08
Version 8.0 Coding Example
7:09
Version 8.1
16:13
Version 8.1 Overview
16:14
Version 8.1 Coding Example
19:48
Version 8.1 Coding Example
19:49
Homework Challenge
26:19
Homework Challenge
26:20
Sending Email Using PHP

43m 50s

Intro
0:00
Lesson Overview
0:14
Lesson Overview
0:15
Built-in Mail Extension
1:28
Built-in Mail Extension
1:29
SMTP Overview
2:04
SMTP Overview
2:05
SMTP - Windows
3:08
SMTP - Windows
3:09
SMTP - Linux/UNIX
4:58
SMTP - Linux/UNIX
4:59
Mail Configuration Directives
6:35
Mail Configuration Directives
6:36
Coding Example
7:48
Mail Server Authentication
10:20
Mail Server Authentication
10:21
fake sendmail Program
12:27
fake sendmail for Windows
12:28
Main 'add-on' feature
13:03
Username & Password
13:25
SMTP - XAMPP for Windows
14:07
SMTP - XAMPP for Windows
14:08
Sendmail Example
16:19
Sendmail Example
16:20
mail() Function
18:39
mail() Function
18:40
additional_headers
19:45
'From' Header
20:12
mail() Function Coding Example
21:09
mail() Function Coding Example
21:40
Web Application Development
31:43
Version 9.0 Changelog
31:44
ContactUs.php
32:52
ContactUs.php
32:53
contactInfo
33:45
Version 9.0 Coding Example
34:31
Version 9.0 Coding Example
34:32
Homework Challenge
41:32
Homework Challenge
41:33
User-Defined Functions

56m

Intro
0:00
Lesson Overview
0:16
Lesson Overview
0:17
Defining Functions
1:29
Four Parts of Defining a Function
1:30
Functions Example 1
2:29
Function Parameters
5:29
Function Parameters
5:30
Functions Example 2
8:20
Return Statements
12:53
Return Statements
12:54
Functions Example 3
14:20
Where to Define Functions
20:34
Where to Define Functions
20:35
include_once Construct
22:10
include_once Construct
22:11
include_once Coding Example
23:55
Reasons to Use Functions
27:44
Take Advantage of Code Reuse
27:45
Improve Code Readability
29:56
Use Instead of 'content' Include Files
32:12
Web Application Development
34:42
Version 10.0 Changelog
34:43
Version 10.0 Coding Example
37:55
Version 10.0 Coding Example
37:56
Outputting HTML in Functions
47:04
Outputting HTML in Functions
47:05
Example
49:02
Coding Conventions
53:16
Coding Conventions
53:17
Homework Challenge
54:33
Homework Challenge
54:34
Variable Scope

31m 37s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
What is Variable Scope?
0:57
Variable Scope
0:58
Global Scope
1:15
Local Function Scope
1:50
Variable Scope Coding Example
2:26
Variable Scope Coding Example
2:27
global Keyword
8:52
global Keyword Overview
8:53
global Keyword Example
9:25
Superglobals
12:34
Superglobals
12:35
Superglobals Example
14:53
Pitfalls of Global Variables
18:34
Pitfalls of Global Variables
18:35
When to Define Variables
22:09
When to Define Variables
22:10
Putting It All Together
22:56
Putting It All Together Example
22:57
Function Scope
28:56
Function Scope
28:57
Homework Challenge
29:41
Homework Challenge
29:42
Homework Challenge (cont.)
30:59
Homework Challenge (cont.)
31:00
Web Application Development

28m 27s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Version 11.0 Changelog
0:56
Version 11.0 Changelog
0:57
processGetVar()
1:42
processGetVar() Overview
1:43
processGetVar() Example
2:25
emailComments()
6:35
emailComments() Overview
6:36
emailComments() Example
7:14
outputItemLink()
11:19
outputItemLink() Overview
11:20
outputItemLink() Example
11:45
calcCartTotal()
19:25
calcCartTotal() Overview
19:26
calcCartTotal() Example
21:14
Homework Challenge
25:56
Homework Challenge
25:57
Optional Parameters

19m 35s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
Optional Parameters
0:26
Optional Parameters Definition
0:27
Default Values
0:53
Optional Parameters Coding Example
3:26
More on Optional Parameters
6:55
Multiple Optional Parameters
6:56
Coding Example
8:05
Homework Challenge
16:18
Homework Challenge
16:19
Web Application Development

23m 7s

Intro
0:00
Lesson Overview
0:13
Lesson Overview
0:14
Version 12.0 Changelog
0:55
Version 12.0 Changelog
0:56
Update to outputLink()
1:44
Update to outputLink()
1:45
outputLink() Coding Example
2:40
outputImg()
13:57
outputImg() Overview
13:58
outputImg() Coding Example
15:35
Homework Challenge
22:05
Homework Challenge
22:06
Introduction to Conditional Loops

57m 20s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
while Loop
0:48
Definition
0:49
Syntax and Usage
1:12
Coding Example
3:33
Looping Over Arrays
13:16
Looping Over Arrays
13:17
Coding Example
13:53
Looping Over Arrays (cont.)
16:43
Internal Array Pointer
16:44
Array Traversal Functions
17:29
current ()
17:30
next () and prev ()
18:16
reset () and end ()
19:25
key ()
19:40
Indexed Arrays: Using current () and next ()
20:40
Indexed Arrays: Using current () and next ()
20:41
Associative Arrays: Using key (), current (), and next ()
24:06
Associative Arrays: Using key (), current (), and next ()
24:07
Array Traversal Functions (cont.)
29:41
list () Construct and Example
30:07
each () Construct and Example
34:40
Lopping Over Arrays Using list(), each()
42:13
Lopping Over Arrays Using list(), each()
42:14
Control Structure Scope
50:37
Definition and Example
50:38
Control Structure Scope Coding Example
51:59
Coding Conventions
54:20
Coding Conventions
54:21
Homework Challenge
54:58
Homework Challenge
54:49
More on Conditional Loops

35m 50s

Intro
0:00
Lesson Overview
0:08
Lesson Overview
0:09
do-while Loop
0:42
do-while Loop
0:43
Simple do-while Loop Example
1:51
Another do-while Loop Example
4:09
continue Statement
10:46
continue Statement
10:47
For Example
11:07
continue Statement Coding Example
12:44
break Statement Re-Visited
18:10
break Statement Re-Visited
18:11
break Statement In while Loops Example
19:38
Infinite Loops
23:26
Infinite Loops
23:27
Coding Example
24:57
Common Loop Pitfalls
25:59
Counter Initialization Occurs in Loop
26:30
Counter Not Incremented in Loop
29:30
Unreachable Break Statement
30:50
Coding Conventions
33:48
Do-while Statements Coding Conventions
33:49
Homework Challenge
34:28
Homework Challenge
34:29
Web Application Development

22m 7s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
Version 13.0 Changelog
0:48
Updating 'viewCart.php' and calcCartTotal()
0:49
Creating isValidCart()
9:22
Adding Three New Item to the Store & Links
15:56
Version 13.1 Changelog
17:20
Updating outputItemLink() to Remove Its Global Dependency on $itemCatalog
17:21
Homework Challenge
20:34
Homework Challenge
20:35
For & Foreach Loops

29m 28s

Intro
0:00
Lesson Overview
0:11
Lesson Overview
0:12
for Loops
0:45
for Loops
0:46
Expression 1
1:22
Expression 2
1:47
Expression 3
2:01
Simple Example
2:27
Simple Example
2:28
Notes on for Loops
8:56
Notes on for Loops
8:57
Ending Loop Using Test Condition and Break Statement
10:06
Ending Loop Using Test Condition and Break Statement
10:07
foreach Loops
12:03
foreach Loops
12:04
Indexed Array Syntax
14:10
Syntax
14:11
Example
15:23
Associative Array Syntax
18:31
Syntax
18:32
Example
19:47
Coding Conventions
25:05
for Loops
25:06
foreach Loops
25:58
Homework Challenge
26:52
Homework Challenge
26:53
Web Application Development

25m 58s

Intro
0:00
Lesson Overview
0:12
Lesson Overview
0:13
Version 14.0 Changelog
1:19
Version 14.0 Changelog
1:20
Version 14.0 Coding Example
1:57
Version 14.0 Coding Example
1:59
Version 14.1 Changelog
5:39
Version 14.1 Changelog
5:40
Version 14.1 Coding Example
7:06
Version 14.1 Coding Example
7:07
Version 14.2 Changelog
15:37
Version 14.2 Changelog
15:38
Version 14.2 Coding Example
16:25
Version 14.2 Coding Example
16:26
Homework Challenge
23:35
Homework Challenge
23:36
Conditional Loop Wrap-Up

23m 12s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Nested Conditional Loops
0:39
Nested Conditional Loops
0:40
Coding Example
1:10
continue & break Re-Visited
5:17
Continue Statements and Coding Example
5:30
Break Statements and Coding Example
11:34
Loop Debugging Tips
15:16
Add Short Debug Statement At the Very Beginning
15:17
Output a Counter Variable
18:10
Add Debug Statement At the Very End
19:20
Homework Challenge
20:20
Homework Challenge
20:21
Homework Challenge (cont.)
22:00
Homework Challenge (cont.)
22:01
Variable-Length Parameter Lists

22m 16s

Intro
0:00
Lesson Overview
0:09
Lesson Overview
0:10
Variable-Length Parameter Lists
0:34
Variable-Length Parameter Lists
0:35
Coding Example
1:51
Variable-Length Parameter Lists (cont.)
5:21
When a Parameter List is Defined For the Functions
5:22
Coding Example
6:32
Variable Type Functions
9:54
is_int (), is_float (), is_string (), and is_bool()
9:55
is_ array ()
10:50
is_null ()
11:01
Variable Type Functions Coding Example 1
11:27
is_numeric() and Example
15:57
Variable Type Functions Coding Example 2
17:12
Homework Challenge
19:35
Homework Challenge
19:36
Homework Challenge (cont.)
20:52
Homework Challenge (cont.)
20:53
Web Application Development

38m 36s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
Version 15.0 Changelog
0:33
outputLink (), outputImg (), is_array () and is_string ()
0:34
Version 15.0 Coding Example
1:31
Version 15.0 Coding Example
1:32
Version 15.1 Changelog
7:55
Removing Redundant Code and Adding New Function Called outputHtmlTag ()
7:56
Version 15.1 Coding Example
8:37
Version 15.1 Coding Example
8:38
Version 16.0 Changelog
14:55
Creating emailOrder() Using New Constants ORDER_EMAIL_FROM and ORDER_EMAIL_SUBJECT
14:56
Version 16.0 Coding Example
16:30
Version 16.0 Coding Example
16:31
Version 16.1 Changelog
32:21
Creating formatAsDollars ()
32:22
Version 16.1 Coding Example
32:57
Version 16.1 Coding Example
32:58
Homework Challenge
36:27
Homework Challenge
36:28
Miscellaneous Topics

31m 49s

Intro
0:00
Lesson Overview
0:10
Lesson Overview
0:11
register_globals Directive
0:58
register_globals
0:59
Coding Example
2:04
$_GET vs. $HTTP_GET_VARS
4:07
$_GET vs. $HTTP_GET_VARS
4:08
register_long_arrays Directive Coding Example
5:44
Magic Constants
7:30
Magic Constants
7:31
__LINE__, __FILE__, __FIR__, and __FUNCTION__
8:16
Coding Example
9:06
exist() & die()
13:19
exist() & die()
13:20
Coding Example
14:08
Execution Operator
16:23
Execution Operator
16:24
Coding Example
17:27
Array Operators
18:23
Equality (==) and Inequality (!=, <>)
18:43
Identity (===) and Non-Identity (!==)
19:13
Union (+) Operator
19:41
Array Operators Coding Example
20:07
Variable Variables
24:13
Variable Variables
24:14
Coding Example
26:07
Variable Functions
28:02
Variable Functions
28:03
Coding Example
29:13
Loading...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
Bookmark & Share Embed

Share this knowledge with your friends!

Copy & Paste this embed code into your website’s HTML

Please ensure that your website editor is in text mode when you paste the code.
(In Wordpress, the mode button is on the top right corner.)
  ×
  • - Allow users to view the embedded video in full-size.
Since this lesson is not free, only the preview will appear on your website.
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Nested Conditional Control Structures

  • PHP allows the nesting of control structures, which means the statement group of one conditional control structure can contain other conditional control structures.
  • There is no limit to the number of levels of nesting allowed.
  • Common control structure errors include:
    • Incorrectly specified test conditions
    • Forgetting a break statement
    • Incorrectly placing an opening or closing curly brace

Nested Conditional Control Structures

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:10
    • Lesson Overview
  • Nested Control Structures 0:36
    • Nested Control Structures
    • Nested Control Structures Coding Example
  • Coding Conventions 6:34
    • Nested vs. Not Nested Control Structures
  • Debugging Control Structures 7:51
    • Debugging Control Structures
    • Incorrectly Specified Test Conditions and Forgetting a Break Statement
    • Incorrectly Placing an Opening or Closing Curly Brace
  • Debugging Tips 16:24
    • Tracing the Execution of Your Code
    • Adding Echo Statement
  • Homework Challenge 21:41
    • Homework Challenge
  • Homework Challenge (cont.) 23:08
    • Homework Challenge (cont.)

Transcription: Nested Conditional Control Structures

Hello again, and welcome back to Educator.com's Introduction to PHP course.0000

In today's lesson, we are going to be covering the topic of nested conditional control structures.0005

Specifically, we are going to be learning about what a nested conditional control structure is and how to use it.0012

We are going to talk about coding conventions as they apply to nested control structures.0018

And then, we are also going to go over how to debug conditional control structures:0023

especially once nested gets involved, as you will see, they can become quite complicated.0030

So, we are going to provide some debugging tips for you.0035

Nesting control structures means that, within the statement group of one conditional control structure, you include another conditional control structure.0039

For example, if we have an if statement with its test condition, and then its opening brace,0049

these opening and closing braces define the statement group of that if statement.0056

Well, within this if statement, we have included another conditional control structure--another if statement.0061

And then, within this if statement, we have included another conditional control structure, this statement.0069

These actually should be indented one further.0077

And so, what you can see is that, in PHP, there is no limit to the number of levels--how deep you can nest control structures.0081

And what it allows you to do is to achieve fine-grain control over the execution of conditional segments of your code.0090

For example, maybe you have a section of code that you want to run if someone is a female, their age is over 25, and they are over 60" tall.0100

In this case, the code we would execute would be "you are a female over age 25, over 5 feet tall."0109

Using nested control structures like this allows you to achieve that sort of fine-grain control,0116

to only execute code based on if multiple conditions are met.0123

Let's go and take a look at a script I have created called nested.php.0129

And what it does...before I get into this stuff here at the top, let's look at the code for it.0135

It takes two GET parameters: one called function and one called arg.0140

function can take on one of three different values: date, toUpper, or toLower.0149

What that is going to correspond to is the date function, the strtoupper function, and the strtolower function, which we learned about.0156

arg is going to refer to an argument that you would pass to these functions.0163

In our script, by passing a function, the function name by the function GET variable, and the function argument by the arg GET variable,0170

what we can do is set up our code so that it will run the specified function on the argument that was requested.0180

The first thing we do is: using the ternary operator, we create short versions of our GET variables; we call one function and one arg.0187

function, again, is the name of the function, and arg is the argument we will be passing to it.0195

Then, we have a couple of nested control structures.0200

So, the first test we do is: we test if function was not equal to null--so that is saying, "Was function provided as a GET variable?"0204

If that is true, then we go ahead and proceed into that if statement.0211

If not, we are going to output a message and say, "No function was selected."0215

Now, once we get in here, we perform another test with an if statement: is argument not equal to null?0219

So, if function wasn't equal to null, but argument was, then we are going to output a message that says no function argument was provided.0224

If not, we are going to go ahead and enter the if statement group.0232

Within this if statement group, which we enter if function is not null and arg is not null, what we have is a switch statement.0235

It runs over the variable function, which is the name of the function passed in.0244

What it does is: it compares that name to each of these three different cases: date, toUpper, and toLower.0249

If it matches any of those cases, what it is going to do is: it is going to run0256

that corresponding function with the argument that was passed in by a GET.0259

And if an argument was passed in, but the function name is not one of these four--it is not one of the ones recognized--0266

then it is going to, by default--its default case is going to output a message that says "invalid function was requested."0273

What I have up here is a form I have created that uses the GET method.0279

It makes testing these out a little bit easier; and you can see that it has the three functions selected for which this code can process.0284

If we look at the source code for this, we can see that this Select dropdown box has three options0294

whose values are date, toUpper, and toLower, which match those cases in our switch statement.0303

So, if I select date, and I pass an argument to it, we know the date function outputs a formatted date,0310

and it does so based on a formatting string we provide to it.0318

And so, I want to output the day, the month, and the year; one way to do that is with the characters j M Y.0321

And if we go ahead and click the Call Function button, we can see that it outputs the current date, June 27, 2011.0328

Likewise, if we do strtoupper, and we pass it a lowercase string, and we call the function, the result we get is turned to all uppercase.0337

Now, if we use the strtolower function, and we put an uppercase string, and we call it, it is going to convert it to lowercase.0350

So, as you can see, by passing in the function name and an argument value, and then using this set of nested control structures,0361

here we have a switch statement nested within an if statement, nested within another if statement.0369

What we do is: we compare one of the GET variables (in this case, function) to a bunch of different cases.0375

And then, we use the other argument, the other GET variable passed in, to go ahead and run a function based off of that.0380

So, that is how you can make use of nested conditional control structures.0388

Now, I just want to talk about coding conditions that we are going to use for nested control structures.0395

The way it is going to work is: when you nest a control structure within another one,0401

you are going to indent the entire nested control structure one indent deeper than the enclosing structure.0406

And we saw that here in nested.php--if we look at the code of that, we can see that here, we have an if statement,0412

and within that, we have nested a switch statement.0420

And as you can see, the switch statement is one indent further than the enclosing control structure.0424

And that said, when you do indent, when you do nest a control structure, the same indentation rules should apply for that control structure.0433

For example, we know that for a switch statement, all of the case statements are one indent deeper than the switch keyword.0441

So, when we nest a switch statement within an if statement, we still indent all of these one extra indent.0446

By the time we get down to these statements here, based on our rules that we have, we have this break statement0452

that is indented four indents deep: one for this if, one for this switch, one for this case, and one for indenting the case statements.0457

So now, I just want to talk a little bit about debugging control structures.0474

As you can see, when you start adding a lot of nested control structures, the code can get very complicated.0476

You get a lot of test conditions, a lot of curly braces, a lot of semicolons, a lot of break statements...things that are easy to mess up, on occasion.0481

So, debugging them can be quite challenging.0493

Common questions you might be asking yourself are, "Why is the code in this if statement not executing when it is supposed to?"0496

"I have supplied the inputs that I thought were supposed to make it happen; why is it not happening?"0502

On the converse of that, "Why isn't the code in a particular if statement executing?" which is right here.0506

And some of the common errors that occur in our control structures are incorrectly specifying test conditions for your control structures,0512

for example, an if test condition; forgetting a break statement in a switch statement; and then also0520

one that can be the most deceiving is incorrectly placing or omitting an opening or closing curly brace.0527

Let's look at the first one, incorrectly specified test conditions.0534

If we go back to our nested.php...let's say we misspelled date here--we put date1, for example.0538

If we go to our code (this is the code behind nested.php), and we change date to date1 and save it,0547

and we refresh the page, now when we go up here and we try to run our date function, and we try to output a formatted date,0556

and we call the function--instead of getting a formatted date, we are getting "invalid function requested."0570

Why is that? Well, what happened is: we specified a function called date.0575

However, when we went through our switch statement (and actually, this is not the updated version,0582

because this was hard-coded into the HTML; let's go look at the code), what is going to happen is: it is going to have a function value;0587

it is going to have an argument value; when it enters the switch statement it is going to have,0594

the function variable is going to be set to date, and it is going to be looking for date.0598

It doesn't find it here, because we have mistyped it; it doesn't find it here; it doesn't find it here.0602

So, what does it do? It outputs a default statement, which says "invalid function was requested."0606

That is one way where misspelling a test condition can cause problems.0610

Another one could be if we leave out a break statement.0617

For example, let's say we leave out the break statement here.0620

We know from what we learned about the switch statement that, once a case is matched within a switch function,0622

every statement is executed until a break or the end of the switch statement is reached.0629

By leaving out the break statement here, what is going to happen is: if we select date, and it matches,0634

we are going to echo this statement, and instead of breaking out of this switch statement and back into whatever statement follows it,0640

we are going to go down here and execute the next line of code it finds, which is this echo statement.0646

So, this statement is going to be echoed, as well; then, it reaches a break, and it's going to break out.0652

We save this, and we refresh it, and we try to run the same thing as before.0657

Now, we have corrected...well, it looks like it already did it, but let me do it anyway...0666

What is going to happen is: we call the function; it is going to output the date, as we expected.0673

But then also, what it is going to do is: it is going to output that next line of code, which as we saw, was strtoupper.0679

What it is going to do is convert that argument we passed in, which was a date formatting string, and just capitalize it and output it.0685

And this could go even a step further: let's say we forgot to include the break statement here--then it is also going to call the strtolower function.0692

So, if we refresh this page again, which is going to submit the same GET values, we can see, it also added it in lowercase.0700

So, that is one error that can also happen with control structures.0707

Let me go ahead and fix these back, and save it and reload it, just to make sure everything is working as normal.0711

OK, I'll test out one of the functions; OK, so everything is working as it is supposed to.0726

Those are the two problems that can often happen, that we just looked at: incorrectly specified test conditions and the forgetting of a break statement.0736

Also, you can incorrectly place or omit a curly brace, which can cause a lot of problems.0745

It makes it hard to figure out what is going on, because PHP is going to give you an error, but usually the error says "error occurred on line 21."0752

But maybe the error actually didn't occur on line 21; it occurred earlier in the file--and I'll show what that looks like now.0758

If we go and look at, for example...we are going to look at this script called debugTest.php.0765

If we go and look at it in the code, we have the same form at the beginning that allows us to select a function and an argument.0777

And then, what it does is: we have a similar code as in the nested.php, except we'll just remove this inner switch statement.0790

And instead of the switch statement, I have just put an if statement that always evaluates true, and instead of0799

calling the function, it just outputs a lot of sample text.0807

And the reason I put...you will see why I put so many sample text lines here in a minute.0810

When I go ahead and run this form, what it should do is: if I pass it a valid function name and a valid argument,0815

it should reach here and output all of these sample lines of text.0822

So, if we go and look at debugTest, and I call the function j M Y (it is not even a matter of what I put in, because0825

we are not calling the functions anymore), and press Call Function, you can see, it outputs all of these lines of sample text.0833

Now, one error that is common, that can cause a lot of problems, is: let's say we forget this last closing brace for the if statement.0840

We save it, and you might look at it, and everything might look OK, so we go ahead and we run the file.0848

And if we refresh the page to run it again with the same values, we are going to get an error.0854

It says you have an unexpected error on line 76; so if we go and look back here, the error that we omitted was all the way up here.0859

But it is saying the error is occurring on line 76.0868

Now, the reason that that is happening is: if we look at this from the PHP Interpreter's point of view, for this internal if statement,0872

once it reaches this opening bracket, it evaluates all of these statements as being part of this if statement.0880

When it reaches the first closing bracket, it says, "OK, I am done with this if statement."0886

Then, when it reaches the next closing bracket, it is going to say, "I'm done with this statement here."0891

And in this case, because there wasn't a third closing bracket for this if statement up here, when PHP reaches the end of the file,0897

it is going to say, "Hey, I never received a closing brace for this if statement up here."0903

It is supposed to see three different braces, and that is why it points to the error down here, because it works inward out,0907

in that it sees, "OK, this if statement was closed; this if statement was closed; but this first one wasn't."0916

So, even though we omitted the curly brace on this most internal if statement, it is generating an error for out here.0922

And as you can see, the error says "unexpected end"; and what that is meaning is that it reached the end of the file,0930

and it was looking for a closing curly brace for this if statement.0937

So, that is one error that can really deceive you, because you might be looking at it and saying, "Well, there is no error on line 76."0941

And the reason is because it is a brace that was maybe omitted...0948

And the reason I put several sample text lines here is to demonstrate0951

how it is going to show the error on a line way down further from where it occurred.0956

For example, this is something that can happen, and it may throw you an error that doesn't make sense, and that is why:0963

because you may have omitted a curly brace 100 lines prior in your code.0969

So, if we go ahead and put this curly brace back in and appropriately close that if statement, and we run it again,0973

everything should work out fine; and there it is.0978

One of the useful techniques you can do for debugging conditional control structures is tracing the execution of your code,0985

which is kind of what we did, where we look through the code, and for example, we looked at debug.txt.0991

Or actually, if we go to nested.php, we can say, "OK, let's say we provided a function and an argument variable as GET variables."0999

So, we would trace down our code, and we would say, "OK, start here; function is not equal to null."1008

"OK, I'm in here," and we're going to proceed further down; "Argument is not equal to null; I enter here."1013

Then we go to function, and then we go through and evaluate what the different case statements are and say,1018

"OK, is it compared to date?" If it does, we echo this, we break out of it, and then we break all the way back out to the beginning.1023

That is tracing through your code and visually reasoning where the code is going to progress.1030

One trick you can do is to include echo statements at the beginning of each conditional statement, or conditional control structure,1037

or switch case, with a brief message that is going to let you know that the statement group or case was entered.1044

What that is going to do is (and you only use this when you are debugging, because it is going to output messages1051

that you wouldn't want to see on a production site): let's say you know that1055

you are evaluating an if condition that is three if's deep within your file.1060

If you put a debug statement at the beginning of every time you enter an if statement,1066

then you know that, when you look at your output when you run a test--you should see three debug statements.1070

And if you don't, it gives you a clue: if you only see one, or if you only see two, it gives you a clue as to maybe where your error is occurring.1075

Maybe you are not entering a particular if statement--when the debug statement doesn't show up,1081

you can say, "Well, why didn't I enter this if statement?" and it can clue you in to where that error is happening.1086

So, if we actually go back and look at another file called debugging.php, it is the same file as nested.php,1091

except that it has debug statements added.1099

So, if we go to debugging.php, and we look at the code, we can see that, starting from the first if statement,1105

after each if statement was entered, we output a small statement that says "debug: function was not equal to null," because that is this test condition here.1112

And then, once we enter the second if statement, we say "argument not equal to null."1122

Then, for each case, so that we know when we have entered a case--we have matched a case in our switch function--1126

we also echo a debug statement: this one says, "OK, we are calling the date function."1131

Here, we are calling the strtoupper function.1135

What that will do is let you trace through and make sure you are going through all of the conditional control structures1138

that you are supposed to, meaning that this test condition here was met,1143

this test condition here was met, and this test condition here was met, for example, if we were using the date function.1146

If we go back now, with these debug statements included, and we run our code,1153

and call the function, we see the output of our debug statements.1161

We can see the first one, debug: function is not equal to null; so that tells us, "OK, we have successfully gotten to this first if statement."1165

debug: argument not equal to null--well, that debug statement means that we got into the second if statement.1173

debug: calling date--that means that we matched the date case in our switch function.1180

And so, therefore, we know that we have gone through and met all of the test conditions that we were supposed to meet for our script,1185

and that provides a way of giving us feedback, in case there is an error.1191

For example, if I go back over to...once again, go to change this date to date1 so that it doesn't match, so we have an error there,1195

and we save it and run the script again, two things are going to happen.1212

One is: we are not going to get the formatted date; we are going to see an unexpected debug statement,1218

saying the default case was reached (which--we are going to wonder, "Why is that?).1223

And then, we are going to see "invalid function requested."1226

If we go back and look at our code and trace through that (and let's look at the actual code),1228

well, in the output, we first saw that function not equal to null and argument not equal to null debug statements were output.1236

So, we know that we have entered this if statement; we know that we have entered this if statement.1243

The next debug statement that occurs in our output is "default case reached."1248

Let's continue tracing through the program; we are in this if statement; we enter the function statement.1253

We should be hitting this debug statement here, but instead, we are outputting "default case is reached,"1258

which means we are reaching a default case, which means that, for whatever reason, the value that we expected1263

to match the date case up here didn't match.1269

And sure enough, if we go ahead (this will clue us in) and say, "Wait a minute, why didn't the date case match?"1272

OK, we had a typo on the date; and what that will allow you to do is: then you can go back and refresh the page,1276

and you can see that it is working now.1285

And so, that is a way to help you debug very complex nested control structures.1287

It is quite a useful technique of just outputting simple debug statements, as they are called,1292

while you are in development, to make things easier, to figure out what is going on.1298

For today's homework challenge, I want to have you do something kind of similar to what we did today1303

with our script that allowed you to select a function and an argument.1307

And it is going to accept three GET parameters: one called operation, one called leftOp, and one called rightOp.1311

What these are going to refer to: operation is going to refer to one of the four basic arithmetic operations.1317

leftOp and rightOp are going to refer to left and right operands of an arithmetic operation.1323

We are going to let operation take on one of four different values: it can be add, subtract, multiply, or divide.1329

And in this case, we are using these abbreviated strings.1337

And so, what is going to happen is: if it receives a leftOp and a rightOp variable from GET,1341

and it receives an operation that is one of these four values, it is going to perform that operation on these values.1347

Now, what I would like you to do is have it output an error message if no operation is provided1353

(so operation would be equal to null, if we are using the short variables that we have been using with the ternary operator),1358

it is going to output an error message in both the leftOp (and that is the keyword here, AND, which is going to imply a logical operator),1366

AND rightOp are NOT provided (NOT also--that is going to denote a logical operation.1372

Then, it will also output an error message if you provide an operation value, but is not one of the four that are allowed.1382

And then, finally, if the user tries to divide by 0, I want you to output a message.1390

Your solution is going to look similar to what we did today with the nested.php example.1395

in that you should be using nested if, else, and switch statements, and your switch statements should make use of the default case.1399

That will allow you to achieve all of the functionality we have just described.1406

And then, what you need to do is test each of the different cases.1411

Supply some different variables, leftOp, rightOp, and operations; you are going to test1414

that all of the different operations work (addition, subtraction, multiplication, and division),1419

along with providing different variable leftOp and rightOp numbers to make sure that they are performing the operations.1423

The other thing that you are going to do is: you are going to try to divide by 0, to make sure that you get that divideByZero error.1431

And then, after you have done that (or if you are having problems figuring out how to get it),1436

I want you to go ahead and add debug statements to each conditional structure, like we did in our example today.1440

So, once you enter each if statement, add a debug statement that says "debugging if statement number 1."1445

And then, also do that for each case in the switch statement.1451

So, for example, if you hit the case add when you are comparing the variable operation against a number of cases,1453

and you compare it against add, when it hits that case, you would output a debug statement saying "performing the addition operation."1462

That will give you practice using the debug statements.1470

And then, just one other note is to be sure to indent your code properly, and make sure you are not missing any curly braces or break statements.1473

And just follow the coding conventions that we have talked about in today's lesson.1481

Thank you for watching; I look forward to seeing you next time.1486

Educator®

Please sign in to participate in this lecture discussion.

Resetting Your Password?
OR

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Membership Overview

  • Available 24/7. Unlimited Access to Our Entire Library.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lecture slides for taking notes.
  • Track your course viewing progress.
  • Accessible anytime, anywhere with our Android and iOS apps.