Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Lawrence 7/8/091Intro. to PHP -- David Lawrence.

Similar presentations


Presentation on theme: "David Lawrence 7/8/091Intro. to PHP -- David Lawrence."— Presentation transcript:

1 David Lawrence 7/8/091Intro. to PHP -- David Lawrence

2 What is PHP?  PHP = PHP: Hypertext Preprocessor (a recursive acronym) 7/8/09Intro. to PHP -- David Lawrence2  “ PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.” = WWW

3 Viewing sites on the World Wide Web requires both a Server and a Client 6/10/08David Lawrence JLab3 Client software runs on users’ computers Server software runs on a different computer Server sends coded information and the client (browser) decides how to display it.

4 Decisions, decisions, ….  Dynamic content requires a program to run somewhere.  Should it run server side or client side?  Server side: PHP, ASP (active server pages), JSP (Java sever pages), CGI (common gateway interface) (used for perl).  Client side: HTML, Javascript, ActiveX, FLASH, Applets 7/8/09Intro. to PHP -- David Lawrence4

5 PHP is a good choice because…  It currently ranks 4 th on the TIOBE Programming Community Index (behind Java, C, and C++)  Well documented and well supported with a shallow learning curve  Server-side is more browser independent than client-side  PHP is open source (i.e. free!) 7/8/09Intro. to PHP -- David Lawrence5

6 Getting started Note: This tutorial does not cover installation of PHP and configuration of the web server.  PHP code is contained in files stored where the server can access them.  Usually, the server is configured to pass any files with names ending in “.php” through the PHP parser. 7/8/09Intro. to PHP -- David Lawrence6 php goes inhtml comes out  Files can be edited with any text editor (some are better than others)

7 Hello World! 7/8/09Intro. to PHP -- David Lawrence7 The PHP code is embedded between the tags

8 Embedding PHP in HTML 7/8/09Intro. to PHP -- David Lawrence8 One easily flow in and out of PHP mode with the tags The parser will replace everything inside the tags with the output of the code they contain Hello World The message of the day is: Hello World! PHP parser PHP file What the browser sees (Browser never actually sees the PHP code!)

9 Including other files  One can “include” one PHP file in another to recycle code  Multiple options for including files: include(“file.php”) require(“file.php”) include_once(“file.php”) require_once(“file.php”) 7/8/09Intro. to PHP -- David Lawrence9 Usually, you want to use require_once() warn if file not found fatal error if file not found include only once (even if nested includes cause multiple requests)

10 PHP variables  Variables are always prefixed with a “$”  The variable type is implied by the context of how it’s used (but you generally don’t care exactly what type it is!)  Variable values carry from section to section when parsing file(s) for a single page, but not necessarily when loading a different page  Undefined variables resolve to 0 or an empty string (they don’t give errors!) 7/8/09Intro. to PHP -- David Lawrence10

11 HTML Forms 7/8/09Intro. to PHP -- David Lawrence11 The tags are a short- hand to printing variables The special variable “$_REQUEST” is an array containing values passed into the script through a form (either POST or GET method) This form just switches the values passed in for word_1 and word_2

12 Accessing a database with PHP 6/10/08David Lawrence JLab12 PDO = PHP Data Objects

13 PHP embedded in HTML 6/10/08David Lawrence JLab13

14 PHP embedded in HTML 6/10/08David Lawrence JLab14

15 Functions  Large scripts can be made modular using functions Unlike many languages, PHP functions don’t exist until the definition is encountered at runtime! 7/8/09Intro. to PHP -- David Lawrence15

16 Arrays  Arrays can be created with the array() function The special $var[ ] syntax appends one element to an existing array. foreach can be used to loop over elements of an array. Specific elements can be accessed with $var[0], $var[1], … 7/8/09Intro. to PHP -- David Lawrence16

17 Sessions  Web pages are ephemeral (in a sense) 7/8/09Intro. to PHP -- David Lawrence17 The PHP code is run and the results sent to the browser The PHP “program” ends and must be run again when the page is again accessed  A “session” provides a way to store variables across pages and accesses to maintain the state of the program a ghost?

18 Sessions  Start a session with session_start() (this must be done BEFORE anything is printed!)  Store and retrieve variables using the $_SESSION[ ] array 7/8/09Intro. to PHP -- David Lawrence18

19 Objects  PHP has objects! But… Members are accessed using the “$this” variable Parent constructors and destructors are not automatically called 7/8/09Intro. to PHP -- David Lawrence19

20 Constructors and Destructors  The constructor method is always called __construct() regardless of the class  The destructor method is always called __destruct() regardless of the class  Parent class ctor/dtor must be explicitly called: parent::__construct() parent::__destruct() 7/8/09Intro. to PHP -- David Lawrence20

21 Inheritance  A class may inherit another class’ methods and data members by extending it  A class may be abstract meaning it only defines the names and formats of methods (not the contents) 7/8/09Intro. to PHP -- David Lawrence21

22 An alternative way … 7/8/09Intro. to PHP -- David Lawrence22  The get_class() routine can be used to get the name of the class of an object  Also, self:: and parent:: can be used to explicitly call static methods of the current object’s class or its parent’s

23 Command Line interface  PHP can be used from the command line without a web server by simply passing the name of the script to the php program 7/8/09Intro. to PHP -- David Lawrence23 Great for debugging!

24 Summary  PHP is a scripting language primarily geared towards web pages (but can be used from the command line as well )  All major web servers (Apache, MS IIS, …) support PHP and PHP supports all major Databases (MySQL, Oracle, …)  PHP allows both procedural and object oriented programming models  See extensive documentation with examples at: http://www.php.net 7/8/09Intro. to PHP -- David Lawrence24


Download ppt "David Lawrence 7/8/091Intro. to PHP -- David Lawrence."

Similar presentations


Ads by Google