230 likes | 340 Views
The University of Tennessee at Chattanooga C. Daniel Chase. An Introduction to PHP.
E N D
The University of Tennessee at Chattanooga C. Daniel Chase An Introduction to PHP “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.”
Overview • Short History • Sample PHP Code & Output • Documentation Resources • Web Applications • Language Basics • Functions • Arrays • A Real World Application
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'
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
PHP Popularity • August 2004 • 16,946,328 Domains • 1,348,793 IP Addresses
A Sample Program • The traditional... “Hello World” • The Source
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
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
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()
Data Types... • Strings • single quote delimiter – no expansion • double quote delimiter – variables expanded • escape sequences with \ • is_string() • Booleans • true • false • is_bool()
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”;}
Other Data Types • Objects • Resources • null
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;
Scope • Local – declared Inside a function • global – declared Outside a function • static – retains value between calls • Function parameters • Sample Variables
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 (!) Operators • Addition (+) • Subtraction (-) • Multiplication (*) • Division (/) • Modulus (%) • Negation (-) • String concatenation (.) • Pre- & Post-increment (++) • Pre & Post-decrement (--)
Assignment • Assignment (=) • Plus-equals (+=) • Minus-equals (-=) • Divide-equals (/=) • Multiply-equals (*=) • Modulus-equals (%=)
Miscellaneous Operators • Error suppression (@) • Execution (` ... `) • Conditional or Ternary operator (?:)
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;
Flow Control - switch • switch ($name) {case 'test1': // do somethingbreak;case 'test2': // do something else break;case 'test3': // do something differentbreak;} • Or use : and endswitch instead of block
Flow Control - while • $total = 0;$i = 1;while ($i < 10) { $total += $i;} • while ($i < 10) : $total += $i;endwhile; • break [levels] • continue [levels] • do ... while
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”;} • exitandreturn
Including Code • include header.php • require footer.php • URL's:include 'http://some.other.host/header.php'
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']