MDIA 4325 Examples

NOTE: Some of the text examples presented here have been adjusted.
See the notes for explanations.

Page Index

Unit 1: Getting Started with PHP
Unit 2: Variables
Unit 3: HTML Forms and PHP
Unit 4: Using Numbers
Unit 5: Using Strings
Unit 6: Control Structures
Unit 7: Using Arrays
Unit 8: Creating Web Applications
Unit 9: Cookies and Sessions
Unit 10: Creating Functions
Unit 11: Files and Directories
Unit 12: An Introduction to Databases

Unit 1: Getting Started with PHP

Text Examples

Example 1-1: Welcome to this Page!
Example 1-2: First PHP Script
Example 1-3: Hello, World!
Example 1-4: Hello, World! with Formatting
Example 1-5: Using Comments

Notes Example

Example 1-a: Hello World with Formatting

Index

Unit 2: Variables

Text Examples

Example 2-1: Using Print_R()
Example 2-2: Variable Comments
Example 2-3: Variables
Example 2-4: Quotes

Notes Examples

Example 2-a: String Variables
Example 2-b: Numeric Variables
Example 2-c: String Variables
Example 2-d: Variable Types
Example 2-e: Casting a String
Example 2-f: Using Variables
Example 2-g: Advanced Variables

Index

Unit 3: HTML Forms and PHP

Text Examples

Example 3-1 Form: Basic Feedback Form
Handled by handle_form.php (example 3-3.php)
Example 3-2 Form: Feedback Form - Method "Post"
Handled by handle_form.php (example 3-3.php)
Example 3-2-2 Form: Feedback Form - Display Errors
Handled by handle_form2.php (example 3-4.php)
Example 3-2-3 Form: Feedback Form - Error Reporting
Handled by handle_form3.php (example 3-5.php)
Example 3-6 / Example 3-7 (hello.html / hello.php): Greetings!

Notes Examples

Example 3-a: HTML Form
Handled by example3-b.php
Example 3-c: Form Returning Input to the Same Page
Example 3d: Calling the Same Page
Example 3-e: Feedback Form
Handled by example3-e1.php
Example 3-f: Feedback Form with Error Handling
Handled by example3-f1.php
Example 3-g: Feedback Form with Error Reporting
Handled by example3-g1.php
Example 3-h: Get Information to create Dynamic Links
NOTE: The following pages all refer to Example 3-h
Example 3-i: Links Generated Based on User Input -- Links Selection Page
Example 3-j: User Data from Generated Links -- Contact Information
Example 3-k: User Data from Generated Links -- Work Information
Example 3-l: User Data from Generated Links -- Hobby Information
Example 3-m: Form Mailer with Thank You Page
Handled by example3-n.php
"Thank You" example3-nThanks.html
Example 3-o: Form Using PHP_SELF and mail()

Index

Unit 4: Using Numbers

Text Examples

Example 4-1: Product Cost Calculator
Handled by example4-2.php (handle_calc.php)
Example 4-3 Form: Using number_format()
Handled by example4-3.php (handle_calc2.php)
Example 4-4 Form: Using Parentheses Form
Handled by example4-4.php (handle_calc3.php)
Example 4-5 Form: Incrementing a Number
Handled by example4-5.php (handle_calc4.php)
Example 4-6: Generate 3 Random Numbers

Notes Examples

Example 4-a: Useful Arithmetic Operations
Example 4-b: Using Comparison Operators
Example 4-c: Using Boolean Operators with Arithmetic Functions

Index

Unit 5: Using Strings

Text Examples

Example 5-1: Forum Posting - Concatenating Strings
Handled by example5-2.php (handle_post.php)
Example 5-3: Handling New Lines - Using nl2br()
Handled by example5-3.php (handle_post2.php)
Example 5-4: PHP String Variables - htmlentities() and strip_tags()
Handled by example5-4.php (handle_post3.php)
Example 5-5: Encoding and Decoding Strings - Using urlencode()
Handled by example5-5.php (handle_post4.php)
Example 5-6: Finding Substrings - Using str_word_count() and substr()
Handled by example5-6.php (handle_post5.php)
Example 5-7: Replacing Parts of a String - Using trim() and str_ireplace()
Handled by example5-7.php (handle_post6.php)

Notes Examples

Example 5-a: Using Heredoc
Example 5-b: Heredoc with Browser Detection
Example 5-c: Heredoc with Global Variables Added

Index

Unit 6: Control Structures

Text Examples

Example 6-1: Registration Form Registration - Using if Conditional
Handled by example6-2.php (handle_reg.php)
Example 6-3: Registration - Using if and empty()
Handled by example6-3.php (handle_reg2.php)
Example 6-4: Registration - Using if-else
Handled by example6-4.php (handle_reg3.php)
Example 6-5: Registration - Validating Password and Year
Handled by example6-5.php (handle_reg4.php)
Example 6-6: Registration - Using Multiple and Nested Conditions
Handled by example6-6.php (handle_reg5.php)
Example 6-7: Registration - Using if-elseif-else Conditional
Handled by example6-7.php (handle_reg6.php)
Example 6-8: Registration - Using Switch Conditionals
Handled by example6-8.php (handle_reg7.php)
Example 6-9: Registration - Using for Loop
Handled by example6-2.php (handle_reg.php)

Notes Examples

Example 6-a: The if Statement 1
Example 6-b: The if Statement 2
Example 6-c: The else Statement
Example 6-d: Checking the Time using else
Example 6-e: The else if Statement 1
Example 6-f: The else if Statement 2
Example 6-g: Switches
Example 6-h: The break Statement 1
Example 6-i: The break Statement 2
Example 6-j: The default Statement
Example 6-k: The exit Statement
Example 6-l: Fortune Teller
Handled by example6-l.php: Fortune Teller
Example 6-l-1: The Fortune Teller using $submit
Example 6-m: The Ternary Operator
Example 6-n: Using Conditionals
Example 6-o: The while Loop
Example 6-p: The do...while Loop
Example 6-q: The for Loop

Index

Unit 7: Using Arrays

Text Examples

Example 7-1: A Soups Array using print_r
Example 7-1b: A Soups Array using var_dump
Example 7-2: Using count()
Example 7-3: Using a foreach Loop
Example 7-4a: A Multidimensional Array using print()
Example 7-4b: A Multidimensional Array using a foreach Loop
Example 7-4c: A Multidimensional Array using var_dump()
Example 7-5: Sorting an Array -- Gradebook - Using arsort () and ksort()
Example 7-6: Sorting an Array -- Form: Alphabetized List
Handled by example7-7.php (list.php)
Example 7-8: Add an Event -- Form: Checkbox Input
Handled by example7-9.php (event.php)

Notes Examples

Example 7-a: Displaying an Array
Example 7-b: Using foreach
Example 7-c: Using in_array()
Example 7-d: Using count()
Example 7-e: Using sort()
Example 7-f: Using Print_r()
Example 7-g: Using array_rand
Example 7-h: Working with Arrays -- The Dresser
Example 7-i: Sorting an Array
Example 7-j: Using array_multisort
Example 7-k: Where do You Live?
Example 7-l: Counting Elements
Example 7-m: User Information
Example 7-n: Displaying Elements in an Array -- I
Example 7-o: Displaying Elements in an Array -- II
Example 7-p: Replacing Elements in an Array
Example 7-q: A Simple Array
Example 7-r: An Associative Array

Unit 8: Creating Web Applications

Text Examples

Example 8-01: A PHP Template
Example 8-02: Header Include File with CSS for Example 8-04
Example 8-03: Footer Include File for Example 8-04
Example 8-04: Template Page with Header and Footer (uses Example 8-02 and Example 8-03)
Example 8-05: Using a PHP Constant
Example 8-06: Using Conditionals and a Constant
Header for Example 8-06
Example 8-07: Using the date() Function in the Header
Header for Example 8-07
Example 8-08: A Simple Login Page
Example 8-09: Registration Form with Data Persistence ("Sticky" Forms)
Example 8-10: Registration Form with mail()
Header File for Example 8-11
Footer File for Example 8-12
Example 8-13: Registration Form using header()
Redirect Page for Example 8-13
Example 8-14: Welcome Page After Login

Notes Examples

Example 8-a: Using Includes for Design

Files Related to Example 8-a

  • header.html: The Header File for Example 8-a
  • footer.html: The Footer File for Example 8-a
  • style.css: The External CSS Style Sheet for Example 8-a
  • nav.html: The Navigation Include for Example 8-a
Example 8-b: Formatting Dates
Example 8-c: Using strftime()
Example 8-d: Using getdate() with an Array
Example 8-e: Using getdate() Inline
Example 8-f: Date Difference and mktime
Example 8-g: Displaying Local Time
Example 8-h: Persistence of Data in Forms
Example 8-i: Form Illustrating Headers Sent Problem
Processing page for Example 8-i
"Thank You" page for Example 8-i
Example 8-j: Headers Sent Problem Solved with Output Buffering
Processing page for Example 8-j
"Thank You" page for Example 8-j
Example 8-k: Redirect with Warning
Example 8-l: Redirect Without Warning

Index

Unit 9: Cookies and Sessions

Text Examples

Example 9-1: Customize Your Settings
Example 9-2: Reading from Cookies (Viewing your Settings)
Example 9-3: Customize Your Settings (Adding Parameters to Cookie)
Example 9-4: Reset Your Settings
Example 9-5: How Session Data is Stored (text file)
Example 9-6: Login Page
Example 9-7: Welcome Page
Example 9-8: Logout Page

Notes Examples

Example 9-a Form: Form for Cookies
Example 9-a: Processing Page for Example 9-a Form
Example 9-b: Counting Site Visits
Example 9-c: Counting Site Visits (Advanced)
Example 9-d: Simple User Login
Example 9-e: Using Session Variables
Example 9-f: Destroying a Session
Example 9-g: A Practical Example using $_SESSION[]
Example 9-g-1: A Secured Page for $_SESSION["access"] NOTE: This page will respond "Access not allowed" because you have not been authenticated via Example 9-g.
Example 9-h: Using session_save_path()
Example 9-i: Using session_encode()
Example 9-j: Using session_decode()

Index

Unit 10: Creating Functions

Text Examples

Example 10-1: Date Menus - A Function Creating 3 Dropdown Menus
Example 10-2: Sticky Inputs (Using Arguments)
Example 10-3: Sticky Text Inputs (Required Arguments)
Example 10-4: A Cost Calculator with Arguments
Example 10-5: A Cost Calculator with Scoping

Notes Examples

Example 10-a: User Defined Functions
Example 10-b: Returning Values from a Function
Example 10-c: Function Arguments
Example 10-d: Passing a Variable as an Argument to the Function -- 1
Example 10-e: Passing a Variable as an Argument to the Function -- 2
Example 10-f: Passing the Variable as a Reference
Example 10-g: The Global Variable
Example 10-h: Using $GLOBALS
Example 10-i: Using substr()
Example 10-j: Using mktime()
Example 10-k: Form for Example 10-l
Example 10-l: Form Handler for Example 10-k
Example 10-m: "You Better Pay Up" Loan Services
Example 10-n: Handler for Example 10-m
Example 10-o: Customer Service Form -- Click on the links below to see the comments received.

Files related to Example 10-o

Comments from Customer Service Form
Suggestions from Customer Service Form
Complaints from Customer Service Form

Index

Unit 11: Files and Directories

Text Examples

Example 11-1: Writing to a Text File
Download the file quotes.txt.
Example 11-2: Locking a Data File
Download the file quotes.txt.
Example 11-3: Reading from a Text File
Example 11-4: Upload a File
Example 11-5: Reading Directory Contents
Example 11-6: Registration Form
Download the file users.txt.
Example 11-8: Login Page NOTE: This login page reads the username and password that was entered in Example 11-6 (from the file users.txt).

Notes Examples

Example 11-a: Input Form: Handled by Example 11-a.php

Files related to Example 11-a

Example 11-a.php: Form Handler for Example 11-a.htm

file.txt Click here to read file.txt.
Example 11-b: Reading Directories
Example 11-c: Getting File Statistics
Example 11-d: Using fgets()
Example 11-e: Using fgetss()
Example 11-f: Using fgetss() with htmlentities()
Example 11-g: Using fseek(), fgets(), and fpassthru()
Example 11-h: Using ftell()

Index