Matthew M.

Matthew M.

For & Foreach 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.
  • 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!

For & Foreach Loops

  • A for loop is a type of conditional loop that requires three expressions in its definition:
    • the 1st expression is evaluated once and only once before the loop begins iteration
    • the 2nd expression is the test condition for the loop, whose value is compared to TRUE before each iteration
    • the 3rd expression gets evaluated after each iteration of the loop
  • A foreach loop is a special type of conditional loop used specifically for iterating over arrays. When using a foreach loop to iterate over an array, the array’s internal cursor is automatically reset by PHP before entering the loop and automatically advanced by one on each loop iteration.
  • foreach loops have two syntaxes. One is typically used for iterating over indexed arrays and the other for iterating over associative arrays.
  • Both continue and break statements work within for and foreach loops just as they do in while and do-while loops.
  • Additional Resources:

For & Foreach 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:11
    • Lesson Overview
  • for Loops 0:45
    • for Loops
    • Expression 1
    • Expression 2
    • Expression 3
  • Simple Example 2:27
    • Simple Example
  • Notes on for Loops 8:56
    • Notes on for Loops
  • Ending Loop Using Test Condition and Break Statement 10:06
    • Ending Loop Using Test Condition and Break Statement
  • foreach Loops 12:03
    • foreach Loops
  • Indexed Array Syntax 14:10
    • Syntax
    • Example
  • Associative Array Syntax 18:31
    • Syntax
    • Example
  • Coding Conventions 25:05
    • for Loops
    • foreach Loops
  • Homework Challenge 26:52
    • Homework Challenge

Transcription: For & Foreach 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 two new conditional loops that PHP provides: the for loop and the foreach loop.0004

We are going to be talking specifically about the for loops and what they are: they are similar, in a way, to a while loop.0012

We are going to be talking about foreach loops, which are specifically meant for looping or iterating over arrays,0018

which is an extremely common task in PHP code.0023

And then, foreach loops have two different syntaxes, typically depending on whether you are iterating over a numerically indexed array or an associative array.0027

We are going to go over both of those syntaxes, and then we are going to go over the coding conventions0037

that we are going to use in this course for the for and foreach loops.0040

As mentioned, a for loop is a new kind of loop we are going to introduce.0046

It is a conditional loop that works kind of like a while loop, except it is a little more complex.0050

It has three expressions that it uses to declare it; and this is an example of a for loop.0055

It has the for keyword, and then it has three expressions, separated by semicolons (and we are going to explain in a second what those all do).0062

And just as a while loop does, it has a statement group enclosed in curly braces.0070

And every time you loop over the for loop, you execute the statements in the statement group.0075

This expression here, expr1, is an expression that you evaluate once before the loop starts.0083

So, when you are (let me erase this) in whatever line of code is before this for loop, when that is done executing0089

(for example, you have a statement), and you begin to execute this for loop, expr1 gets evaluated: it always gets evaluated one time.0098

Then, what happens is: you have expr2, which is your test expression, which is just like the test expression in the while loop.0107

If that test expression is true, then you go ahead and execute the statements in your statement group.0115

Then, expr3 is an expression that is evaluated or executed at the end of each and every loop.0121

So, every time you have a test expression that is true, you execute the statements in the loop.0129

Then, you go ahead and evaluate expr3, which is typically something like incrementing a counter.0134

You re-evaluate the test expression, expr2; and if it is still true, then you go ahead and loop through it again.0140

This is a simple example of a for loop that is going to output the string of numbers "1, 2, 3, 4, 5."0148

And so, for your first expression here, we have a variable declaration, i, and we are setting it equal to 1.0156

So, when whatever statement is up here is done executing, and this for loop begins, the first thing it does is executes the statement.0163

What it executes: it creates a variable i, or if it is already created, it sets the variable i equal to 1.0170

Then, it evaluates this test condition, which is the second expression in a for loop.0176

It is going to say, "Is the value of i less than or equal to 5?"0183

Well, we just set it equal to 1, so that is going to evaluate to true.0186

Because that is true, it is going to execute the statement group, which, in this case, is just an echo statement.0189

And all it is going to do is echo the value of i.0196

Once it echoes the value of i (it is going to echo the first value, 1, here), then, because the loop has ended, it goes and evaluates expr3.0199

expr3, in this case, is an increment operation on the variable i.0211

So now, i that was previously 1...after the loop goes through a first time, i is now equal to 2; and that is on the second iteration.0216

One thing to note, importantly, is that this only gets executed when the loop first begins; it doesn't get executed on each loop.0226

So now, i is equal to 2 (let me clean this up here a little bit); it is going to evaluate the test expression again.0234

2 is less than or equal to 5; it is going to echo it out.0245

Again, it is going to increment i; i is going to be equal to 3; 3 is less than 5; it is going to echo it out.0248

It does so until i equals 5; then, when i equals 5, it is going to echo it.0253

So, we are going to have this string echoed; it is going to increment i a fifth time.0259

i is going to be equal to 6 now; then, when it goes ahead and evaluates this expression, 6 is not less than or equal to 5; it returns false.0265

The loop exits, and you jump out to whatever code was down here.0275

Now, this may look kind of familiar, because it is sort of the same structure that we used when we used a while loop with a counter.0278

And this same exact for loop could be written in a while loop, as well.0285

For example, let's say this was in a different script: we could declare a counter, like we have done before; we'll declare a counter i=0.0292

Then, we are going to create a while loop; and in our while loop, we are going to set the expression i less than or equal to 5.0302

Opening curly brace...echo i...and then, after we echo i, as we have seen in other while loops that we have used, we want to increment the counter.0313

So, we are going to increment i and close the curly brace; and this code here (actually, I made a mistake here;0328

this should be 1)--this while loop right here performs the same exact functionality as this for loop up here.0335

The for loop is a little bit cleaner; you don't have to include these increment statements within your while loop.0344

You don't have to include the declaration statement before the while loop.0349

So, when you are using these examples where you are going to be using counter variables, it is usually preferable to use the for loop,0352

because you can initialize your variable, create the test condition, and then also increment your counter, all on one line,0360

rather than, as here, in 1, 2, 3 different spots.0369

And so, again, this while loop is the same functionality.0373

Now, one important thing to notice is the semicolons here: you have to separate the first expression from the second expression0377

with a semicolon, as well as the second expression from the third expression with a semicolon.0385

And that is key: otherwise, you will get a syntax error, or a partial error, when you try to run your code.0390

Let's take a look at a for loop in a more practical application, looking at iterating over an array.0396

We have an example file called forLoops.php, and we are going to iterate over an indexed array.0402

And what you can see is: we have declared an indexed array here with just three strings in it: the characters a, b, and c, all in lowercase.0414

Now, what we have done here is: we have created a for loop, and we have created an initial counter, set equal to 0.0422

We have created our test condition that, as long as i is less than the count (or the size of the indexed array), the loop is going to continue to iterate.0428

And then, we are also going to increment i at the end of each loop.0437

So, what this is going to do is: it is going to loop over the indexed array.0440

It is going to start at element 0, and then it is going to increment i after each iteration, until it gets to 5.0444

Once it is equal to 5, it is not going to be less than the count anymore; you are going to jump out of the loop.0452

And then, all we are doing, simply, within the loop, is: we have a simple echo statement that is outputting a statement that says,0456

"The element of indexArr at this index here (we are just going to use i as the index, so the first time through, it's 0,0462

because as we know, indexed arrays start with a 0 index)...it's going to say indexArr[0] in Uppercase."0474

And then, we are going to access that value in the indexed array.0481

In the first iteration of the loop, we are going to access the 0 element of the array; I'm going to output it as uppercase.0487

What this is going to do is: this loop is going to be iterating over an array, and in this case,0494

it is going to be making uppercase versions of the letters in it and outputting it.0501

And so, this is an example of using a loop to iterate over an array to perform some functions on the elements in it.0505

If we go and run this code, we can see that it says indexArr[0] in uppercase is A; 1 is uppercase B; 2 is uppercase C.0511

And we have this statement here, "ending loop using the test condition."0524

And we are going to come back to that in a second; but what that is saying is that this loop ends because the test condition eventually returned false.0527

And speaking to that matter about the test condition, as we know, there are while loops and do-while loops.0539

You can use break statements to break out of loops, as well.0544

So, both the continue and the break statement work within four loops, exactly as they do as while or do-while loops.0547

A continue statement will have you jump back up to the beginning of the loop and evaluate the test condition again.0555

When continue works with a for loop, though, it also (because it reaches the end of the loop) is going to evaluate that third expression, as well.0563

So, that counter will get incremented.0570

The break statement just jumps out of the loop, just as it did with the while loop.0572

And then, additionally, like in our while statements, sometimes when we use break conditions,0577

you would have an indefinite loop by declaring while equal to true.0582

And then, somewhere within our loop statement group, we would have a break statement that would allow us to jump out of the loop.0588

We can do that similarly--create an indefinite loop in a for loop--by leaving the second expression in the for loop definition blank.0594

But you have to include its semicolon; so let's take a look at what that looks like.0604

Down here (let me just un-comment the code), as you can see, we have a for loop definition.0610

We have a counter variable that we have set equal to i; we have our increment operation, which occurs at the end of each iteration of the loop.0619

And here, as you can see, we have left the second expression, which is the test condition, blank.0628

And when you do that in PHP, that is essentially saying the test condition is equal to true,0633

which means that loop is going to run indefinitely until it encounters a break statement.0637

Notice that we have included the second semicolon here, even though there is nothing here.0642

And if you left that out, you would get an error; so we are going to leave that in there.0646

And then, just for demonstration purposes, all we have done, essentially, is move that test condition into the loop.0650

And whereas before, we were saying, "If i is less than count index..." now we are saying, "If i is greater than or equal to the count index,0656

then the end of the array has been reached; go ahead and break."0664

And as we know how break works...it works the same as in a while loop.0667

Once the statement is reached, it automatically jumps out of the loop, and we come down here and execute whatever next statement is available.0670

If i is not greater than or equal to the count of the array, it is going to perform the same echo operation that we did up here.0678

It is going to output and say, "Indexed array, element 0, equals the uppercase version of that."0685

So, if we go and run this (make sure it's saved; refresh the page), now we can see here: ending the loop using the break statement,0691

it performs the same exact function, except we have used a break statement.0702

And again, the reasoning behind doing this example is to show you how to do an indefinite loop using a for loop,0707

and then also to show you how that the break statement still works within for loops, as well.0715

Now, the next kind of loop that we are going to talk about is probably the one used most often in PHP; it is called a foreach loop.0724

And it is a special type of loop, similar to the for loop, similar to the while loop, but it is made specifically for iterating over arrays,0730

which is a very common function that you do in PHP--actually, in all programming languages.0737

So, it is built into the PHP itself.0744

What it does (and I am going to show you the syntax in a second) is: you provide the foreach loop with an array that you want to iterate over.0749

What it does is: when that foreach loop starts, it sets the array's cursor.0758

Again, the array cursor, the internal array pointer, is where the current element of the array is right now.0763

We saw, in our previous lessons on while loops, how you could move the array cursor by using the next function, or previous,0771

or using the reset function to set it back to the beginning.0777

When you enter a foreach loop, it automatically resets the cursor back to the first element.0781

It calls the each function on the array, which is going to pull out the key and value for you, if you need those.0785

And then, as the each function works, it increments the array cursor one.0794

It is going to go ahead and execute the statements in the foreach loop.0798

And then, because the array cursor has been executed, when it gets back to the beginning of the loop,0802

it is going to pull out the key and value of the next item.0808

It is going to advance the array cursor by one and repeat.0810

Now, as we know from the each function, eventually, when the each function gets to an array where the cursor is past the last element,0813

it returns false, and then that is how the loop breaks out of the loop.0821

That is with a for loop and all of our other loops; continue and break statements work exactly the same.0825

And a foreach loop comes in two syntaxes.0831

They both can be used for indexed arrays and associative arrays, but typically one is used for iterating over indexed arrays.0835

And so, we are just going to call it the indexed array syntax.0841

And likewise, there is one that is used typically for iterating over associative arrays, so we are going to call that the associative array syntax.0844

This is what a foreach loop for indexed array syntax looks like.0853

We have the foreach keyword, and then here, we have a parentheses.0857

And what we do is we supply it (in this case, it would be) an array--a variable that is an array.0862

Then, you have a special keyword here called as; and then, we define a variable, and we can call it whatever we want here.0869

In this case, we are calling it value.0877

What that means is: first, when the loop begins, it resets the array cursor in array to the first element.0878

It is going to pull the value of that first element, and set whatever variable we declared here equal to that value.0888

It is going to run the statement group (for example, we could say echo 'value').0895

Now, every time it pulls that value out of the array, it is running the each function, so it is going to advance the array cursor.0903

So, the next time, when it loops back up here, it is going to go to the second item in the array.0913

It is going to set the variable ('value' again) to whatever the value of the next element in the array is.0917

Here is a more concrete example: we define an array called indexArr, which just has three numbers (1, 2, and 3) in it.0924

And then, here is an example of using the foreach statement to loop over that array; and we are going to output each of the values in the array.0932

This first argument (or expression, you might call it) that you provide to the foreach loop is the array you want to iterate over.0939

So, in this case, we want to iterate over the indexArr.0946

We have the as keyword, and then, when we iterate over, we are creating a variable called value0948

that we are going to have foreach populate with whatever the value of the current array element is at that time.0955

So, when this foreach loop begins, the first thing it does is resets the array cursor indexArr to the first element; so it's going to be pointing at 1.0963

It sets value equal to 1; it enters and advances the array cursor (so the array cursor, at this point, is pointing to 2).0971

It goes ahead and runs a statement group (and in this case, we are just basically echoing the value that was set).0980

It returns back to the top, and it is going to, again, set value equal to the value of the current element in the array.0985

Since the array cursor was advanced, value is then going to be set to 2.0994

It is going to set value to 2; it is going to echo 2; it is going to repeat again to 3.0998

When it comes around the fourth time, the array cursor will have advanced past the end,1003

so that each function that it is implicitly calling is going to return false.1006

And then, it is going to jump out of the loop.1011

Let's take a look at another example of a foreach loop looping over an indexed array.1015

We have an indexed array that contains the same values that we had in the previous example for for loops--just the lowercase letters a, b, and c.1023

We are going to use the foreach loop to iterate over that array and just output, as we did before,1033

the uppercase values of each of the elements in that indexed array.1039

So, here we provide with the name of the array we want to iterate over, indexArr.1045

We provide the name of the variable that we want to access the current array element's value as.1052

And in this case, we want to access, within our statement group, the current element's value with the variable called value.1063

What it is going to do is: when this first occurs, it is going to reset the cursor of indexArr to the first element, to point to a.1071

It is going to set value equal to a; it is going to advance the cursor; it is going to run the statement group, because value is now set to a.1078

It is going to run the strtoupper function, and it is going to output that.1085

And it is going to repeat that for each element in that array.1089

If we go and look at this script and how it runs, we can just see what it does.1094

Looping over the indexed array, it output all of the values in uppercase.1102

The other syntax that you can use with a foreach loop is typically used for associative arrays.1112

It pretty much looks exactly the same as the other one, except for this part right here.1118

The indexed array syntax of the foreach loop would set this last variable that you provide1125

to the value of the current element in the array that you are looping over.1136

In this one, you can additionally not only extract the value into whatever variable you want to call it (here, we are calling it 'value'),1143

but you also can extract the key of the current element in the array.1150

So again, when we enter the foreach loop, whatever array we pass it, it is going to set the array cursor to the first element in the array.1157

It is going to set key equal to the key of that first element; it is going to set value equal to the value of that first element.1165

It is going to advance the array cursor; it is going to execute the statement group.1171

Maybe we will be making use of these variables.1175

It is going to come back up to iterate the loop again; as long as there is another element in the array to iterate over,1179

it is going to continue to do that until it reaches the end of the array.1184

Here is a concrete example: we have an array called assocArr that has two key/value pairs in it.1187

One key is called first, which we have set to the string 'joe'; one key is called last, which we have set to 'smith' to represent a first and last name.1195

And what we are going to do is just iterate over that associative array and output all of those key/value pairs.1202

What we have done is provided assocArr to the foreach loop, which is the array we are going to loop over.1209

And then, we are saying that, for every element of assocArr that gets looped over, we want the key of the current element1215

being looped over--the variable key--to be set to the value of the key of the current element.1224

We want this variable value to be set to the value of the current element that is being iterated over.1232

Then, here, we have a simple echo statement that is just outputting the current key with our array operator.1240

And then...it doesn't actually operate it; this is just a string being output so that we can see it.1250

And then, it is going to output the value.1255

When you run this array, it is going to output two lines.1258

The first one is going to say first, and then with this array operator as a string, 'joe'.1261

And then, the second line is going to say last='smith'.1268

Let's take a look at iterating over an associative array now, using this other syntax for foreach.1274

Here, we have an associative array that contains three key/value pairs--three elements.1285

The key to each of the three elements is a three-letter abbreviation for the month, for the first three months.1291

The value associated with those abbreviated month keys is the full name of the month, all spelled out.1298

We are going to do the same thing we just saw in the last example: we are going to loop over that associative array, using a foreach loop.1306

And we are just going to output all of the key/value pairs that we find.1312

So, when this foreach loop starts, assocArr's cursor gets reset to this first element, which is jan=January.1316

It is going to set key equal to the value jan, which is the key of the current element.1324

It is going to set value equal to the string 'January', which is the value of the current element, and advance the array cursor.1330

Then, it is going to go ahead and process the statement group that is associated with the loop.1336

Here, we are just outputting the key and the value.1341

It is going to come back up to the loop again, as long as there is another element in the array (which there is).1345

Then, it is going to set key equal to feb; it is going to set value equal to February.1349

It is going to run that again; it is going to do that for all three of these key/value pairs within the associative array.1354

And so, when we run this, we should see an output of all of those.1359

So, let's take a look at what this looks like in the browser.1362

And here, you can see, looping over the associative array, it does what we expect:1367

it output the key, which gets set as that key variable that we defined in the parentheses of the foreach loop.1370

And then, it outputs the value of the current array element that it is looping over.1379

And this value variable was set to that value.1383

Now, one thing to note is that, even though this is an associative array syntax, it actually can be used for indexed arrays, too,1392

because indexed arrays have keys; they are just not strings--they are actually numbers.1399

Let's say, for example, we just removed the keys of this array so it becomes an indexed array.1403

Now, each of these values (January, February, and March)--they all have an index.1417

But the index is a numeric index, so it is going to be 0, 1, and 2.1422

So, you can still use this: when we output it, it is going to say, "0=January; 1=February; 2=March."1425

And so, if we go ahead and refresh the page, we can see that that is exactly what it has done.1432

So, you can use that syntax for indexed arrays, as well.1437

Likewise, you can use the (actually, let me undo this) indexed array syntax1443

for looping over an associative array, as well, if you are only interested in the value.1465

So, for example, we have an associative array here, which is the same as before, with our abbreviated months and then their full names.1469

And when you loop over it in this way, it is just going to pull the value out; so all it is going to pull is January, February, March.1476

And so, we can just go ahead and output each of those values when it loops over it, and it is just going to pull the values out.1482

When we run this, it is just going to say "January, February, March," which it does.1488

You can use either syntax; typically, the indexed array syntax, as we call it, is used to iterate over indexed arrays,1492

And the associative array syntax is used to iterate over associative arrays.1500

Coding conventions for for loops and foreach loops: for loops are going to be formatted like this, similar to the syntax we have been using before.1506

We have the keyword followed by a space, and then the parentheses for the expressions (in this case) of the for loop,1515

followed by a space, and then the opening curly brace on the same line.1523

You are going to have the closing curly brace at the same indentation as the for keyword.1527

All of the statements within the statement group are going to be indented one indent.1532

And then, for the for loop, because we have three expressions, we are going to have each expression...1536

The first two will be immediately followed by a semicolon and a space,1541

so we have a semicolon and a space, as opposed to just having a semicolon and no space here.1545

So, we are always going to do that: the space is not necessary, but that is going to be the convention.1553

That is our convention for for loops; for our foreach loops, they are going to be formatted as this.1557

It is the same thing as with a for loop: we have a space between the parentheses and the foreach keyword,1562

space here, curly brace in the same line, closing curly brace indented the same, statement indented one...1567

And then, what we have is: immediately following the opening parentheses, we list our array variable, followed by a space1574

and the as keyword, followed by another space, and then the name of the variable that we are going to be setting the value to.1581

The foreach in the associative array syntax is going to be basically exactly the same,1589

except that we are going to add this key and this array assignment operator sign.1595

There is still going to be a space between as and whatever variable we decide to set for key,1601

a space between key and the array assignment operator, and a space between the array assignment operator and value.1605

That is the syntax we are going to use for that.1611

For the homework challenge, I want you to create an empty indexed array called numbers.1615

You are going to use a for loop; instead of iterating over an array, in this case, we are going to be populating an array.1619

And I want you to use the for loop to populate this indexed array, numbers, with the integers 1 through 5.1625

And the hint here is to use the value of the loop's counter variable as a way to populate numbers.1633

At the end of this first for loop, numbers should be an indexed array containing the numbers 1, 2, 3, 4, and 5.1640

Then, I want you to use a foreach loop to iterate over that array, the now-populated numbers.1647

And what I want you to do is iterate over the array, adding up each of the numbers in the array.1652

So, by the end, you should have a sum that equals 15, because 1, 2, 3, 4, and 5 added together equals 15.1658

So, after you iterate over the array, you want to output the sum (this variable sum that I am going to have you create).1665

And then, verify that it equals 15.1672

And then, one other thing that I want you to do is create a separate foreach loop.1675

And I am going to have you iterate over the GET superglobal, which, as we know,1680

is the array that holds all of our GET name/value pairs that are supplied to a script.1684

And on each iteration over the GET array, I just want you to output the name and value of each GET variable.1689

Now, in a script that has no name/value pairs passed to it, nothing is going to happen.1697

But what I would like you to do is then generate some random name/value pairs, and append them as query strings1703

to whatever your script is, so that when you supply them to them, when this loop runs, it is going to output each of those name/value pairs.1711

And what you should do is supply some random name/value pairs, like first='joe' & last='smith'.1718

Provide that as a query string to whatever your script name is, and then verify that when you run that, you get output:1733

first is 'joe' (or maybe you want to use that symbol); last equals 'smith'.1741

And try that for any number of different variables; that will give you an example of iterating over the GET array, which is something that is often done.1754

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

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.