Matthew M.

Matthew M.

Introduction to Conditional Loops

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!

Introduction to Conditional Loops

  • A while loop is a conditional control structure that executes a statement group repeatedly as long as its specified test condition remains TRUE .
  • A while loop’s test condition’s value is compared to TRUEbefore each execution of the loop’s statement group.
  • 'Looping over arrays' is a common programming function, and PHP provides several built-in functions for doing so. They work on the basis of an array cursor , which is a 'marker' for the 'current' array element:
    • current() – returns the value of the array element at the current array cursor position
    • key() – returns the key of the array element at the current array cursor position
    • next() – advances the array cursor by one
    • prev() – moves the array cursor back by one
    • reset() – sets the array cursor to the 1st element
    • end() – sets the array cursor to the last element
  • The list() construct and the each() function are also used to loop over arrays.
  • list() is used to assign values to multiple variables at a time from an indexed array.
  • each() returns key/value information for the current array element in an array and advances the array cursor by one. It returns FALSE if the end of the array is reached.
  • Unlike some programming languages, PHP does not have ‘block-level’ scope used with control structures.
  • Additional Resources:

Introduction to Conditional Loops

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:08
    • Lesson Overview
  • while Loop 0:48
    • Definition
    • Syntax and Usage
    • Coding Example
  • Looping Over Arrays 13:16
    • Looping Over Arrays
    • Coding Example
  • Looping Over Arrays (cont.) 16:43
    • Internal Array Pointer
  • Array Traversal Functions 17:29
    • current ()
    • next () and prev ()
    • reset () and end ()
    • key ()
  • Indexed Arrays: Using current () and next () 20:40
    • Indexed Arrays: Using current () and next ()
  • Associative Arrays: Using key (), current (), and next () 24:06
    • Associative Arrays: Using key (), current (), and next ()
  • Array Traversal Functions (cont.) 29:41
    • list () Construct and Example
    • each () Construct and Example
  • Lopping Over Arrays Using list(), each() 42:13
    • Lopping Over Arrays Using list(), each()
  • Control Structure Scope 50:37
    • Definition and Example
    • Control Structure Scope Coding Example
  • Coding Conventions 54:20
    • Coding Conventions
  • Homework Challenge 54:58
    • Homework Challenge

Transcription: Introduction to Conditional Loops

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

In today's lesson, we are going to be introducing the topic of conditional loops.0004

Specifically, we are going to be introducing the most fundamental conditional looping structure, which is known as the while loop.0010

We are going to talk about looping over arrays, which is basically going over the elements in array and performing certain actions on them.0018

And this is an extremely common task that you will use throughout all of your PHP code.0026

We are going to talk about some functions used in looping over arrays, which are array traversal functions.0030

We are going to talk about the topic of control structure scope, which is another discussion of variable scope.0037

And we are going to go over some coding conventions as they apply to while loops.0045

A while loop is essentially a conditional control structure; it is kind of like an if statement or an else statement.0050

And what it is: it has a statement group, or a group of statements that get executed when a certain condition is met.0055

Unlike the if statement, though, or the switch statement, it allows the statements within its statement group to be executed repeatedly.0063

So, what happens is: for example, here is what a while loop looks like.0072

It has the while keyword; it looks very similar to an if statement; and it has a test condition.0077

And what it does is executes the statements within its statement group, if the test condition evaluates to true.0082

And that is the same type of test condition you can use for an if statement or an elseif statement.0090

The difference is that, unlike an if statement, once the statement group is executed, it doesn't move down to the next line of code.0094

It actually goes back to the top and reevaluates the test condition.0101

If the test condition is still true, it goes ahead and executes its statement group all over again.0106

And it does that repeatedly until the test condition equals true.0112

The key thing to note is that before it executes the statement group is when it evaluates the test condition.0116

And before re-entering that statement group, it reevaluates the test condition on each what is called "loop" of the structure.0122

While loops typically use what is known as a counter or a boolean flag that gets altered within the loop statement group0133

in order to end the execution of the loop.0141

I'll clean this up.0147

So, because this loop gets executed repeatedly until the test condition returns false,0148

you need to have some way of making that test condition false.0156

And the way you do that is by altering some variable within your statement group, so that test condition will equal false, and that loop will stop.0159

Otherwise, you will get stuck in what is known as an infinite loop, which we are going to be talking about in another lesson.0172

Now, one thing to note, also, is that like an if statement, the statement group of a while control structure only gets executed while the test condition is true.0179

So, if we had a while statement where we set the test condition equal to the literal value false,0192

then, just as an if statement, if we had done the same thing,0205

whatever statements are within that while statement group are never going to be executed.0208

Let's go take a look at a script called while.php; and the first thing we are going to introduce is what is known as a loop counter.0215

And a loop counter is something that you can use to know when to end the execution of the loop.0226

What we are going to do is create a loop that is going to output the numbers 1 through 5.0233

And what it is going to do is: each time the loop goes through, it is going to increment a counter variable by 1.0240

And then, the next time that loop goes through, it tests to see whether that counter variable is less than or equal to 5.0247

And if so, it continues to output until the incrementing of the variable makes the test condition false.0256

So, let's take a look at what that actually looks like: it's a little easier to see in action.0262

We are going to define a loop counter variable; and the most common loop counter variable that you will see0266

in all sorts of PHP scripts, and in all programs, actually, is called i.0272

And we are going to set that equal to 1.0279

So now, we have created a variable called i; it is equal to 1; and then, in our while statement, we are going to have a test condition0284

that says, "As long as i is less than or equal to 5, I want you to keep executing the statements within this while statement group."0290

And what we are going to do within our while statement group is just simply echo the counter, the variable i.0300

So (let me finish typing this), for example, when this loop first executes, i is going to be equal to 1.0307

It is going to go up to the while; it is going to check the test condition; is 1 less than or equal to 5? Yes.0317

So, it is going to enter the statement group; here, it is going to output i, whose current value is equal to 5.0322

And then, the key here is that the next step that we do is to increment the value;0327

and we are going to use the increment operator that we learned about in our lesson on operators.0331

What that is going to do is increment i, so that after this first while loop is executed for the first time,0337

at the end, i is going to be equal to 1; but then, when we increment it, it is equal to 2.0345

It is going to go back up to the top; it is going to say, "Is 2 less than or equal to 5?"0349

That is going to evaluate to true, so it is going to enter again; it is going to output 2.0352

It is going to then increment it to 3 and go up to the top and test "is 3 less than or equal to 5?"0357

Yes; it is going to output it.0362

It is going to do that each time until, once i is incremented to 5, it will say "Is i less than or equal to 5?--true."0364

It is going to output 5; and then, when we increment it, i is going to be equal to 6.0372

So, the next time that this while loop is entered, it is going to say, "Is 6 less than or equal to 5?"0377

It is going to return false, and then you are going to essentially jump out of the while loop.0381

So, let's go ahead and run this code and take a look at it in our browser.0387

And you can see, what it has done is output the five variables 1, 2, 3, 4, and 5.0396

Now, the other thing you can do, for example, is: let's say we wanted to output ten numbers--0400

you could just change the test condition to 10, so that it executes ten different times.0406

And then, we are going to get the output 1-10.0411

Now, the other thing that was mentioned was that you can also use what are known as boolean flags in a while statement.0418

Here, in this while statement, the while loop ends any time that i is less than or equal to 5; we return it back to its original value.0426

Well, you also can use a while statement to just test a boolean variable.0435

If the boolean variable is true, it goes ahead and evaluates the loop; if it's false, it doesn't.0440

So, what we are going to do is...before we do this, I want to introduce a function.0446

If we go to php.net, there is a function called rand that generates a random integer.0448

And what you can do is supply it with two arguments, a minimum integer and a maximum integer.0456

And when you call that function, it is going to return a random integer within that range specified by min and max.0463

So, what we are going to do is: in our while loop, each time the while loop runs, we are going to be generating a random number.0473

And we are going to be testing to see if the number is over 90.0480

And if the number is over 90, then we want to exit out of the loop.0484

And this is just a test example--something you can do with a while loop--and we are going to use a boolean flag to do that.0489

What we are going to do is create (actually, first I am going to comment this) a boolean flag.0497

And boolean flags, or boolean variables, are often started with the word is, because it's "Is it true? Is it false?"0505

And we are going to call it isOver90; the name implies, "Is 'over 90' set to true or false?"0513

We are going to set it to false initially; and then, in our while condition, we are going to say,0520

"We want to continue to generate random numbers until we generate a number that is over 90."0528

The way we do that is: we use the NOT operator on isOver90.0533

So, what this is going to do is: every time this loop executes, when it comes back up to the top and evaluates the test condition,0540

it is going to perform the NOT operation on isOver90.0546

So, as long as isOver90 is still false, this test condition will evaluate to true, and it will continue to execute the loop.0551

So initially, because we set isOver90 to false, NOT false is going to equal true; the loop is going to be entered initially.0559

Now, within the while loop, we are going to generate a random number, just called randomNum.0567

And we are going to generate a random number in the range 1-100; and this is using the rand function.0573

And then, what we are going to do is output the random number that is generated in each execution of the while loop.0580

Now, this loop is going to continue, because isOver90 is initially set to false, indefinitely,0598

until something acts to change this variable isOver90 to true, which will make the test condition evaluate to false.0606

So, what we can do is add an if statement that is going to test the value of randomNum on each execution of the loop.0612

And it is going to test of randomNum is greater than 90; then, we are going to change the value of isOver90 to true,0627

because now it is true; we have generated a random number that is over 90.0638

When this function eventually generates a random number over 90, (which it eventually will--that is the nature0646

of a random number generator), isOver90 will be set to true.0651

And then, when it goes back up here to evaluate the test condition, it is going to say NOT true an evaluate to false.0656

It is going to jump out of the while loop.0660

And then, at the bottom here, after it exits the loop, we are going to output a message that says "a number greater than 90 was generated."0662

If we go ahead and run this script again (oops, it looks like I had a typo)...0672

I'm going to go ahead and comment out this section up here, as well, so we don't see that output.0689

I'll double-check things; it seems like we are having a problem here.0715

I'll make sure I saved the page; oh, there it is--it just took a while to run.0731

As you can see, when this boolean flag example ran, in this case, it ran through several numbers before it generated a number greater than 90.0738

As you can see, the last number generated is equal to 98, which is going to set that isOver90 flag equal to true.0749

And then, we are going to output that the number over 90 was generated.0755

Because of the nature of the random function, this loop is going to run a different amount of times each time the while loop is run.0758

So, if we refresh the page, we are going to get a different set of numbers.0766

In this case, it generated a number greater than 90 on the first call to the rand function.0769

If we do it one more time, we can see that it took 1, 2, 3, 4, 5 tries to get a number greater than 90.0774

This is an example of, instead of using a counter, using a boolean flag that you change the value of to true or false,0782

within your while loop, based on a condition that occurs.0789

One of the most common things that we are going to be using the while loop for,0798

and loops in general (we are going to be learning about some more in future lessons), is to loop over, or iterate over, arrays.0801

Basically, what that does is: you have an array that has maybe 5 different elements in it,0809

and you want to perform maybe some function on each element in the array (for example, output it).0813

Well, while loops allow you to loop continuously over an array, performing actions on each element.0818

And it is an extremely common programming cast.0824

One way to do this is to make use of a loop counter, which we had just talked about.0829

Let's go look at a file called loopingOverArrays.php.0834

Here, we declare an indexed array (at the beginning) that just contains the integers 1, 2, and 3.0839

And we are going to create a while loop that is going to loop over the array, and what it is going to do is output each element of the array.0846

So, what we can do is: we are going to declare a loop counter, and we're going to set it equal to 0 (and I'll explain the reason for that in a second).0851

And then, our test condition is: we are going to say, "As long as the loop counter is less than count($indexArr)..."0861

Now, the count function returns the number of elements in an array, and we know indexed arrays start with a 0 index.0870

So, if there are 5 elements in an array--or, here there are 3 elements in this array--count is going to return 3.0882

So, it is going to say, "As long as the counter is less than 3, I want you to go ahead and run the loop."0890

Since the counter is initially set to 0, it is going to execute for the value i=0, i=1, i=2.0895

When i equals 3, it is going to jump out of the loop.0902

So now, what we are going to do is: within the loop, we are going to output the particular element that we are going to access using the i counter.0909

For example, we are going to output (it looks like I have a typo here) indexArr,0919

and using the square bracket notation with some double quotes, what we can do is:0932

within the square brackets, for indexArr, we can include the variable i, which is our counter.0937

And what that is going to do is say, "OK, I want you to echo the value of indexArr that has the index i."0942

And because arrays are 0 indexed, and they start with 0, it is going to, on the first iteration of the loop, output the value with index 0,0951

which is going to be 1; the second iteration--it is going to output 2; and so forth.0959

And then, the key thing that we need to do is to increment the loop counter.0969

Otherwise, the loop will just continue to run indefinitely, because i is going to be set to 0, so it is just going to keep going over and over again.0973

So, if we go ahead and run the script, we can see that it output 1, 2, and 3;0981

so, it has looped or iterated over the array and output each value of the array.0990

And the way we did that, in this case, was using a loop counter.0997

Now, there are other ways to loop over arrays in PHP; and PHP actually has a bunch of built-in functions that are useful1004

for working with and looping over arrays.1010

And the functions work on the basis of what is known as an internal array pointer, or an array cursor.1013

Essentially, that is a marker that says where in the array the cursor is located at.1020

For example, when you initially create an array, the internal array pointer (or the array cursor) points to the first element in the array.1026

Then, there are different functions that you can use to move the cursor.1034

For example, you could use the next function to move the cursor down one;1037

so now, the current element pointed to by the internal array pointer is the second element.1040

Let's take a look at some of the functions that do this.1046

The first function we are going to talk about is called current; and what that does is returns the value of the array,1052

at whatever position the array cursor is currently located at.1060

So, when you first declare an array, if you were to run the current function on that array (and all of these functions take1063

an array as their argument)--if we called current and passed it the variable sample (which down here, you can see, is an array1071

containing 5 integers), if we had just declared sample, the array pointer is going to point at the first element of the array;1081

so, when we run current, it is going to return the value at whatever element is pointed to by the array cursor.1088

So, it is going to return this first element.1095

Before I talk about key, I want to talk about these other ones.1097

There is also the next function and previous function, which advance the cursor or move it back by one.1101

When this array is first declared, we then ran next on the array.1108

Now, initially the array cursor pointed here; after we run next, the array cursor points to 2.1119

So, if we were to run current on this array (let's say we were to echo it),1124

current is going to output the value of the element currently pointed to by the array cursor.1136

Since we have advanced it to 2, this is going to output 2.1142

I'll clean this up.1146

And the same functionality works for the previous array, as well, except maybe if we had run next a couple of times,1152

so the cursor had been advanced out here to 3, we could use the previous function to move the cursor backwards.1159

Two other functions that move the cursor around are the reset and end functions.1165

The reset function returns the cursor to the first element in the array.1171

And the end function, as the name implies, sets the cursor to the last element in the array.1175

Now, when working with associative arrays, we can make use of the key function.1181

which is an analog of the current function, in that (for associative arrays, they have keys)1186

it will output whatever the key is for the current element that the array cursor points to.1192

And actually, it is not just for associative arrays; these indexed arrays have keys, as well.1199

For example, this index is 0; this is 1; this is 2.1205

When this array is first declared, if we echoed the result of the key function on sample,1209

and if we did it immediately after we declared this sample array, it is going to output the value 0,1225

because the key of the current element pointed to by the array cursor, which is the key of the first element in the array, is 0.1231

So, let's go and take a look at some of these functions for looping over arrays.1241

We have a file called loopingOverArrays2; and the first thing we are going to do is loop over an indexed array.1245

And here, we have created an indexed array, the same as before, with the numbers 1, 2, and 3 in it.1250

And what we are going to do is declare a loop counter, so we know how many times to loop over the different arrays.1258

We are going to loop over it as many times as there are elements in the array.1265

So, we are going to create a loop counter again and set it equal to 0.1268

And we are going to use the same test that we had before, which is:1273

we are going to test if the loop counter is less than the length of the array we are looping over.1279

Now, what we want to do is retrieve the current value on each iteration of the array.1287

So, when we initially start this array, the array cursor is going to be pointing at the first element in the array, which is going to be equal to 1.1294

So, when we run current on the first iteration of the loop (we run current here on indexArr),1302

it is going to return the value where the current cursor position is, which is going to be 1.1309

Then, we are going to go ahead and output this.1315

What we have done is: when we enter this array, i is going to be equal to 0.1341

And what we are going to go ahead and do is: on the first iteration, we are going to get the value of the current element,1346

which is going to be this first element, 1.1352

So, we are going to have value, and then we are going to output "current value=" in this case "1."1354

Now, instead of incrementing a counter, because we are not accessing the elements in the array using a counter--1361

we are not doing what we had done before in this example, where we access the elements of the array to output them1371

using the counter as the index into the array--and we are going to make use of these new array traversal functions,1377

we are going to go ahead and increment, or advance, the array cursor by 1, using the next method.1384

What this is going to do: calling next on indexArr is going to advance the cursor by 1.1391

So, after this loop runs for the first time--after it outputs the first value--the cursor is going to be incremented by 1.1396

So now, the cursor is going to point to the second element in the array.1402

And the reason here that, even though we are not using the loop counter to access the array, we are including it,1407

is because it is going to be used to define how many times we loop over this particular array.1411

If we go ahead and look at the output of this script, we can see that output($currVal) equals 1, 2, and 3.1418

This made use of the current function and the next function for looping over indexed arrays.1428

I'll go ahead and comment out this section, and we are going to do the same thing for associative arrays.1438

Here, we have defined an associative array with three key/value pairs,1447

the keys being strings 1, 2, and 3, and the values being the integers 1, 2, and 3.1454

And then, we are going to loop over the array as many times as there are elements in the array.1462

So, we are going to be declaring a loop counter again; we are going to be using the same test,1466

except we are going to be counting the number of values in the associative array.1470

We are going to be incrementing the loop counter at the bottom.1474

And at this point, what we are going to do in each loop is retrieve1478

the value of the key and of the value of the value of the current element pointed to by the array cursor.1481

And the way you do that, to get the key, is use the key function (which we had talked about).1492

So, running a key on assocArr is going to return the key that is currently being pointed to by the array cursor.1497

Since we just defined this function previous to the loop, the array cursor is going to point1505

at the first element, which is going to be this key/value pair right here.1511

So, key, in this case, on the first iteration, is going to return the value 1.1515

Then, just as before, we can use the current function to get the current value.1519

And for this first iteration of the loop, it is going to be the value equal to 1.1524

And then, we are going to output that; and this is just going to be a formatted output.1528

It is going to say that the associative array that has the key currKey has the value currVal.1551

Then, you can see: what we do as we go ahead as we did before is: we move the array cursor up one,1566

so that, when we go to the loop the next time through, it is going to access the key here and the value here of that next element.1571

And then, we are incrementing the loop counter by 1, so that this loop eventually ends when we reach the number of elements in the array.1582

We go ahead and save this and run the script again.1590

We can see that we have gone ahead and looped over this array called assocArr.1596

And then, the key 1 is associated with the value 1; 2 is associated with 2; 3 is associated with 3...as we expect.1600

And this is making use of the key, current, and next array traversal functions.1607

One thing to notice is: let's say we actually commented out this next statement within this loop; what is that going to do?1613

Well, we are always going to run through this loop (in this case) three times, because i is going to take on the values 0, 1, and 2.1622

Once it equals the value 3, it is going to kick out of the loop, and that is because we are incrementing it at the end of each loop.1630

However, because in this loop we never advance the array cursor by 1, and the cursor is initially set to this first element in the array,1636

which is the key/value pair "the string 1=1," this loop, each time it goes through,1647

is going to just output three times that initial key/value pair.1653

The reason is because we never advanced the array cursor.1657

For example, if we comment it out and run it again, we are going to see that we get the same output three times.1659

That shows the importance of making sure that you use this next function to be able to advance the array cursor.1669

Another thing to note is: let's say we were to run the same exact loop all over again--run it twice.1677

So, it is the same thing, and now we are going to run it a second time; well, let's see what happens when we go ahead and output the script.1688

We are going to see that, the first time it goes through, it outputs 1, 2, and 3, as expected.1700

But the second time through, it doesn't output anything.1704

And what is the reason for that? Well, the reason is that after this first loop occurred, we had run the next function on assocArr three different times.1707

So, by the time this loop ends, the array cursor, which had started here, had advanced to one past the last element in the array.1720

So, when we go down here and try to retrieve the key and current value of the element that the array cursor is pointing to,1730

it is not going to point to anything, because the cursor has gone on beyond the end of the array, which is why nothing gets output.1739

Now, the way we could solve this is, as we learned about, the reset function.1748

We can reset the associative array, and what that is going to do is put the array cursor back to the first element in the array,1753

so when this runs the first time through, the array cursor is going to be pointing at this first element.1762

So now, we should just see our output doubled.1769

If we go ahead and refresh it, we can see that the same thing happened over again.1772

Now, there are two other functions that are used often, and more frequently, actually, than the last ones that we just went through,1783

to loop over or traverse or iterate over arrays.1789

One is not a function; it is a language construct called list; it sort of acts like a function.1794

And then, there is an actual function called each.1799

Now, before we talk about how to use these to traverse over arrays, I want to explain how they work.1804

The list construct is used in this way: let's say we have declared an array sample with three values in it.1809

Well, what we can do is use the list construct: we type list and then an opening parentheses,1817

and then we can put in a comma-separated list of different variables.1823

And when we set this list construct equal to an array, what it does is populates1829

each of these variables specified within the parentheses of list with the values of this array.1835

And it does so...the first value specified in this list construct only works on associative arrays,1842

so the first variable we specify to list is going to be set to whatever value is in the indexed array sample that has the index 0.1849

So, in this case, variable 0 is going to get set to a.1861

And then, the second variable listed is going to access the value in the sample array with the index 1.1865

So, b has that index; so 1 is going to get set to b, and then 2 is going to get set to c.1874

Now, if we go and look at listEach.php, which is a script, we have 2 arrays defined at the beginning: indexArr and assocArr.1885

This one just, in this case, contains 2, 4, and 6; and then, this one is the same as we saw in our last sample file.1898

So, what we have here is a list statement that has three variables, and we set the list construct equal to the indexed array.1904

So, as we just learned, what is going to happen is: 0 index is going to be set equal to 2; 1 index is going to be set equal to 4;1913

and then, 2 index is going to be set equal to 6.1921

So then, if we go ahead and echo each of these different variables, we should see that they are set to the values 2, 4, and 6, as expected.1925

If we save this file, and we load it up, we can see that 0 index, 1 index, and 2 index are set equal to 2, 4, and 6.1934

Now, when using the list function, you don't have to specify as many variables as there are values in the array.1946

For example, let's say we just want to grab the first two values from this array, the one at the 0 index and the value at the 1 index.1956

When we run this function, only these two values are going to get set.1966

We run the script again; well, first of all, if we look at this output, it says 0 index, 1 index, 2 index...1972

we are going to see that it outputs 2 and 4, because 0 index and 1 index were able to be set.1980

However, because we never actually set the variable 2 index, and we are trying to echo it in our echo statement down here, we get that undefined variable error.1986

And one other thing to note is: let's say we just want the first and third variable in the array.1998

Well, you can do that with list by just not putting anything in for the second element.2005

For example, if we want the value of the array at the 0 index and the 2 index (which would be the first and third element),2011

and we don't want the value at the 1 index; in list, we can use a comma-separated list that just adds an extra comma2018

and leaves out any variable for the second value in the array.2029

In this case, 0 index is going to get set to 2; 4 is going to get set to nothing, because nothing was specified.2037

And then the variable 2 index is going to get set equal to 6.2043

So, if we don't try to get an error down here again...now, when we rerun the script, it still has one index in here, but2047

we can see that (I'll go ahead and correct that, so that it looks better)2061

we successfully used list to just extract the first and third values of the indexed array.2068

So here, we can see: the 0 index is equal to 2; the 2 index is equal to 6.2075

Now the each function--to talk about that: what it does is: when you run the each function on an array, the parameter you pass it is an array.2081

It returns an array that contains information about the current key/value pair of the array.2092

So, wherever that array cursor is pointing to in the array; when you run this function, it is going to provide you back an array2098

that has information about the key and value of the current array element.2105

The other thing that it does (and you can see the & here) is that, after it does that, it advances the array cursor by 1.2110

So, after you have run this function, it is the same thing as if you extracted the key/value information from the current element,2117

using maybe the key and current functions, and then ran the next function on the array.2123

So, each sort of has the same functionality as that, built into one.2134

And one other thing to note is that, if each is at the end of an array (meaning that you run each on an array2141

where the cursor is pointing to the last element), it is going to return false.2149

And we will see how that will be useful in a second.2153

So, when this each function runs on the sample array (let me erase some of this, so it's easier to see)--2155

the sample array contains the strings a, b, and c--it is going to return a value that, as mentioned, is an array.2162

And the array is going to have the form of an associative array, so each return is going to be an associative array that has several key/value pairs.2170

When this function here, each, runs, it returns an array, and it's an associative array that has 4 elements in it.2217

It has 2 elements that correspond to the value of the current element (in this case, the value associated with the index 12225

in the array that each returns is set to the value of the current element--in this case, 1).2240

You can also access it because it creates a key called value, and it sets that equal to the value of the current element.2246

And then, it creates two other elements in the array that it returns: one with the key 0, and one with the key "key."2252

And that returns the key of the current element--in this case, assuming we had run this immediately after sample was declared,2262

the value returned is a, because the cursor points to a.2268

And the key of an indexed array is the numerical index, and in this case, it's 0; so, as we can see, this is what this returns.2272

Let's go take a look at each in action.2280

What we are going to do is: we have our associative array up here that has the three key/value pairs in it: 1, 2, and 3.2292

And we are going to loop over that associative array, and what we are going to do is run each on that array every time this loop occurs.2301

Now, as we just learned, what that is going to do is: this function is going to generate an associative array2319

that contains information about the key/value pair of the element that is currently being pointed to.2326

And it is also going to advance the array cursor by 1, so when we run each again,2333

it is going to get the next element in the array, because the cursor is going to advance.2339

Then, what we are simply going to do is print out the each return, which is the array returned to this, so we can see what information it contains.2343

And then, we are just going to increment the loop counter by 1; and that is in order to make sure we loop over the entire array.2354

If we go and run this function again (let me comment out this first part), and we reload the page,2361

we can see that, on each iteration of the loop, we ran the print_r function2380

on the eachRtn array, which was the array returned by the each function.2384

And as we can see, as we saw in the slide, it's an array that contains four elements, four key/value pairs.2389

The first key is the number 1, and that is the value of the current element.2399

It also has a key called value, which also echoes the current value of the current element.2404

And then, it has two keys: 0 and the key named "key"; and that outputs the current value of the current key.2409

And so, as you can see, each time this is gone through--the first time, the value output was 1, because that was the first value in the array.2418

And it output the key; it set the key equal to the variable 1, because if we look back at our array, the key is the string 1.2425

And then, the second time it went through, each return array contained the value 2 as the key and the value 2 as the variable.2437

And if we look, we can see that that happens here, for both 2 and for 3.2444

One thing to note is that...let's say we go ahead and run this loop one extra time, and we add a +1 here to the end.2449

That means that it is going to try to run the loop after it has processed all of the elements in the array.2458

Well, we learned that each returns false whenever it reaches past the end of a particular array--when the array cursor moves past the end of an array.2464

So, when we go ahead and print each return here, each return is no longer going to be an associative array; it's going to be the value false.2476

And as we will see when we print it out, it is going to show that false was actually output.2482

And we are going to have to test for that, because false, when converted to a string, is just the empty string.2487

So, if each return is true, we are going to echo the array, or print out the array.2496

If each return is false, we are just going to output the string "false."2506

When we run this one more time, we can see that, on that fourth iteration of the array,2520

each returned the value false; and so, here we have output the value false.2527

Now that we have learned about how each and list work, we can use that to traverse or iterate over an array.2537

If we go to this new file called listEach2.php, first we are going to loop over an indexed array.2544

Here we have created an indexed array with the values 2, 4, and 6.2550

Because we know the importance of...if we want to loop over the whole array, we want to make sure that the array cursor points2554

to the beginning...we run reset on the array originally, even though we just defined it; it is a good practice to do.2559

We are going to define a variable to hold the current array index; and we are going to explain that in a second.2566

What we can do is: because each returns the value false when it reaches the end of the array,2577

we can use that as part of our while test condition, because it evaluates to a boolean variable.2582

What we are going to do is use the list construct in combination with the each function.2589

So, if we type list...if we type this statement here, now what is going to happen is: when this while statement first gets executed,2596

it is going to test the test condition, and the first thing it is going to do is run this assignment operation.2616

And that is going to say, "Run the function each on indexArr," and it is going to,2624

as we know, return an array, assuming that the cursor is pointing at a valid array element.2629

And then, it is going to set (as we know from the list function) this first element of list equal to whatever has the index 0 in this returned array.2636

And in this case, it's an indexed array, so it is just going to return either 0, 1, or 2.2649

And then, in the second part, we have list set to val; and what that is going to do is: because this is the setInt argument2658

to the list construct, it is going to take on the value of whatever the indexed array returned by each has at index 1.2667

In this case, key we are just going to leave blank, which you will commonly see, because in the indexed array, we are just concerned with the val.2677

And val is going to get set to whatever the value is at the current cursor position.2685

What we can do is output, on each run of the loop, the value that it pulls from the array.2692

Actually, we don't...let's ignore this array index...2706

What is going to happen is: each time this loop runs, it is going to pull the current key/value information from the indexed array.2714

It is going to advance the array cursor by 1; it is going to set value equal to the value in the array returned by each that has the index 1...2723

which, as we learned from our previous example, the index 1 refers to the current value, and then the index 0 refers to the key.2736

Because we are not concerned with the key, we can just leave this part blank.2748

And normally, this would be filled with whatever value is associated with the 0 index of the array on the right side of the equal sign of the list operation.2752

So, when we run this, what it is going to do is loop over indexArr and output each value.2762

Oops, that is a different script.2772

And so, you can see, what it does is: when it looped over the indexed array, it output the values 2, 4, and 6,2781

which, as you can see, were the three values in this array.2786

Now, where you will more commonly see this list(), each() combination is in looping over associative arrays.2789

Let me comment out this section up here.2797

And here, we have declared an associative array with three elements in it--three key/value pairs.2804

We reset the cursor to the beginning of the array; and now, we are going to use the list construct,2810

along with the each function, to get both the key and the value from the current array cursor position.2816

When we run each on assocArr, every time this test condition is evaluated, it is going to return an associative array.2827

that has four elements in it: one of them has the index 0; one has the index 1.2836

The one with the index 0 is the key of the current element in the array, and the one with the index 1 is the value of the current element in the array.2842

Because we know list, when you specify two variables in the list construct, the first value gets set,2850

and the first variable gets set to the value of the array that is at the 0 index on the array specified to the right of the equal sign,2859

and the second element gets set to whatever value is in this array at the 1 index,2870

key and value are both going to get set to the output of each.2876

Additionally, the array cursor is going to get incremented by 1, so when we reach the end of the array, each is going to return false.2878

The value of this assignment statement is going to be false, and the while loop is going to end.2884

What we can do is output each key/value pair; so let's see, I'll call it...2889

And all this echo statement does is: it is going to output the string assocArray that is going to represent our variable.2914

It is going to include in the square brackets the key that we pull from the associative array.2921

And then, it is going to output an equal sign with the value that we pull from the array.2925

If we go ahead and run this script again (reports error...it looks like I added an extra parentheses in here),2931

you can see, what it did is looped over the associative array.2950

It output the key/value pair for each element in the array; the key 1 has the value 1, the key 2 has the value 2, and so on.2953

Now, one thing that we could do to also show you how these different array traversal functions work:2968

Let's say, instead of calling reset at the beginning of this loop, we called end.2976

So, the array cursor is going to actually point down here at the last value in array, which is going to be 3.2980

Well, when we go to this while statement, when we run this each function, it is not going to return false;2987

it is going to return the key/value information associated with the last element in this array.2993

But then, when it goes through a second time, because the cursor is advanced past the end of the array,2998

each is going to return false, and so the loop is going to exit.3004

So in this case, only the third element is going to get output; so if we refresh the page, we can see that assocArr equals 3.3007

Another thing you can see is: let's say we, instead of specifying end here, specified next,3016

which is going to initially set the array cursor at the second element in the array.3021

Then, this loop is going to actually only output the last two elements.3029

If we refresh, we see that it outputs 2 and 3.3033

One thing I want to talk about is control structure scope, or what some languages call block-level scope.3039

And basically, what I am going to be talking about is that PHP doesn't have that.3045

In PHP, there is only global scope and local function scope, which we have talked about.3048

In some languages (and the reason I am explaining this is because some of you might have experience with other programming languages),3053

if you declare a variable...let's say this is an entire PHP script: this variable, ifVar, is first declared within this if control structure.3058

Well, in many programming languages, if a variable is declared within a control structure, it is not available outside the control structure.3070

So, for example, if we declared ifVar equals 1 within this if statement, if we tried to echo it out here, it would say the variable was undefined.3079

That is, in other programming languages...PHP doesn't have that...what is known as control structure, or block-level, scope.3088

If you declare a variable within a control structure, it is in the global scope.3095

Because this is true, we are always going to execute this if statement; ifVar is always going to get set equal to 1.3100

And then, because ifVar doesn't have scope restricted to the control structure--it has global scope--3106

this is going to output 1, because it is going to know the value of ifVar, because it was declared in here.3113

If we go and actually look at the file called scope.php, where we have the same code, and we go ahead and run it,3121

we can see that it outputs the value 1.3134

One thing to note is that...let's say this was false, so ifVar never got set to the value equal to 1.3136

Well now, what is going to happen is: when we try to access ifVar down here, it is going to say that the variable hasn't been declared,3146

because it never actually reached this declaration statement, and it wasn't declared prior to this if statement in the file.3151

If we go ahead and run this file, we are going to say "undefined variable ifVar."3159

So, even though you can declare variables within control structures and access them outside of the control structures,3164

it's a common, and I consider, a good practice to go ahead and always declare your variables outside of the control structure,3172

and at least set them to some default value (in this case, we will set them to the empty string).3181

Now, what that is going to do is: then, when we try to access ifVar down here, if it doesn't get set in this if statement,3185

which it is not going to, because the test condition is false, it is just going to output the empty string, and we are not going to get the error.3190

Now, the other reason for doing that is that, if you are working on a team of people,3196

everybody may not be familiar with how PHP works, and that they don't have local control structure scope.3202

And so, in those cases, those people are going to look at this and say, "Well, if this statement is missing right here, ifVar is not going to work."3207

So, by including the declaration outside of the control structure, those people are going to understand what is going on,3219

because otherwise, when they initially look at this, they are going to say, "Hey, wait a minute; this doesn't work."3233

So, it is also something to just increase readability among different people on your team.3237

So again, just to review that: PHP does not have any block-level or control structure scope.3245

Anything declared within a control structure, if it is declared in a global script file, is in the global scope, and is accessible outside of the control structure.3250

The coding conventions for while loops are going to look like this.3261

It is going to be basically the same as an if statement, except we have a while keyword instead of if.3267

We have a space in here between the opening parentheses of the test condition and the keyword.3272

There is a space here between the opening curly brace and the ending parentheses.3277

We have the statement group that is indented one indent further than the while statement.3282

And then, we have the last closing curly brace on its own line at the same indent level, here, as the while keyword; so these are on the same indent level.3288

For the homework challenge today, I want you to create an indexed array called lower.3299

And it is going to contain five lowercase letters that are just strings, a through e.3304

And utilizing a loop counter and the count function, which tells you the number of elements in array,3309

I want you to loop over the lower array, and while you do that, create a new variable called upper.3315

And what that is going to do is: each time you are in the while loop, I want you to change, using the strtoupper function,3323

the value of the current lowercase letter that you are looping over.3330

Change it to uppercase, and set upper equal to that value.3339

So, what you are going to do is: as you go through the while loop, you are going to be building up this upper array3343

that is going to contain all of the letters in lower, but it is going to contain them in uppercase form.3347

And then, go ahead and output upper (using print_r and var_dump) to confirm that upper actually contains the uppercase versions of the lower array.3351

And the reason for doing that is: it is going to give you practice in looping over array using a counter.3361

It is also going to show you that, when you loop over array, you can do different things to it; you can perform different actions.3367

Then, I want you to create an associative array called days that has the keys which are the 3-letter abbreviations3374

for days of the week (so for example, like MON and TUE for Monday and Tuesday).3380

And then, as the values, it is going to have the full name of the days of the week.3386

Then, this time, instead of using an array counter, use the list construct and the each function to loop over this associative array.3390

And create, in the process, a new array called daysReversed,3400

where the keys of this new array are the values of days, and the values of the array are the keys of days.3405

So, you are basically reversing the key/value pairs.3415

After you exit the loop, go ahead and output daysReversed to confirm that the keys and values were switched.3417

And you can use either the print_r or var_dump function.3424

And that is just to give you practice using this list construct and each construct for looping over associative arrays.3428

That ends today's lesson; thank you for watching Educator.com--I look forward to seeing you next time.3436

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.