an introduction to php the university of tennessee at chattanooga c. daniel chase “an introduction...

23
An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic pages in comparison to static HTML, including a short history of PHP, statement syntax, basic program logic statements and data types, simple PHP pages and HTML forms.”

Upload: clifton-stafford

Post on 26-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

An Introduction to PHP

The University of Tennessee at Chattanooga

C. Daniel Chase

“An introduction to basic PHP use with a focus on the power of dynamic pages in comparison to static HTML, including a short history of PHP, statement syntax, basic program logic statements and data types, simple PHP pages and HTML forms.”

Page 2: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Overview

● Short History● Sample PHP Code & Output● Documentation Resources● Web Applications● Language Basics● Functions● Arrays● A Real World Application

Page 3: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

The History of PHP

● Created by Rasmus Lerdorf● June 1995 –

Personal Home Page Tools (PHP Tools) version 1.0 released– CGI-based– Original intent was Not to create a scripting language

● April 1996 -PHP version 2 (PHP/FI) released– Now called a 'scripting language'

Page 4: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

The History of PHP

● Zeev Suraski & Andi Gutmans volunteer to the underlying parsing engine

● June 1998 - PHP Version 3.0 released– In use on over 70,000 web sites

● May 2000 - PHP Version 4.0 released– Zend Engine introduced

● July 2004 – PHP Version 5.0 released

Page 5: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

PHP Popularity

● August 2004– 16,946,328 Domains– 1,348,793 IP Addresses

Page 6: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

A Sample Program

● The traditional... “Hello World”● The Source

Page 7: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Things to notice

● File type: .php● Normal HTML tags● <?php ... ?> Tag surround PHP sections● PHP lines end with semicolon ; usually...● Your first PHP command: echo● Comment lines

– shell style #– c++ style //– c style /* ... */

● White space● Case sensitivity: Variables Only

Page 8: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Resources

● Main site - PHP.net● Commercial Support - Zend.com● Smarty Template Engine – Smarty.php.net● Documentation

– All versions - php.net/docs.php– Web-based– Download

● HTML● Windows CHM

Page 9: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Data Types

● Integers– decimal default– octal: leading 0 and only 0-7– hex: 0x prefix and 0-F– is_int()

● Floating-Point Numbers– normal: 3.14– scientific notation: 0.314E1– is_float() or is_real()

Page 10: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Data Types...

● Strings– single quote delimiter – no expansion– double quote delimiter – variables expanded– escape sequences with \– is_string()

● Booleans– true– false– is_bool()

Page 11: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Data Types...

● Arrays– $person[0] = “Edison”;$person[1] = “Wankel”;

– $creator['Light bulb'] = “Edison”;$creator['Rotary Engine'] = “Wankel”;

– $person = array('Edison', 'Wankel');$creator = array('Light bulb' => 'Edison', 'Rotary Engine' => 'Wankel');

– Looping:foreach ($person as $name) { echo “Hello, $name\n”;}foreach (creator as $invention => $inventor) { echo “$inventor created the $invention\n”;}

Page 12: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Other Data Types

● Objects● Resources● null

Page 13: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Variables

● Names– Begin with $– Case Sensitive– First character must be Alpha, underscore (_) or ASCII

0x7f to 0xff– Other characters add digits also

● Variable Variables– Name of variable stored in aother variable– $foo = 'bar';$$foo = 'baz';

● Variable References– $black =& $white;

Page 14: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Scope

● Local – declared Inside a function● global – declared Outside a function● static – retains value between calls● Function parameters● Sample Variables

Page 15: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Operators

● Addition (+)● Subtraction (-)● Multiplication (*)● Division (/)● Modulus (%)● Negation (-)● String concatenation (.)● Pre- & Post-increment (++)● Pre & Post-decrement (--)

● Equality (==)● Identical (===)● Inequality (!= or <>)● Not identical (!==)● Greater than (>)● Less that (<)● Greater than or equal (>=)● Less than or equal (<=)● Logical AND (&&, and)● Logical OR (||, or)● Logical XOR (xor)● Logical negation (!)

Page 16: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Assignment

● Assignment (=)● Plus-equals (+=)● Minus-equals (-=)● Divide-equals (/=)● Multiply-equals (*=)● Modulus-equals (%=)

Page 17: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Miscellaneous Operators

● Error suppression (@)● Execution (` ... `)● Conditional or Ternary operator (?:)

Page 18: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Flow Control - if

● if ($good) { print ('Dandy!); $real_good = true;} elseif ($error) print ('Oh, No!');else print (“I'm ambivalent...”);

● if ($user_validated) : print ('Welcome!); $greeted = 1;else : print (“Access Forbidden!”); exit;endif;

Page 19: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Flow Control - switch

● switch ($name) { case 'test1': // do something break; case 'test2': // do something else break; case 'test3': // do something different break;}

● Or use : and endswitch instead of block

Page 20: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Flow Control - while

● $total = 0;$i = 1;while ($i < 10) { $total += $i;}

● while ($i < 10) : $total += $i;endwhile;

● break [levels]● continue [levels]● do ... while

Page 21: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Flow Control - for

● for ($counter = 0; $counter < 10; $counter++) echo “Counter is $counter”;

● endfor● break● continue● foreach ($array as $current) { echo $current;}

● foreach ($array as $key => $value) {echo “Value or $key is $value”;}

● exit and return

Page 22: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Including Code

● include header.php● require footer.php● URL's:include 'http://some.other.host/header.php'

Page 23: An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic

Web Applications

● Retrieving Form data: $_REQUEST['field name']● Retrieving Server info: $_SERVER['field name']● Sending headers: header('location: http://localhost/')

● Cookies: setcookie(name [, value[, expire [, path [, domain [, secure]]]]]]);

● Retrieve Cookies: $_COOKIE['cookie name']