1 / 23

An Introduction to PHP

The University of Tennessee at Chattanooga C. Daniel Chase. An Introduction to PHP.

Télécharger la présentation

An Introduction to PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.”

  2. Overview • Short History • Sample PHP Code & Output • Documentation Resources • Web Applications • Language Basics • Functions • Arrays • A Real World Application

  3. 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'

  4. 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

  5. PHP Popularity • August 2004 • 16,946,328 Domains • 1,348,793 IP Addresses

  6. A Sample Program • The traditional... “Hello World” • The Source

  7. 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

  8. 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

  9. 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()

  10. Data Types... • Strings • single quote delimiter – no expansion • double quote delimiter – variables expanded • escape sequences with \ • is_string() • Booleans • true • false • is_bool()

  11. 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”;}

  12. Other Data Types • Objects • Resources • null

  13. 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;

  14. Scope • Local – declared Inside a function • global – declared Outside a function • static – retains value between calls • Function parameters • Sample Variables

  15. 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 (--)

  16. Assignment • Assignment (=) • Plus-equals (+=) • Minus-equals (-=) • Divide-equals (/=) • Multiply-equals (*=) • Modulus-equals (%=)

  17. Miscellaneous Operators • Error suppression (@) • Execution (` ... `) • Conditional or Ternary operator (?:)

  18. 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;

  19. 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

  20. Flow Control - while • $total = 0;$i = 1;while ($i < 10) { $total += $i;} • while ($i < 10) : $total += $i;endwhile; • break [levels] • continue [levels] • do ... while

  21. 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

  22. Including Code • include header.php • require footer.php • URL's:include 'http://some.other.host/header.php'

  23. 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']

More Related