Matthew M.

Matthew M.

User-Defined Functions

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

Lecture Comments (4)

3 answers

Last reply by: Russell Burnham
Thu May 16, 2013 7:14 PM

Post by Russell Burnham on April 18, 2013

Matthew, I can't get the return to work, here is my code:

"<?php
function testrr($num){
if ($num == 1){
return 'one';
}
elseif ($num == 2){
return 'two';
}
}

testrr(1);


?>"

This always returns blank.

Am I doing something wrong?
Please help.

User-Defined Functions

  • Defining a function requires four parts:
    • the function keyword
    • a function identifier
    • a function parameter list
    • a function statement group, or ‘function body’
  • Function parameters are variables defined in a function’s definition that can be used within the function’s body. They are initialized by arguments passed to the function each time it is called.
  • Return values of functions are specified using a return statement.
  • Return values can be of any PHP data type. If a return statement is never executed within a function, NULL is returned.
  • The include_once construct functions exactly as an include statement, except that if the file specified has already been included it will not be included again. require_once functions like a combination of include_once and require .
  • Additional Resources:

User-Defined Functions

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:16
    • Lesson Overview
  • Defining Functions 1:29
    • Four Parts of Defining a Function
    • Functions Example 1
  • Function Parameters 5:29
    • Function Parameters
    • Functions Example 2
  • Return Statements 12:53
    • Return Statements
    • Functions Example 3
  • Where to Define Functions 20:34
    • Where to Define Functions
  • include_once Construct 22:10
    • include_once Construct
    • include_once Coding Example
  • Reasons to Use Functions 27:44
    • Take Advantage of Code Reuse
    • Improve Code Readability
    • Use Instead of 'content' Include Files
  • Web Application Development 34:42
    • Version 10.0 Changelog
  • Version 10.0 Coding Example 37:55
    • Version 10.0 Coding Example
  • Outputting HTML in Functions 47:04
    • Outputting HTML in Functions
    • Example
  • Coding Conventions 53:16
    • Coding Conventions
  • Homework Challenge 54:33
    • Homework Challenge

Transcription: User-Defined Functions

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

Today's lesson is an extremely exciting lesson--probably my favorite of the whole course--0004

because we are going to be teaching you how to create your own user-defined functions,0009

which is really going to increase your efficiency as a PHP programmer.0012

Specifically, we are going to talk about how to define your own functions, and the syntax that is involved with that.0018

We are going to talk about function parameters, which is ways to pass data to functions.0024

We have previously talked about that when calling functions using function arguments.0029

We are going to talk about return statements, which allow you to return a value from a function,0033

because, as we had learned before, functions are expressions, and they have values.0037

We are going to discuss where to define functions within your PHP code,0043

and that is going to involve a discussion of the include_once construct,0047

which is an extension of the include construct that we have talked about before.0052

And we are going to also talk about some reasons why you use functions, and why to separate code out into functions.0056

Then, we are going to make an update to our web application, version 10.0,0063

where we are going to extract some of the code from it and put it into functions,0068

so we can see our user-defined functions working firsthand in our web application.0073

And then, we are just going to quickly talk about when you do output HTML in functions, and then also coding conventions as it relates to functions.0079

Defining a function requires four parts: it requires the function keyword (and this is an example of a function declaration);0091

you provide a function identifier or a name of the function; then, in parentheses, you provide what is known as a parameter list0100

(we are going to talk more about that in a minute), and then, just like a control structure,0108

you have a statement group enclosed in parentheses, and that contains all of the code that gets executed when a function is called.0112

And this section of a function is known as the function body--the code contained in the function is known as the function body.0120

Anything contained, any PHP code that we have gone over so far, is allowed anywhere else in PHP script.0127

One thing to mention is that function identifiers or function names follow the same naming rules as we have for variables and constant identifiers.0135

Let's take a look at some examples of some basic functions, and see how they work.0146

Here, we have a sample script called functionExamples.php, and right here, this function is an example of the most simple function.0153

It is a function that, when it gets called, does nothing.0164

It has no statements in it, which is a perfectly legal function.0167

Now, as we know, when we call a function, it stops execution of the current script, goes and executes the code in the function,0170

and then returns the value to the script where it was called from; and then, the script continues to execute.0178

So here, in this case, we have defined this function; and then, I am going to go ahead and just call it.0183

And the way we do that is: just as we have been calling built-in PHP functions, we have the function name with the parentheses and a semicolon.0191

And this function does nothing, but it shouldn't cause any problems or any errors, and when we load the page, nothing should happen.0200

But this is just to demonstrate that a function can contain anything, including the lack of any code.0206

If we load functionExamples.php, nothing happens.0215

The other thing we can do is: we can create a function that just has one statement in it, but it is an empty statement.0220

And again, this is just to emphasize the fact that functions contain statements, and they contain any types of statements, including an empty statement.0233

And if we go ahead and call this function, we should receive no output, as well.0243

I'll go ahead and load the page, and nothing happens.0249

So now, we are going to get a little more interesting: we are going to have our function do something.0254

We are going to create an example function that is going to output some code by an echo statement.0259

So, we have this function called example1_2, and all we are going to do is simply echo a statement that says "inside example," and then the function name.0269

So, when we go ahead down here, below the function, and we call this function, example1_2,0284

what is going to happen is: when we reach this statement right here, it is going to call the example1_2 function.0297

It is going to run the code inside of it, which in this case is just an echo statement,0303

and it is going to echo that statement, and then continue processing the rest of the script.0306

So, we should see this line output now, when we run the script.0310

And when we reload the page, you can see, the echo statement was output that we had in the function.0316

So now, I want to talk about something called function parameters.0330

Function parameters are variables that are defined when you define a function, and they are variables that can be used within the function body.0335

They are described in between the parentheses of a function definition; and you can have as many parameters as you need.0344

When you call a function, as we have seen with built-in functions to PHP, we can supply data to functions.0351

And those are called function arguments; the data you supply is called an argument, and the argument fills the parameter variables of the function.0359

For example, when we define this function with param, we define it as having one parameter called param.0369

And what that means is that we can access this variable, param, anywhere within the function body.0377

And here, we can see that we just echo the value of param.0383

And the way that that gets said is by...when you call the function with param, you provide a function argument.0385

And what happens is: param gets set to this value.0394

So, every time this function is called, whatever value is supplied as an argument to the function--this parameter will get set to that value.0402

Now, I use the term parameter and argument, and it's sort of a technical distinction.0413

Technically, when you define a function, and you create (I'm erasing this) a parameter list, the variables that you make available0416

in the function definition are known as function parameters.0431

And when you call a function, you call a function with arguments, and those are the values that the parameters are going to get set to.0437

And sometimes, you will hear those terms used interchangeably; but essentially, they refer to kind of the same thing.0443

When you call a function, you supply arguments which set the value of the parameters specified in the function definition.0448

And you can have multiple parameters in a function, and what happens is: let's say, for example, there was another parameter here--0456

the way that it would get set (let's call it param1) is: when we call the function, we would supply a second function argument.0469

The order that the functions are supplied in specifies the order that the parameters get set in.0482

So, the first parameter gets set to the value of the first argument; the second parameter gets set to the value of the second argument.0494

Let's go take a look at some functions that use parameters.0502

So here, we have another example function that we are going to create that is going to take one parameter.0513

And all that it is going to do is echo the parameter that gets passed in.0518

For example, when we call this function, it requires one parameter, so we are going to have to supply it a value that it can output.0526

So, we are going to supply it with a string; and when we call the function...let's supply it with the string "Hello, World!" for example...0533

Now, what is going to happen is: when we call this function, this code is going to stop executing right here.0548

It is going to go up to where the function is defined; it is going to set the parameter variable,0554

this param variable, to the argument specified, which was "Hello, World!"0560

And then, it is going to run the code in the function body, which just says to output the parameter variable,0565

which is the string value that was passed in.0571

So, when we run our code, we should see it output this string "Hello, World!"0574

And if we go and take a look at that (oops, I had a typo; I forgot an e down here), you can see that it output "Hello, World!"0580

And if we include a break statement up here, this will look nicer...or actually, let's just get rid of this function call.0601

We can see that "Hello, World!" is output.0618

And the unique thing about function parameters is: they can be set to a different value every time the function gets called.0622

And that is what makes the function useful: it allows you to dynamically do things.0629

It can do one thing in one instance...for example, output one string one time it is called,0632

and then output another string when it is called a different time.0637

So, for example, we could input to this function the string "Goodbye" and then now, when we call this same function again,0640

even though it is the same function, it is going to perform a slightly different action, based on the arguments that we specified.0651

So, when we load it now, it is going to output this new string statement that we passed in.0660

Let me go ahead and comment out this line.0669

And now, what we are going to do is demonstrate, using a function that has multiple defined parameters.0672

Here we have a function with two parameters called param1 and param2.0679

What I am going to have it do is create a string that is just the concatenation of both parameters.0684

So, these parameters are both going to be strings that get passed in, and then we are going to create a string inside the function0691

that is just going to be the concatenation of those two strings, using the dot operator.0699

And we are just going to echo this concatenated string.0708

When we call this function, example2_1, we provide it with two parameters.0718

In this case, we provide it with "Hello," and then, as a second parameter, we provide it with the string " World!"0725

So now, when we call this function--when we execute this function call--it is going to take these two strings.0741

It is going to set the value of param1 in this function to Hello,; it is going to set the value of param2 equal to World!.0747

And then, the function is going to execute, and it is going to execute the concatenation of those two strings.0753

We should, again, see the string "Hello, World!" output.0761

If we go and reload the script, we can see, "Hello, World!" was output.0764

As we know from our Introduction to Functions lesson, functions are expressions.0775

What that means, that we know about expressions, is: they have a value.0780

So, functions always have a value, and they always return a value to the script that they were called from.0783

And the value of a function is known as its return value, and return values in PHP can be any type.0790

It can be an array; it can be a scalar, as we learned; it can be an object.0801

The way that you specify the return value of a function, or the value that it returns to the script it was called from, is using a return statement.0807

One thing to note, that we are going to show an example of in a second, is that you can have multiple return statements within the same function.0817

What happens is: when a function is executing, any time it reaches a return statement,0824

it automatically ends execution of the function and returns whatever value it was set to return in the return statement.0830

And even if there are statements that are still unexecuted in the function, it doesn't matter.0839

Once that return statement is reached, the function ends, and it returns back to the execution of the current script.0842

One other thing to note is that, if a return statement never occurs, or is never executed, within a function,0848

null is the value returned by default for all functions.0855

Let's go take a look at the return statement in action.0859

The first thing we are going to do is: we are going to have the return value...0870

we are going to use a return statement that is just going to return a static value.0873

So, it's going to have it return the string "Goodbye," and then we'll call the function, and this is a function with no parameters.0877

What is going to happen when we call this function in this statement right here: example3_0 is going to get called,0898

and what it is going to do is: it is going to execute the function body of example3_0.0907

And we see in the function body that all it contains is a return statement.0912

So, what this is going to do is: when this function reaches this return statement,0914

which is the only statement in the function, it is going to return the value Goodbye.0918

And that is how the return statement works: the statement starts with the return keyword, followed by a space,0923

and then whatever value you are going to return, which could be an expression;0929

it could be the output of another function, because functions have values.0934

Basically, you can return anything with value.0936

When this gets called, example3_0 is going to get executed; its value is going to be inserted here, and its value is going to be the return value Goodbye.0939

So, this is going to echo the return value Goodbye, and so the script should output Goodbye.0947

If we go ahead and reload the script, we can see that that is what happens.0955

So now, one thing that we are going to talk about that we had mentioned was that you can have multiple return statements within a single function.0966

For example, if we have a function that accepts one parameter, we can maybe conditionally return a value, based on the value of that parameter.0976

For example, we create an if statement that says, "If param equals the integer 1,0986

then we are going to execute a return statement that is going to return the string 1."1000

"Alternatively, if param equals 2, we are going to have it return the string 2."1007

So in this case, when this function gets executed, you can see that there are two return statements.1023

If param is equal to the integer value 1, it enters this if statement block, and it executes this return statement, and the function exits.1027

So, none of the remaining code gets executed.1034

If parameter equals 2, it is going to not enter this if block; it's going to enter this elseif block, and then it is going to return the value 2.1038

We could additionally add an echo statement here that says, for example, param is not equal to 1 or 2.1045

And also, what happens with a function is: if no return statement is specified,1059

the function just returns whenever it executes the last statement within the function.1063

So here, if param doesn't equal 1 or 2, this statement is going to get executed.1070

However, if it does equal 1 or 2, the function is going to return up here or right here.1074

And this echo statement won't get executed, or if param is equal to 1, none of this will get executed, as well.1080

That demonstrates a function that can have multiple return statements.1088

For example, if we call this function (and we are just going to echo its output, because it is returning string values),1093

let's put the value of 1 in; so it should enter this first section of code; it should return the value of 1.1111

And then, because it returns, the rest of it shouldn't get executed; so this echo statement should not occur.1119

When we run this code, we can see, it outputs 1.1125

Likewise, if we just change this down here to the value 2, we can see that it outputs the value 2,1130

and it never gets to this echo statement here, because again, once you reach a return statement, the function stops executing.1140

The other thing I wanted to demonstrate is: we have mentioned that, if a function doesn't have a return statement, it automatically returns the value null.1148

So, if we just have an empty function here, with a single statement that does nothing, and then we were to run the function,1158

we are going to run a comparison on the value returned by the function, to see if it is equal to null.1168

And we should find out that it is true.1173

So, if we run the comparison example3_2, what we are saying here is: we are running a comparison operation that is saying,1176

"call function example3_2, compare it to returned value (which in this case should be null,1193

because no return statement was explicitly specified); it should equal null,1197

and because it equals null, this statement should be true."1202

And with echo statements, the boolean value true gets output as the value 1.1208

That is the conversion from a boolean true to a string.1214

So, when we run this, we should see just the number 1 output; and that is confirming that this returns the value of null.1217

When we run the function, we can see that 1 is output, meaning that it did, in fact, return the value of null.1224

We have talked about how to define functions; we have talked about defining them with parameters and defining them with return statements.1236

We haven't really talked about where to define functions.1243

Typically, functions get separated into their own PHP files, known as function libraries, usually grouped according to the task they perform.1247

You might have 4 or 5 different functions, for example, that perform tasks on arrays.1255

So, you might create a function library that contains all of those functions.1261

And the function library only contains the PHP code for those functions.1265

It is just a PHP file that contains all of the function definitions.1271

And the way you access the file is by using an include statement; so if you are running a PHP script,1279

and you need to access, for example, an array function that you have defined in a function library,1284

you include that function library by an include statement, and then you will be able to have access to the function.1289

Now, one thing to note is: unlike some programming languages, in PHP, a function definition does not need to occur before it is called.1297

You can call a function and then define it later in the script,1308

or call a function and then include the function library where it is defined later on in the script, and it will still work.1311

As long as the function that you are calling is defined somewhere within a script, within the script it is being called from1318

or an include file of that script, it will work.1324

The way you typically include functions, and what we are going to use in this course (or include function libraries within your code) is1332

using a new construct that we haven't talked about, called include_once.1338

It is similar to the include construct, and what it does is: it takes the file that is specified and includes it in line, in the file where the include statement happens.1344

The one difference is that, with include_once, if you specify a file to include, and it has already been included, it won't get included again.1356

For example, if in this single PHP script we have this one statement, include_once ('functionLIB.php'),1369

which maybe is a function library, and somewhere in the script, we had another include_once function that says functionLIB.php,1376

it is not going to re-include it, because it has already been included once.1385

The reason that this is used with function libraries, as opposed to using an include statement, is:1389

if you redefine a function within the same PHP script, you get a fatal error in PHP.1394

For example, if you accidentally include the function library twice within your script,1400

using just regular include statements, you are going to get a fatal error.1405

And similar to include, include_once also has an analog, which is called require_once, which functions the same as the require statement,1410

which throws a fatal error if it can't find the file it is trying to include.1418

But it also functions like include_once in that, if you call require_once on the same file1422

multiple times in one script, it only gets included the first time.1428

Let's go take a look at a script that demonstrates this ability.1432

We have a file called includeOnce.php, and here we have another file called functionLIB.php.1438

This is a file that is just a function library; it contains a couple of different functions.1446

Here, in this case, we have a function called test, and a function that we are going to use in another example called outputHtmlHeader.1453

What happens is: let's say we want to use the test function within our script.1460

And let's get rid of this include statement--comment this out.1467

Now, when we run this file, we should get an error saying that the function is not defined,1471

because the function hasn't been included or been defined within this file.1476

So, if we go ahead and load the file up, it is going to say "Fatal error: undefined call to the function test."1480

OK, so we need to include the function library in order to have access to that function.1488

So, what we can do is use an include statement, include 'FunctionLIB.php'.1494

And this test function just simply outputs a simple statement; it says, "in test."1499

Now, when we run the file, everything should be OK; and it goes ahead and runs.1505

Now, however, let's say we accidentally, later, down here in our script, forgot that we had included that function library already,1509

and we run an include statement again, and try to call test again.1519

Well, we are going to get a fatal error here, because FunctionLIB.php is going to be included twice.1524

And because FunctionLIB.php has the definition of two functions in it, and it is called twice,1529

PHP is going to see those functions being redefined, which is a fatal error.1537

So now, when we run our script, which has these two inclusions of the same function library, we are going to get a fatal error.1541

And we can see "Fatal error," and it tells you what we would expect: you cannot re-declare test.1550

It says that test, defined in the FunctionLIB, cannot be re-declared.1556

The way to fix this problem is if we use include_once on both of these statements.1568

And if we always use include_once when including function library files, we won't have this problem.1572

It is going to include FunctionLIB.php here; it is going to call the test function;1580

and then, when we try to include it again, later on down the file, it is going to say, "Hey, I have already included this file;1585

I'm not going to include it again," and you eliminate getting that fatal error for trying to redefine the function.1589

If we save this file now, we should be able to run test twice.1597

We go ahead and load the file; we can see, we get that test was run; it says "in test" here twice.1602

Similarly, we can use...as mentioned, there is an analog require_once that does the same thing,1610

except it returns a fatal error if the file you are trying to require or include cannot be found.1623

So, we should get the same output, because it works.1630

And what that means is that this function was not included a second time.1632

Now again, the way require differs from include is that, if it can't find the specific include file1636

(for example, if I mistype the name of it), it is going to throw a fatal error.1643

So, if we run the file, it is going to say "Fatal error": require_once wasn't able to open this misspelled function library.1647

And so, that is the difference between require and include.1657

What are the reasons to use functions?1666

The main reason, or one of the major reasons, is to take advantage of code reuse.1670

What that means is: if you have a common task that you are performing over and over again,1675

you can isolate the code into one spot (into one function), and then you can just call that function over and over again, as you need it.1678

What that does is: that is going to limit your mistakes to one place.1685

The mistakes can only occur within that one function, whereas, if you had, for example, 5 lines of code that were included in the function,1690

if you include those 5 lines of code everywhere (in 5 different files, for example), that is 5 different places where a mistake can occur.1697

If you take that code out, put it in a function, and replace it with a function call,1706

then if there is an error in that code, you only have to fix it in the function file.1712

Additionally, if you want to (for example) change the functionality of something,1718

what you can do is apply the changes all in one place.1723

So again, if you use a function call (for example) to display something, and you want to change how it is displayed,1726

instead of going through each file, editing the code that displays it,1733

if you put that code within a function, you can just go ahead and apply the changes that need to be made in one spot,1737

and it goes ahead and applies it to all the other things.1743

So, this is going to increase your efficiency as a programmer; it is going to allow you to get things done faster.1745

Additionally, it is going to allow you to reduce writing the same functionality repeatedly.1751

For example, let's say you spend a couple of hours one day developing a couple of functions that you use for working with arrays.1756

Well, maybe you have another PHP project down the road, and you want to use some of those same functions.1763

Well, by including them as functions in a function library, you can reuse those in your new PHP application.1770

If you hadn't separated them out as functions (had just hard-coded them into your PHP pages),1776

when you went to your new project (for example), you wouldn't be able to just include the function library and reuse it.1781

So, separating things into functions allows you to reuse them, not only within one application, but in multiple applications.1791

The other thing that you can use functions for is to improve code readability.1798

That is by separating your HTML and PHP.1803

Typically, you want to separate (and this is something we haven't really talked about) your HTML and PHP as much as possible,1806

because it makes for cleaner-looking code, and also, when you are working on a team (for example)1814

you might have people that are HTML developers and people that are PHP developers.1819

The less you intermingle the HTML and PHP with one another, the easier it is1822

for different teammates to work together, without messing up one another's code.1831

If the HTML developer only works with HTML files with minimal PHP code, the chance of him messing up the PHP is limited.1836

Likewise, for a PHP developer, if he is only working with PHP files that have very little HTML, the chance of him formatting the HTML incorrectly is reduced.1845

So, you can use functions (for example) to remove large sections of PHP code from mixed HTML and PHP files.1854

You might have a 200-line PHP page, and maybe 25 of those lines are PHP code.1865

Well, what you can do is separate it out into a function, and then,1872

when your HTML developer goes to edit the HTML on that page, all they see is one function call.1875

They don't see the 25 lines of code that that developer has a chance to mess up.1881

Another thing that you can do is...sometimes, you just have ugly mixes of HTML and PHP code.1890

When you use echo statements, you have a lot of escaping with backslashes to output quotation marks, and so on.1897

So sometimes, if you have smaller "ugly" sections of mixed HTML and PHP code, you can separate that out into a function.1905

And what that is going to do is make your PHP page that is actually output look cleaner.1916

You are not going to have to look at the messy PHP and HTML mixed together.1924

Instead, you can have it in a function, so that ugliness only occurs in one spot.1927

The other thing that some people use is: you can essentially use a function as a way to include HTML content--to replace include files.1934

That is something you might see on different projects that you work on; you might see different developers use it.1944

For example, we have a script called functionVsInclude.php.1952

Here, we are including a function library, using the include_once statement we just learned about.1964

And the reason we are doing that is because it contains this function called htmlHeader.1969

What this htmlHeader does is outputs (for example) the header of an HTML page.1973

Maybe we want to include this at the beginning of every page.1980

So, what we could do is: in our page, we could go ahead and call outputHtmlHeader.1983

And what it is going to do is take the echo statements that are in that function, run them, and then output that.1990

Now, we could achieve the same thing by using an include statement...1998

for example, if we included this file called header.html,2003

which contains all of the same HTML that is output by this outputHtmlHeader function, and instead, we include it in here.2007

So, basically, there are two different ways of doing the same thing.2015

Some people like using functions to do that; maybe, instead of using an include statement for a header and a footer,2017

you have two functions called outputHtmlHeader and outputHtmlFooter; it is a preference thing.2024

You may see that in different spots.2032

And one thing to also note is that here, we have all of these echo statements.2034

But as we know, with PHP you can always escape to HTML mode.2038

So, we actually could replace all of these echo statements with the exact same content that we have in header.html.2042

What is going to happen is: when we run this outputHtmlHeader, it is going to execute what is in its function body.2060

And what is in the function body is an escape to HTML and an output of HTML code.2067

And then, at the end of the function, it just returns.2074

So, that is another way to output HTML, as well.2077

So now, let's talk about improving our web application.2083

We are going to move to version 10.0, and we are going to2086

take advantage of what we have learned about creating user-defined functions to clean up our code a little bit.2089

In checkout.php and contactUs.php, there are some rather complex boolean expressions used to validate the form data submitted.2097

For example, in checkout.php, we validate that a name, a street address, a city, a ZIP code, and so forth, were all provided.2108

It makes a complex boolean expression.2116

In contactUs.php, we verify that all three (name, email address, and comment) were submitted on the form.2120

We are going to separate those boolean expressions out into a function that is just going to return a boolean.2126

It is going to return true or false; and they are going to be called validation functions.2131

What that is going to do is make our code look cleaner, because,2137

instead of having these long, ugly PHP statements, we have a single function call.2140

We are also going to create a function that is just going to be used to output HTML <a> tags, or anchor tags, which are HTML links.2146

It is going to allow us to pass in (for example) a URL and the name of the link that we want,2156

and it is going to dynamically create an HTML string that uses the URL and name provided.2160

We are also going to add a constant to config.php that is going to be called LIB_DIR.2168

What that is going to do is: we are going to put these functions into function libraries, and we are going to store them in a lib directory.2174

And, just as we created an INCLUDE_DIR constant in our config file, we are going to create a LIB_DIR one2181

that is just going to be a path to where we are going to store all of our function include files.2186

What we are going to be doing is creating two library files, stored in the lib directory, one called outputLIB.php, and one called utilLIB.php.2192

That is the convention we are going to use in this course for signifying function library files.2204

You will have a descriptive name for the functions that are included in the file,2209

and then it is going be ended with the three capital letters LIB as part of the name.2214

And that is going to let you know, when you look at that file, that this is a function library.2220

This function library, outputLIB.php, is going to be used for functions that do any sort of outputting of HTML.2225

And then, utilLIB.php is going to be used for utility functions.2232

For example, that is where we are going to put our validation functions;2239

they are functions that perform utility operations you can use within your web app.2241

So, we are going to edit checkout.php and contactUs.php.2247

What we are going to do is take out the complex boolean expressions and replace them with the validation function.2252

We are going to edit config.php, so that it has this updated content, LIB_DIR.2259

And then, we are also going to update store.php to make use of the function we are going create up here2264

that is going to allow us to create HTML anchor tags.2269

Let's go take a look at the code; for example, if we look at version 9.0, if we look at checkout.php (this is version 9.0),2276

and if we scroll down to where we do the data processing, we can see that there is one section down here2296

where we are validating the shipping information that was provided by the customer.2304

And if you look, this is a particularly ugly and complex boolean expression that is validating all of the data.2308

And so, what we can do is take that out and put it in a function that returns the boolean value of that expression.2317

And then, we can replace it with a single function call, and that is going to make it look cleaner.2324

For example, we replace that boolean expression; instead, we just have a function call down here, called isValidCustomerData.2328

And the function name has a descriptive name; typically, functions that return boolean values have is prepended to them, as the first part of the name.2338

So, isValidCustomerData is going to tell us about whatever we are passing here--is this valid customer data?2350

And so, we are getting the same functionality, but we are doing it in a simpler, cleaner statement.2358

Likewise, in contactUs.php, we validate the form to make sure that an email, comments, and name were provided.2366

What we can do is take this boolean expression out, and replace it with a simple function call that says isValidContactInfo.2375

Now, the other thing that provides is: let's say, later down the road, for these validation functions, we want to provide more validation.2384

Maybe we want to say that "state" has to be a certain state, or for the contact information, that an email address has to meet a certain format.2391

For example, it has to have an @ symbol; it has to have the domain name and the username as part of it.2400

Well, we can add that to this isValidContactInfo function, and this line right here will never change.2405

That added code won't show up in our HTML file.2412

So, we are going to keep the PHP and HTML as separate as possible.2415

And what that is going to do is allow us to change the functionality, but we don't have to change this code here; it is going to keep this code looking clean.2420

So, if we go to the LIB directory of version 10.0 and open up utilLIB.php, we can see these two functions that we have created.2428

The first one was called isValidCustomerData, and what we do is, from checkout.php, pass it the customerData variable,2442

which is an associative array that represents all of the GET data passed to checkout.php.2452

It goes ahead and uses this customerData parameter, and what it does is performs the same validation function that we had in the script.2459

It says, "Just make sure that each of the different fields are not empty."2466

If all of them are not empty, it is going to return true, so that we can know that it's valid, and proceed as appropriate in our checkout.php.2470

Likewise, for the contactInfo validation, we have an isValidContactInfo function.2483

We pass it the contact information that was submitted on the Contact Us form, and we perform the boolean validation function.2491

If it returns true (that all of the information was provided), then it returns true to our script, and it can go ahead and execute.2500

When it calls this, if this is true, it outputs the appropriate page title (for example).2508

If not, we are going to use our error functionality and set up an error message saying "You must complete all of the fields in this form,"2513

and also set our error flag (that we had talked about in previous versions) to true.2520

The other thing that we are going to do is create a function that is going to allow us to output HTML links, or <a> tags.2529

For example, if we look at version 9's store.php, we can see, we have a link hard-coded into each page2538

that is a link to each different item in the catalog (item-1001, 1002, and 1003).2545

What we can do is create a function that will allow you to pass two parameters or arguments to it,2552

one for the URL of the link that is going to be used for the href, and then also one value2560

that you can put in the body of the link, that is contained in the a tag.2566

If we open this other library file called outputLIB.php, we can see a function called outputLink.2571

It accepts two parameters, both of which are strings: a URL and a body.2580

And what we can see is that it creates a link variable; and what it does is dynamically creates an a tag.2585

It sets href equal to the URL specified; it sets the body of the a tag equal to the body specified.2598

This is a useful function, because this is one of the ways of eliminating ugly PHP/HTML mixed code from your file.2606

And it is not too prevalent in this example, but we will see it more, as our application gets more advanced,2616

and our URL's that we are using, and our expressions to set the value of URL's, get complex.2623

This is going to neaten them up, because now, instead of having a statements, which included echo statements from PHP,2628

we have replaced it with simply a call to outputLink, which we echo.2642

And we pass it the URL that we want (in this case, for item-1001),2648

and then we provide it the name that we want to be in the tag (which is going to be the item's name).2652

And we do that for all three of these.2658

And if we go and look at version 10.0, this is the store home page.2661

We can see, if we look at the source, that our links are just as they were before, except they have been created by this outputLink function.2672

And they work just as they had before.2686

One thing to note: if you have a chance, after the lesson, to look closer at the files that we have created,2693

particularly function libraries, you can see that to each function, I have added a lot of comments at the beginning.2702

It is always good to add comments that provide a lot of information to the programmers about your code.2710

For a function, I usually have a comment that states the purpose--what the function does.2716

In this case, it is saying it is outputting a link that is an a HTML tag, an anchor tag.2722

I also described the parameters that the function takes, so somebody can quickly look at this and say,2729

"OK, it takes 2 parameters, one called url and one called body, and this is what they represent."2734

"The url represents what is going to be used in the href attribute with the anchor tag,2738

and body represents the text that is going to be output as the body of the anchor tag."2744

And then, I also have a return value section that says, "This function returns a string,2748

and the string that it returns represents a well-formed HTML anchor tag."2753

How do we make these functions available to our scripts?2762

That is where the config file comes in; we have updated config.php, and what we have done is created a new constant called LIB_DIR,2764

which states that the function library directory is called LIB, and it is off of the document root.2781

And that is where all of our function libraries can be found.2791

And then, what we do is add to our include section, using the include_once directive.2794

We include each of the two libraries that we are going to use, outputLIB.php and utilLIB.php.2799

This is going to make them available to all of the files where config.php is included.2806

You can see that we make use of the LIB_DIR constant, which says that output.php is in this LIB directory, and you include it one time.2810

And so, that is how we make those functions available to our web application.2819

One thing I want to comment on is outputting of HTML in functions.2827

It is usually preferable to refrain from directly outputting HTML within functions by echo statements or HTML, when it's possible.2831

The reason for that is that, if you have function libraries (for example) in multiple different library files in your code,2840

and you are calling different functions and different scripts,2847

and one function in one library outputs some HTML; another function in another library outputs some HTML...2850

it makes it hard for your HTML editors (which may be you, or it may be a separate person) to figure out2856

where they can go to edit the HTML, so that it is output properly.2862

So, by not outputting HTML in your functions, and only doing it in the actual PHP pages that are going to be shown,2865

it allows the person editing the HTML to find it all in one location.2874

Now, sometimes it is necessary to output HTML within functions, and the recommendation that I have2880

is to put output functions in their own separate library.2887

So, for our example, in this web app, we have created a library called outputLIB.php2890

because that generates HTML (in this case, it generates anchor tags).2897

We are going to build out that library as this application develops, and you are going to see that there is going to be more complex HTML output.2902

But by putting it in that one library, your HTML developer knows that if the HTML that they need to edit is not found2909

in the actual PHP pages that are being output, then it is going to be found in this function library that is defined for output functions.2915

One other thing to mention is that, if you have some functions that generate short segments of HTML2923

(for example, our outputLink function that just generates a link to output),2929

it is highly preferable to generate the HTML content as a string and return that string, rather than echoing it directly in the function.2934

For example, in our outputLink function, we return this link that we created.2943

We could, instead, just echo it directly here--echo link--and we would achieve the same thing as what we had done before,2952

if we just replaced the echo statement here with outputLink.2964

So, it's going to call outputLink, and it is going to echo that.2967

Now, there are a couple reasons why that is not preferable.2969

Let me go back and change it to the way it was.2973

The first one is that it reduces the flexibility of your application.2978

For example, if we return the string, then in the store.php, because we are returning it as a function and not directly outputting it,2981

we can perform another operation on it, whereas if it just gets echoed, it is echoed to the output and there is nothing you can do with it.2993

Maybe we want to include that HTML as all uppercase, so we could use the strtoupper function.3001

Well, because we returned it as a string, we could use that string as a value supplied to the strtoupper function.3007

And then, when we echo it, we can make it all uppercase.3013

And so, it gives us the flexibility of altering that; and in this case, it's a function chain.3018

So, when we go and reload our home page, we can see that (I guess I just did it for the second one, item-1002) it is output in all uppercase.3023

And if you look at the page source, you can see that even the HTML code itself, href and all that, was capitalized, as well.3040

That is one thing that is a benefit of returning a string, rather than echoing it directly.3053

The other thing is that, if you want to create complex echo expressions--for example, let's say we wanted to echo3060

the string "Hello" and then output that link by concatenating it--what is going to happen is: right now, it is going to work as expected.3070

It is going to output Hello; it is going to call the outputLink function, return that <a> anchor tag, and then output it all together.3086

So, if we go and view our code (oops, I forgot to get rid of the extra parentheses here), we can see that3094

it outputs Hello, and then it outputs the anchor tag, as expected.3115

Now, in our outputLink function, if we had instead echoed it directly, something interesting is going to happen.3120

Now, when we call this function, it is going to say "echo this string right here."3134

And in order to echo this string, what PHP is going to do is concatenate the output of outputLink to Hello.3141

Well, in order to do that, it has to call outputLink and find out what its value is.3149

So, when outputLink gets called, because we have echoed the link now, it is going to echo it.3153

So, before this concatenation can occur, outputLink is going to be echoed; so the link is going to end up before Hello.3159

For example, if we go ahead and run this file, we can see that, even though in the echo statement, we have put the outputLink3167

tag at the end of the concatenation operation and Hello at the beginning, the order gets reversed.3179

And that is because, since the function didn't return the string, we can't concatenate it in our echo statement.3184

Instead, it just gets output immediately when it is evaluated.3191

That is one of the problems that that can cause.3195

Just really quickly, I want to finish up with talking about coding conventions.3198

Functions, as you have seen in the examples, are going to be formatted in the same manner that we format if statements,3202

in regards to the placement of curly braces and indentation, the one difference being3209

that the if keyword is going to be replaced by the function keyword.3213

The other thing is functions with multiple parameters: each function in the parameter list is going to be separated from the previous one3217

by a comma and a space, so it is going to be param, and this is going to be a space before param2,3225

as opposed to not having the space there.3235

The other thing that we are going to do is: we are always going to define all of the functions for our course within function library files.3238

You can define them within a script, but then that reduces the ability to reuse those,3244

because then, for example, if you defined it in contactUs.php, if we defined in a email function within there, for example,3250

then if we ever wanted to use that again, we would have to include that whole PHP page.3258

And so, in order to get access to that function, what that is going to do is include all of the HTML, as well.3262

So, we are going to include all of our functions in library files.3267

For the homework challenge, I just want you to create a PHP script that is going to create a function called add, your first user-defined function.3275

It is going to accept two integer parameters, and what it is going to do is return a string that is the sum of those two integers,3284

surrounded by the bold tags used in HTML.3291

It is going to take in two integers, and it is going to return a string.3296

And then, I want you to call the function from your script and output the result.3299

And what that is going to do is a couple of things: it is going to teach you how to define your own function;3303

it is going to teach you how to use multiple parameters in a function;3310

it is going to teach you how to use a return statement; and then, it is also going to echo3313

the comment that I had about returning strings, as opposed to echoing them directly.3317

Then, what I want you to do is get practice creating function libraries: go ahead and create a function library called mathLIB.php.3323

And put the function within that file; and then, in your main script, when you have moved it out, it gets deleted.3331

And then, use an include_once statement (to get practice using that) to include that LIB file in your script.3338

And then, try calling the function again, to make sure it still outputs the sum, formatted in bold.3344

Just be sure to remember to format the function according to the course's coding conventions.3349

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

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.