david lawrence 7/8/091intro. to php -- david lawrence

24
AN INTRODUCTION TO David Lawrence 7/8/09 1 Intro. to PHP -- David Lawrence

Upload: aubrey-gibbs

Post on 17-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

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

Intro. to PHP -- David Lawrence 1

AN INTRODUCTION TO

David Lawrence

7/8/09

Page 2: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 2

What is PHP? PHP = PHP: Hypertext Preprocessor

(a recursive acronym)

7/8/09

“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

Page 3: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

3

Viewing sites on the World Wide Web requires both a Server and a Client

6/10/08 David Lawrence JLab

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.

Page 4: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 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/09

Page 5: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 5

PHP is a good choice because…

It currently ranks 4th 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/09

Page 6: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 6

Getting startedNote: 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/09

index.php

php goes in html comes out

Files can be edited with any text editor (some are better than others)

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

Intro. to PHP -- David Lawrence 7

Hello World!

7/8/09

The PHP code is embedded between the <?php and ?> tags

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

Intro. to PHP -- David Lawrence 8

Embedding PHP in HTML

7/8/09

One easily flow in and out of PHP mode with the <?php and ?> tags

The parser will replace everything inside the tags with the output of the code they contain

<HTML><TITLE>Hello World</TITLE>

The message of the day is:

Hello World!</HTML>

PHP parser

PHP file

What the browser sees

(Browser never actually sees the PHP code!)

Page 9: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 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/09

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)

Page 10: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 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/09

Page 11: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 11

HTML Forms

7/8/09

The <?= and ?> 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

Page 12: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

David Lawrence JLab 12

Accessing a database with PHP

6/10/08

PDO = PHP Data Objects

Page 13: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

David Lawrence JLab 13

PHP embedded in HTML

6/10/08

<HTML>+

Page 14: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

David Lawrence JLab 14

PHP embedded in HTML

6/10/08

Page 15: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 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/09

Page 16: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 16

Arrays

Arrays can be created with the array() functionThe 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/09

Page 17: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 17

Sessions

Web pages are ephemeral(in a sense)

7/8/09

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?

Page 18: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 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/09

Page 19: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 19

Objects

PHP has objects! But…Members are

accessed using the “$this” variable

Parent constructors and destructors are not automatically called

7/8/09

Page 20: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 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/09

Page 21: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 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/09

Page 22: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 22

An alternative way …

7/8/09

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

Page 23: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 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/09

Great for debugging!

Page 24: David Lawrence 7/8/091Intro. to PHP -- David Lawrence

Intro. to PHP -- David Lawrence 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/09