Matthew M.

Matthew M.

Introduction to 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)

1 answer

Last reply by: Matthew M.
Wed Apr 11, 2012 4:21 PM

Post by Andrew RAFT on March 25, 2012

Hi Mathew,

I enjoy your video and find them very informative, I would like to know how to output the time function to show the time in real time and show the seconds counting on the web browser.

1 answer

Last reply by: Matthew M.
Tue Feb 7, 2012 3:31 AM

Post by AnhDuc Nguyen on December 29, 2011

mktime({$dateArr['hours']}, {$dateArr['minutes']}, {$dateArr['seconds']},"
. " {$dateArr['mon']}, {$dateArr['mday']}, {$dateArr['year']})

Why using curly bracket ({} in here? I am confused when using {} and when not use?

Introduction to Functions

  • A function is a group of PHP statements that can be executed on request by other PHP code using what’s known as a function call .
  • When reaching a function call, PHP halts execution of the current script, executes the function’s section of code, and then returns to executing the original script.
  • A function call is made by typing name of the function followed by a pair of parentheses.
  • Function arguments are expressions that can be 'passed' to a function by placing them in a comma-separated list between the parentheses of a function call.
  • Functions are themselves expressions and thus have a value. The value of a function is called its return value , and this value is 'returned' to the calling script.
  • Functions can be chained together by using the return value of one function as an argument to another.
  • php.net provides documentation, via a function reference , for all of the built-in functions PHP makes available to programmers. The documentation provides function prototypes , which are descriptions of the arguments a particular function accepts along with what type of data value it returns.
  • Functions in PHP can be defined to take optional arguments . In the php.net function reference, optional arguments are denoted in a function’s prototype by placing them within pairs of square brackets.
  • Additonal Resources:

Introduction to 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:10
    • Lesson Overview
  • What are Functions? 0:51
    • Definition of Faction
    • PHP and Function Call
  • Function Calls 2:42
    • Function Calls
    • Function Arguments
  • Return Values 4:56
    • Return Values
  • Function Chaining 6:29
    • Function Chaining
  • PHP.net Function Reference 8:23
    • PHP.net & Function Prototypes
    • PHP.net Function Reference Example
  • Optional Function Arguments 12:28
    • Optional Function Arguments
  • String Functions 14:57
    • strtoupper() and strtolower()
    • implode (), str_replace(), explode(), strpos(), substr(), and strlen()
  • Array Functions 25:48
    • count()
    • in_array() and array_key_exists()
    • sort() and ksort()
    • Example: count() and in_array()
    • Example: array_key_exists()
    • Example: sort() and ksort()
  • Date & Time Functions 33:38
    • date() and time()
    • getdate()
    • mktime()
    • Date & Time Functions
    • Example: date() and time()
    • Example: getdate()
    • Example: mktime()
  • Homework Challenge #1 44:31
    • Homework Challenge #1
  • Homework Challenge #1 (Cont.) 45:28
    • Homework Challenge #1 (Cont.)
  • Homework Challenge #2 46:34
    • Homework Challenge #2
  • Homework Challenge #2 (Cont.) 48:06
    • Homework Challenge #2 (Cont.)
  • Homework Challenge #2 (Cont.) 49:17
    • Homework Challenge #2 (Cont.)
  • Homework Challenge #3 50:08
    • Homework Challenge #3

Transcription: Introduction to Functions

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

In today's lesson, we will be providing an official introduction into using functions.0004

Specifically, we will be going over what functions are; we will be talking about several different facets of using functions,0012

such as function calls, return values, and function chaining.0019

We are also going to explore a reference on php.net, which is a function reference that provides documentation for all the built-in functions to PHP.0024

And then, part of that is: we are going to go over optional function arguments, which we will learn about as we go through.0034

And then, we are going to cover some common functions in three different groups: string functions, array functions, and then date and time functions.0040

I said the title of this lesson is an official introduction to functions; and the reason I said that is because we have been using functions already.0052

We were first introduced with the phpinfo function that outputs all of the information about a PHP configuration.0061

We have also used other functions, like the var_dump function that outputs the type and values information of a variable.0067

So, we actually have used them before; now, we are just going to officially talk about what a function is.0075

Basically, a function is a group of statements that can be executed on demand.0080

It is essentially a section of code that usually performs a specific task--for example, getting the length of a string.0088

And this is a task you might perform over and over again, so you set it aside in a block of code, and then you name it.0095

And then, what you can do is: you make what is known as a function call,0102

which is a way to invoke that section of code and perform the function that you need.0107

What happens is: when PHP is processing a script, and it reaches a function call, it stops execution of the current script,0114

goes to where the function is defined, runs the function code, and then returns back to the original PHP script after performing the function.0123

Now, as mentioned, PHP has a built-in library of functions that provide commonly-needed tasks,0133

such as working with strings, working with arrays, and working with dates and times--which are all things we are going to learn about in this lesson.0141

There are also many extensions that are built into PHP, such as a MySQL extension for using the database...and so, those are built in, as well.0148

How do function calls look? How do you make a function call?0164

Well, we have used them before with phpinfo and var_dump; essentially, a function call,0168

which requests the execution of the code defined in the function, is simply the name of the function, and then a pair of opening and closing parentheses.0173

In this case, the time function is a built-in PHP function, and it gets a current timestamp of the date and time right at the moment.0185

That is an example of a function with what is called no arguments.0194

There are also a lot of functions...most functions accept arguments.0198

What arguments are is expressions provided in between the parentheses of a function.0203

It's a way to pass data and information into the function, so that it can work on it.0210

For example, here we have a built-in PHP function called strlen; what this function does is returns the length of a string that you pass in as a function argument.0216

In this case, our function argument is the string abc, and so what happens is: when this line of code right here is run,0227

when PHP gets to the strlen function call, it stops execution, goes out to the strlen function code,0239

uses this value right here that was input as a function argument to the function, and returns the length of that string.0247

That is the particular function...the string length function.0254

In this case, it would return the value of 3.0257

One thing to note is that functions expect their argument values to be of a certain type; in this case, for example,0260

the string length--obviously, you pass in a string.0267

And one thing to note is that, if you pass a function arguments that aren't of the correct type,0270

the return value (which we will learn about in a minute) of a function is undefined, which is just stating that the way the function will work is unknown.0278

Another thing to mention is that sometimes I've heard function arguments referred to as parameters.0289

We just mentioned the term return values; what that means is: functions, like variables and literals, are expressions.0298

And because we know from our lesson on expressions, function is something that has a value; a function has a value.0308

What a function's value is known as, is its return value--this phrase right here.0315

What it is: when the function executes, it will return a value to the original script where it was called from.0322

For example, if we have this line of code right here--we have a message variable, and we have a string literal,0329

and we are concatenating to it using the concatenation operator we had learned about, the output,0337

or the value of the strlen function with the argument abc...0345

Now, we just learned that strlen is a function that outputs the length of a string that is passed into it as an argument.0351

So, what is going to happen is: when PHP reaches this section of code right here, it is going to go and evaluate the strlen function.0357

strlen is going to give a return value of 3, so that is the value of the function, and it is going to essentially replace it in line.0364

So, instead of having this anymore, you have 3, because 3 is the value of the function, or the return value of the function.0372

Essentially, this line up here would end up with a message that has a string literal: length is 3.0379

One thing that you can do with functions is known as function chaining.0391

Because functions take expressions as their arguments, and a function, as we just learned, is an expression itself--0394

it has a return value--you can use a function call as an argument to another function.0401

For example, PHP has a built-in function called getdate, and it accepts one argument that is a timestamp0408

(what is known as a UNIX timestamp, which we will get to in a few slides).0418

So, what it does is: it outputs an array of date information.0422

So, what you can do is: you can use the time function, which creates a timestamp, as the argument to the getdate function.0426

What happens, in this case, when this line gets evaluated here: PHP goes to evaluate this function, and then0437

it sees that its argument is a function itself, so it goes out and it finds the time function.0446

It executes the time function, and whatever value that returns--it then passes that value into getdate.0454

And getdate uses that to produce its output.0460

Functions can be chained together as many levels deep as needed.0464

For example, there is also a function called implode that we are going to learn about.0468

You can, for the function implode...what it takes is an array as its second argument, as we will find out.0474

And because getdate returns an array, you can use getdate as its argument.0482

And additionally, we learned that getdate takes a timestamp, and because the time function returns a value that is a timestamp,0487

you can include that as the argument to getdate, and you can include this whole function chain as an argument to the method implode.0495

One nice thing about PHP is that it has a lot of built-in functionality.0505

And at php.net, there is a documentation known as the Function Reference that provides information0509

about all of the built-in functions provided by PHP.0516

Specifically, one of the things it provides are function prototypes, which are basically descriptions of how to use the function.0521

It describes the arguments, and the types of arguments, the function takes, as well as what values it returns.0527

Additionally, the documentation just has some general information about what the different functions do.0534

It is part of the PHP manual, and you can find it at this address, which we are going to go to in a second.0540

It documents all of the core, built-in PHP functions, as well as the PHP functions provided by common extensions.0546

For example, it is common to interact with the MySQL database using PHP; and so that is a common extension.0553

So, in this Function Reference at php.net, you can find information about the functions provided by the MySQL extension.0561

Let's go take a look at the php.net Function Reference.0570

The way that you would get there is: if we go to the PHP homepage, and then select Documentation at the top of the page,0576

and English, it brings you to the PHP manual, which we have gone to before.0587

And if you scroll down the PHP manual, there is a section called Function Reference.0591

If we click on that, that brings us to the Function Reference for PHP.0596

What it does (let me blow it up a little bit) is: it lists, by category, all of the different functions that are built into PHP or provided by extensions.0601

For example, if you scroll down, you can see all sorts of different functions that are provided.0613

When you look in the text processing category, there is a subcategory called strings, which is an extension--0619

a part of the PHP core--that provides string functions.0629

And so, when we go to the strings Function Reference, you can see, there is a link called string functions.0633

If we click on that, what it does is: it lists all of the different string functions that are available to you as a PHP programmer.0643

If we go down, and we look at a function we had talked about (let's see, where is it?), the string length function,0654

and we click on that, here we can see what we just talked about as a function prototype.0661

What this says is: it gives the name of the function, strlen in this case, and then it defines the arguments that the function accepts.0668

In this case, it accepts a string argument; and in the sections below, it describes what the arguments are meant to be.0676

And so, this identifier right here is used below; for example, the first parameter is called string.0686

So, when you go down and look at the More Documentation, and you look at the parameters list, you can see string.0692

It tells you that this argument is the string being measured for length.0698

Additionally, what you can tell is that this function to the left of the function name is its return value.0703

And so, this shows that this function, strlen, returns an integer.0710

And if you look further down, there is also a section on return values; in this case, it says, "The length of the string parameter, on success, is returned."0714

"And if not, if the string is an empty string, it returns 0."0723

This provides information--it gives a description of what the function does, and then it provides information about how you would call the function.0726

You would know to call it by passing it a string as an argument.0734

And it also provides information about knowing what sort of result you should get from it--what value it will give you.0737

In this case, we know that it's an integer value.0742

One other thing that you can do in PHP is: functions can have optional arguments.0749

For example, you might have a function that can take one argument, or it can take two arguments.0757

Or maybe, sometimes, it can take even three arguments.0764

Functions can be specified so that they have required arguments, and then also any number of optional arguments.0769

Well, the PHP manual's Function Reference that we just looked at has a way of denoting optional arguments to functions,0775

which is important so that, when you go to look at the manual to review different functions that you may want to use,0784

you will understand that they are optional functions.0789

If we go and look at the date function...and one of the nice things about the PHP website is: at the top of the screen, it has a search box.0791

It says "Search for___ in the___" and it has a dropdown box, and you can select (what is selected by default: function list).0800

If you type the name of the function that you are looking for (in this case, we are looking for the function date), and you hit Enter,0807

it will bring you to the function description page for the date function.0814

And here, again, we have a function prototype; it says the function's name is date; it returns a string.0819

And then, here is where it says it accepts as its first parameter (which is a required parameter) a string (and they're calling it format).0825

And then, the way it signifies its second parameter as optional is by including it in square brackets.0833

So, because this first parameter is not enclosed in square brackets, this is a required parameter.0838

However, because the second parameter or argument is included in square brackets,0846

that lets you know, as a programmer, that you don't have to provide that.0851

But what it also tells you is that, if you do provide the optional parameter--it tells you that it needs to be of the integer data type.0855

Additionally, for optional arguments, they always have default values.0866

So, here you can see that what happens if you don't provide this optional argument when you call this function:0870

it is going to implicitly assign to this second argument the value of the time function.0878

In this case, it is going to say...if you just call date with one argument, you are going to provide a string to it,0885

and then, for the second parameter, it is automatically just going to use the output of the time function.0890

Now, we are going to talk about some commonly-used functions as they relate to strings.0901

Strings, you work with a lot in PHP; we are constantly outputting and changing around HTML tags.0906

And PHP has a wealth of built-in string functions, and this is a list of some of the common ones.0912

We are going to go through some examples using these functions.0921

These first two--as the name implies, they are used to convert a string that you pass into it as an argument to all uppercase or all lowercase.0925

If we go look at a page created for this lesson called stringFunctions.php, we can see (let's blow this up a little more)0939

a code example of using the strtoupper function.0953

Its use: this is my version of the function prototype; it's string to upper, and it takes one parameter as its value, which is a string.0957

So here, we have an example of calling the strtoupper function, and it would use this syntax here.0968

Here, we are passing it the value Ca, which would be an abbreviation for the state of California.0974

And if we were to output the value of this function, it would output CA.0979

And additionally, here we have the strtolower function, which is similar, except that when you pass it a string value,0988

it converts it all to lowercase letters.0994

Now, one thing to note, if we go and look at the source code behind this, is that in double-quoted strings,0998

we can output variables using variable interpolation.1007

However, even though a function is an expression of value, we can't include a function call within double-quoted strings.1012

So, for example, for this first part--for the strtoupper function example--1020

what we have done is called the strtoupper function on this Ca string, and assigned it to a variable.1026

And then, what we have done is: in our output statement, we have included that variable within our double-quoted string.1034

So, we'll echo it out; and that is because you couldn't replace upper with this--that wouldn't work, and PHP wouldn't know how to evaluate that.1042

However, what you can do, if we look at the strtolower example: instead of having this extra step where we create a variable,1057

and then supply the variable to the echo statement, we can use the concatenation operator (which we had learned about).1063

And so, we put the initial part of our output message; we close that string; and then, we call the strtolower function on the string variable,1070

which here is [email protected], for example.1083

This function is going to go out and run and return the value, which is a string in lowercase.1087

It is going to add that string by concatenation to this original output string, and then it is going to concatenate at the end a break statement.1093

That is one thing to be aware of: you can't include functions within double-quoted strings, as you can variables.1104

Some of the other functions that PHP provides is called implode, which converts an array to a delimited string; we are going to look at that in a second.1113

There is a string replace function, and what you can do with that function is replace all the occurrences1126

of a certain string within another string with a replacement string.1132

So, for example, you have a string maybe 50 characters long, and it contains the character a five times.1139

You could use the str_replace function to replace the character a with b.1145

We also have the explode function, which is the converse of the implode function; it takes a delimited string, and it deconstructs it into an array.1150

There is the strpos function, and what that does is: you pass it a string and a character sequence that you are interested in finding1161

in that string, and it returns the position of where that character sequence occurs in the string.1168

There is the substr function, which is going to allow you to return a specified portion of a string,1175

and then the strlen function, which we have talked about, which returns the length of a string.1181

Let's go take a look at some of these functions in action.1186

If we look at our stringFunctions.php file again in our web browser, here we can see the use of the implode.1188

The way implode works is: you pass it a delimiter, and you pass it an array.1198

What it does is takes all the values from the array and creates a string, where they are delimited by the delimiter that you pass into it.1202

So, in this example, we have passed to implode the colon delimiter, and we have passed an array containing three different values.1211

It has two strings and an integer, and what this implode function would output is right here.1218

And as you can see, it's a string of all the values in the array, delimited by colons.1226

Now, one thing that we could do is: let's say we change our mind, and we don't want to use a colon as the delimiter in the string.1233

Well, we can use the str_replace function to replace it with a different delimiter.1239

The way that works is: it has three arguments.1244

The first one is the string that you want to replace, which can be one or multiple characters.1248

It has the replacement string that you want to put in its place, and then the third argument is the string that you will be searching to do the replacement in.1252

In this case, the string we want to replace is the colon character, and we want to replace it with a comma.1262

And the string that we are going to perform this replacement operation on is the delimited string that we had up here,1270

which is the array values, separated by colons.1277

Once we get the output of this replace function, it is then going to give us this right here, which is a comma-delimited list of the three values in the array.1283

Now, we are going to demonstrate the explode function, which is, again, the converse of the implode function,1296

which takes a delimited string and deconstructs it into an array.1304

The way explode works is: you pass it two values--you pass it, as a second parameter, the string that you want to extrapolate into its parts.1312

And then, you provide the delimiter that you want to use to know where to divide the string up.1321

In our case, because we have replaced the colons with commas in our string, we are going to explode, using the comma as our delimiter.1329

We are going to pass it the comma-delimited string.1340

What that is going to do, as you can see, to the output, is: it is going to output an array of length 3.1343

What it is going to do is: it is going to extract everything before the first comma into the first value of the array.1351

It is going to extract everything before the second comma into the second value of the array, and so on.1359

Here, you can say we have an array of length 3, and the three values are 1000, Baseball Glove, and 29.99.1365

Now, we also mentioned this strpos function, and what that will do is: it takes as an input a string, and then,1375

you provide an additional search string that you want to find out what position that string is located at, or if it even occurs in the string.1383

Here, we provided the string position function with this value $1099.99.1392

We are saying we want to find the position of the decimal point, and what we get for output is the answer 5.1402

Now, if we look here, the period, or the decimal point, is actually the sixth character in this string.1409

We have 5 before it, and then 1; but the reason it outputs 5 is because the strpos function uses a 0 index.1419

It names the first position of a string as being 0, so the dollar sign is 0, 1 is 1, 0 is position 2,1428

the first 9 is position 3, the second 9 is position 4, and then the decimal point is position 5.1436

What we can do is: let's say that, now that we know where the decimal point is, we want to extract out the fractional part of the dollar, or the cents part.1444

Well, we can use the substr function; that takes as its argument a string, and then it takes two integers,1454

one being what position the string you would like to start extracting a substring from is in, and from that position,1461

it uses a third parameter, length, to decide how many characters from that position you gave it you would like to extract.1469

In our case, we pass it the same string, which is $1099.99, and where we want to start is: we want the last two digits in the number.1476

And because we know that the decimal point before is at position 5, and we want to start at position 6,1489

we use as our second parameter 5+1--we would use the position of the decimal point at 5, and we add one to it1499

to say "start at position 6, and then give me a substring of length 2, starting at position 6."1507

And so, our output would be 99, because starting at position 6, the next two characters are 99.1514

And then, just quickly, as we had talked about before, the strlen function outputs the length of a string.1523

In this case, for the decimal portion of the number that we just got, if we had saved it in a variable1529

and then input it to the strlen function, we would get the output of 2, because the string contains 2 characters.1535

Let's go back to those slides...and now, we are going to talk about some different array functions that are commonly used and are built into PHP.1546

The first one is the analog to strlen, but for arrays; it's count, and what that does is returns the number of items or values within an array.1557

Then, there are two search functions, in_array and array_key_exists.1567

What those functions do is return true or false, depending on whether a value passed to those functions exists in that array.1572

In particular, in_array searches...you pass it a value, and it searches all the values in the array to see if the value exists.1580

array_key_exists, on the other hand--whatever you pass it as its value, it searches all of the keys of the array to see if that key exists.1588

Additionally, there are many sort functions, but two ones that we are going to talk about are sort and ksort.1597

What sort does is: it sorts a function based on value; so if all your values were, let's say, integers, in a random order,1606

and you ran the sort function on them, it would resort the array so that the functions1612

were now in ascending numerical order, which is the default sorting order.1616

One thing to note is that this does remix the array, which can be a problem if you use this function on an associative array.1621

We are going to talk a little bit about that in our example; that is going to be one of the homework challenges--1631

to use the Function Reference at php.net to find a function that will sort an associative array by values, that maintains the indexes.1636

Also, we have the ksort function, which, like sort, sorts the function, but it sorts it based on keys, so it sorts the keys in ascending order.1647

This keeps the key indexes.1658

Let's take a look at a file called arrayFunctions.php, which demonstrates some of these different functions.1662

The count function--it simply takes an array as its one argument.1672

And in this case, we are passing to the count function an array with three integers in it: 10, 8, and 9.1677

And it gives us an output of 3, stating that there are three values or items within the array we passed to it.1684

Now, the in_array function takes, as its second parameter, an array you want to search.1692

And then, as its first parameter, it takes a value you want to search for.1697

So, in this example, we are calling the in_array function; we are saying,1701

"I want you to look in this array that has the three elements 10, 8, and 9, and tell me if the value 9 is one of those items in the array."1707

And because it is, it, of course, outputs the value true.1716

Now, you can also do this on associative arrays; for associative arrays, we know that this part1720

to the left of the array assignment operator is the key, and then the part to the right of it is the value.1727

So, when we run in_array on the associative array, it searches all of the values of the array, which are the values to the right of the array assignment operator.1734

So, when we search this array that has three items in it, with each item having the different keys, c, b, and a, and then they have different values,1748

and we are searching if the value Smith exists, this function is going to output true, because, as you can see,1760

the value of the first item in the array, associated with the key c, is Smith.1766

And just to show that this only works on values and not on keys, if we were to run the same function using the same array,1771

but passed it the value c, and said, "Is c in this array?" it is going to return false.1779

So, even though it's a key and it's in the array, it returns false, because in_array only searches the values of an array.1784

Now, on the other side of things, we have the array_key_exists function.1794

What that does is: similarly, it takes an array as input, and then it searches for a particular key (it looks like a type here--this should say key).1800

And then, it returns true or false, whether that key exists.1812

So for example, if we run array_key_exists on a numerically-indexed array that contains the numbers 10, 8, and 9,1816

well, we know from our lesson on arrays that the first item in a numerically-indexed array has index 0;1823

the second item has index 1; and the third item has index 2; and the index is also known as the key.1830

And so, we are saying, "Does the key 2 exist for this array?" and the output is true, because the value 9 has the index, or key, of 2.1838

So now, we are going to try this on the same associative array before.1849

We are going to ask if it has the string b as one of the keys in the array,1852

and it is going to return true, because this second key/value pair has b as its key.1858

And again, to demonstrate that this function only searches keys and not values, if we pass the same array,1865

and then ask it to search for the key John, it is going to return no, even though John is in the array,1871

because John, in this array, is a value and not a key.1876

So now, we are going to talk about the sort function; and what that does is: you pass the sort function an array,1882

and it sorts the array by values; it resorts the array in ascending order for values.1889

If we pass it the array with the numbers 10, 8, and 9, in this random order, the output that the sort function would generate,1898

or what it does to the array we pass it, would be: the 0 element would become 8; the second element would become 9; the third element would become 10.1905

You can see, it orders them in ascending order.1915

Now, similarly, if we run this on the associative array, we can see that it sorts by values, and it makes the first value in the sorted array1918

equal to B., because in ascending order, B. is highest up on the list.1932

Additionally, John would come next in alphabetical order, and then Smith.1939

So as we can see, it sorted on the different values in alphabetical order.1944

Now, the thing to notice, that I mentioned on the slide, was that this re-indexes an array.1948

So, in this case, now these a, b, c keys are lost.1953

And as mentioned, one of the homework examples is going to be to research in the Function Reference1957

to find a function that will sort this array right here, but after sorting it by value, it will maintain the key associations.1961

Then, finally, we have the ksort function; and that is a sort by key.1972

And so, it takes one argument, which is an array; so if we pass the ksort function this associative array we have been using all along,1977

it sorts the array by key, so a is higher than b, and b is higher than c.1987

So, the a key is going to be first in the array now; the b key is going to be second; and the c key is going to be third.1993

And we can see that the values that were associated with each key remain with that key.2001

This function is a way to sort by key and keep the key/value associations together.2009

Now, I just want to wrap up by talking about some date and time functions that are available.2020

PHP has a bunch of built-in date and time functions, the first of which that we are going to talk about is the date function, which returns a formatted date.2027

You can optionally pass a timestamp for the current date and time, and then you pass it a format string.2038

And based on this string that you pass to it, it will output or format the string in a particular way.2045

We have the time function, which returns what is known as a UNIX timestamp.2053

We are not going to really get into the details of the UNIX timestamp, but basically, what a UNIX timestamp is, is an integer number2058

that represents the current date and time when the function is run.2065

So, when you run the function time, it returns an integer of time right now, and that integer is known as a UNIX timestamp.2070

Then, we have the getdate function, and what that does is: you pass it a timestamp, and from that timestamp,2081

it can separate into parts the different parts that the timestamp represents.2089

It can separate into hours, minutes, seconds, the day, year, the month, and so forth, and it returns it as an associative array.2093

Also, there is the make, or mktime function, that you can use to build a UNIX timestamp from supplied date values.2102

One thing to note is that date and time functions have a couple of directives within php.ini that they use to configure how they work--2113

in particular, the date.timezone directive; and what you do is: you can go and set that to a particular time zone identifier value,2122

which can be found at this location here, and that will treat all of your date and times as if they were in that time zone.2136

If we go and look at dateFunctions.php, we can see examples of all of these different functions that we just talked about.2146

One thing that you will notice is...before we get into the details of date, I just want to talk about the timezone directive.2160

Basically, what this function does is outputting the current date and time, and it also outputs the time zone.2168

And right now, the time zone output is CEST, which refers to Europe--Berlin, because that is the default value that was set in php.ini in the XAMPP configuration.2174

So, because we are filming on the West Coast for this course, we are going to go ahead and update the php.ini file.2187

And what you can do is: it's quite a long file, but if you search far down in the page, there will be a date section,2196

if I can find it; there it is; and as you can see, it has the date.timezone directive, and it is set by default, in XAMPP, as Europe--Berlin.2212

What we are going to do is: we are going to set it to West Coast time--so we are going to give it the value america/los_angeles.2230

And the reason we use america/los_angeles is because that is one of the allowed time zone identifiers, or one of the identifiers that it recognizes.2240

The way that you can do that is by going to the link that we had in our slide.2250

For example, if we go to this link right here, it will tell you all of the different time zones that are available.2253

We have gone ahead, and we have updated our default time zone.2261

Because it is a change to php.ini, we need to reboot or restart Apache, so that the change will take effect.2264

And if we restart it and go back and look at our date function that output the abbreviated time zone identifier for Europe-Berlin,2275

and we refresh the page, we can see that it changed to PDT, which is the Pacific time zone.2283

Now, where you can find these is on the link that we had before, which can be found by...2294

if you look up the date and time extension in the PHP manual, you can find the list of supported time zones,2306

which...it would be easier for you to just cut and paste the link, if you had it before.2312

And you can see that it has listed different continents and areas in the world, and you can click on an area, such as America,2316

and see the different values that you are allowed to use.2323

If you look down here, you can see that, for West Coast time (let's see, where is it? My eyes aren't serving me well--oh, it was off the screen)...2326

You can see that they have one for America, Los Angeles, and you can see that it uses an underscore.2352

But for example, if you were on the East Coast, you could use the america/new_york to use East Coast time.2357

So, now that we have set the time zone for this installation of PHP, let's talk about the different functions we had mentioned.2369

Basically, the date function returns a formatted date string.2378

It can take two parameters; and actually, if we go and look at the date function in the PHP manual,2382

and again, we are going to use the search box up here--that is easier, we can see that it takes a string as its first parameter.2390

And optionally, it takes a UNIX timestamp as its second parameter.2397

And the parameter defaults to time, which is the current unit timestamp, if not provided.2402

If we run this function date, which we are here, with just one parameter, it is going to format our date based on the current time.2408

And as you can see, it explains that in the description: "Returns a string formatted according to the given format string,2416

using the given integer timestamp, or the current time, if no timestamp is given," which is what happened in our case.2422

Now, this parameter called format, which is a string--you can see down here,2430

it provides a lengthy description of all of the different values that you can include in this format string.2434

There are all sorts of different things that you can do: you can use it to output days, months, years, seconds...2440

You can do it with leading zeros or without; and there are just different ways to extract and format different pieces of a timestamp.2447

So, in our example, we have gone ahead and used the formatting string right here.2457

And on your own, you can go ahead and look and research, and see how this format string translates into the output that we get here2464

by looking at this format parameter description; it shows all of the different...basically, they are just like formatting characters...2473

that you can use to output certain parts of the date.2482

In this case, this formatting string formats the current time, because we didn't provide a second parameter.2485

And it says "output the day of the month, the month, the year, a comma, and then output the hour, minutes, seconds, using AM or PM."2491

So, we are not using military time; and then, "also output an abbreviated form of the time zone identifier."2502

Now, as mentioned, the time function, which is our next function we are going to talk about, returns a UNIX timestamp2509

for the current date and time, which...when we run this script right now, and we refresh the page, is going to change.2515

And as mentioned before, a UNIX timestamp is an integer that computers know how to work with,2522

and can decode into the date and time that it represents.2528

That is how the time function works.2534

The getdate function takes as an input a timestamp; and in this case, we could run the getdate function with the time function as its parameter.2536

And so, time is going to generate, as its return value, the current timestamp.2549

And so, getdate knows how to take that timestamp and extract it into different parts, and it outputs an array.2553

And as you can see, it's an associative array that contains all sorts of different keys--seconds, minutes, hours, month, year, and so forth,2561

and extracts the date information and the timestamp.2569

You can see that it knows that, when this getdate function is called, the hour was 4; the minutes were 9; and the seconds were 46 seconds.2572

And then, you can also see that it was June 24, 2011.2586

Now, what you could do is: now that you have these date values, you can use the mktime function to build up a UNIX timestamp.2596

There are different functions for going back and forth between UNIX timestamps.2602

The mktime function works by passing in...it has a specific order of arguments it takes.2606

It first takes hours, then minutes, then seconds, the day and the month and the year, and it will generate a UNIX timestamp based on that.2611

If we go ahead and pass it, the values that we got in this array up here, dateArray, which was received from the current date and time,2620

and we pass it those values--you can see that it generates a UNIX timestamp.2632

And if we notice, because we use the same values that were generated by the getdate function,2636

we should generate the same UNIX timestamp as was used in the getdate function.2644

And as we can see, part of the getdate associative array that it returns--the last value in the array,2651

with the key of 0, is the UNIX timestamp that it used to dissect all of the different parts of.2658

And as we can see, because we use the same parts down here in mktime, these two values match up.2664

That ends the course material; I just want to talk about a couple different homework challenges that we have for you,2673

that are going to give you some experience working with functions and calling functions in more detail,2679

and using some of the functions that we went over.2683

In this homework example, which you will be able to look over, you can see that we are just going to have you convert string to uppercase and lowercase.2687

And then, practice chaining functions--for example, if you pass an uppercase string to the lowercase function, it would return it lowercase.2694

And then, you pass that value to the uppercase function; it returns it to uppercase.2708

So, by chaining those two functions together, you essentially get the original value.2712

That is one of the things that we are going to have you do.2716

And then, I will have you create an array that just contains five integers,2717

using float functions to create a string where the integers are separated by colons.2724

And then, like we did in the course, I will have you replace the colon delimiter with commas.2730

And then, we are going to have you use the strpos function in a little bit different way than we used it in the course,2735

to find the position of the first comma in our now-comma-delimited string.2740

And then, we are going to use the substr function to remove the first integer in our comma-delimited string.2745

So, we are going to have to remove the integer, as well as the comma that precedes it.2754

So, just remember, when you do that, that these string position and substring functions consider the first character to be 0 and not 1.2761

That will give you a little experience with that; and it is kind of challenging, and a little tricky, at first,2770

getting used to using these strpos and substr functions, so it should be a good exercise.2773

Then, I just want to have you run the explode function on our now-shortened list of integers.2779

And then, because explode generates an array, we will have you use the count function to verify that the array length is now 4, instead of 5.2784

For the second challenge, I am going to have you work with some of the array functions we learned about.2796

I am going to have you create an associative array of the 12 months in the year, in ascending order,2800

so that it starts with January and ends with December, where the key is a string value of the number of the month.2805

And then, the value is the month name spelled out.2814

Make sure that you declare them in ascending order.2819

And then, I want you to look at the php.net Function Reference (and this is to give you practice in learning to use the reference)2821

to find a function that will sort the array in reverse order.2828

Right now, it is sorted from January to December, and I want it to sort the other way around.2834

One way to quickly find the array functions that are available--a little hint I'm going to give you is that, if you type ksort,2840

which we know sorts an array by keys--if you type that in the Function Reference box at the top of the page,2846

if we go and do that quickly...instead of having to search the Function Manual for all of the different array functions,2851

we can see that, because ksort is an array function, that on the left-hand side, it lists all of the other array functions that are available.2861

I want you to go through and find one of these functions that will reverse sort an array.2867

Have it sort by the keys, which are numeric.2877

And then, you verify that by printing out the array to make sure that it's in reverse order.2883

Then, resort the array in ascending order back to its original version, using the ksort function that we had learned about.2888

And verify that that is done with print_r, and then sort the array by value.2896

What that is going to do is: it is going to sort the array according to the value, which is the month name.2902

However, I don't want you to use the sort array; I want you to research in the Function Reference again2912

to find a function that will sort an associative array by value, but keep the key associations,2918

because if we run the sort function on it, what it is going to do is: it is going to sort the months according to value,2924

which would make April the first one, but it's going to give it an index of 0.2930

In order to keep the index (for April, which is the fourth month) of 4, you need to find a new function that will maintain that index.2934

After you find that function and apply it, use print_r to verify the sort by value.2941

And, if it is done correctly, April should be the first month, with a key of 4, and September should be the last month, with a key of 9.2948

Then, I'm just going to have you practice with the array_key_exists and in_array functions2959

to verify that January is not a key in array, but a value, and that it is a value in array.2964

If you did a search on array_key_exists, it should come up false; if you did a search on in_array using the value January, it should come up true.2972

One thing to note is that these functions return boolean values, and if you try to echo a boolean value,2981

as we learned from our type casting course, the echo statement implicitly casts a boolean value to its string equivalent.2987

And for true, we know that it gets converted to 1, and for false, that gets converted to the empty string.2995

Just keep that in mind when you try to echo the results of these arrays, so that you will understand that it is not going to output true or false.3000

Finally, for our last homework challenge, I am going to have you work with the date and time functions.3010

The first thing I would like you to do is update the date.timezone directive in your php.ini file to your current time zone.3014

And remember to restart Apache after you make the change, because any configuration changes require a restart of the web server.3024

What I would like you to do is use the date function to output the current time in 24-hour format, which would just be the hour and the minutes.3033

And include an abbreviated time zone identifier, so that you can know that the change you made up here3044

to the time zone in the php.ini file works.3049

And what you will need to do to do this is: you will need to research the date function in the Function Reference3054

to learn how to set that first format parameter you pass to date to get it to output the things you want.3062

So, look back at the Function Reference; if you go to the date function, you will have to search this section down here,3067

that talks about the format parameters, to learn about the different format characters you use in order to output a date in the format you would like.3077

Then, I would like you to use the getdate and time functions and chain them together, so that getdate3088

will output an array of the current date and time information.3093

And then, using that getdate array like we did in class, I want you to use the date in that array with the mktime function3097

to generate a UNIX timestamp that matches the timestamp that was used when you called the getdate function...3106

as mentioned, you are going to use the time function.3114

That is going to sort of replicate what we did in the course; and if things work out right, as we have shown,3117

the timestamp that you generate from mktime should match the last element in the getdate array, which has a key value of 0.3123

Those two should match up.3132

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

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.