php presented by christine fang and ian stuart ● cs616 spring 2003

31
PHP Presented by Christine Fang and Ian Stuart CS616 Spring 2003

Post on 19-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

PHP

Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

Page 2: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• PHP - the “PHP Hypertext Preprocessor”• It’s an open-source programming language for

building dynamic, interactive web sites.• Devised by Rasmus Lerdorf in 1994.• PHP is a project of the Apache Foundation.• In technical terms, PHP is a cross-platform,

HTML-embedded, server-side web scripting language.

What is PHP?

Page 3: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• The code is written in files containing a mixture of PHP instructions and HTML code.

• PHP between <?php tag and ending tag?>

• Programs run on a web servers and return HTML code readable by any browser.

• No client-side plug-ins are required.

How does it work?

Page 4: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

Web Server/Browser Interact with PHP

Page 5: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• PHP can be used to write the web sites which anyone familiar with the Web uses every day: e-commerce sites, search engines, etc.

• Allows custom pages for clients

• Supports back-end database connectivity

What can we do with PHP?

Page 6: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

Server software• A PHP compatible

web server• PHP4• A relational database

system

Client software• A web browser• A text editor, such as

Notepad, Emacs, vi, BBEdit, and so on.

What software do we need?

www.firepages.com.au

Page 7: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

Like HTML, PHP variables are not strongly typed:they don’t need to be cast by the programmer.

• String (text)• integer (numeric)• double (numeric)• array• object

Data Types

Page 8: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

<html><head></head>

<body>

<?php

$myClass = “CS616”;

echo “Welcome to $myClass”;

?>

</body>

</html>

A Very Simple Example

Page 9: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• GET method and POST method

• Most common HTML form controls: text boxes, checkboxes, radio buttons, listboxes, hidden fields, passwords and buttons.

• The ACTION attribute is used to specify which page we go to once the form is submitted. The .php file suffix indicates that the page is sent to the PHP script engine.

Handling HTML Forms

Page 10: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• Numerically-Indexed Arrays

• Associative (String-Indexed) Arrays $treeMember["YearOfBirth"] = 1913;

• Non-Sequential Arrays $employeeNames[268] = "Fang"; $employeeNames[145] = "Stuart";

• Multidimensional Arrays

PHP: Arrays

Page 11: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• Conditional/Branching Statements– if / else / else if– switch / case

• Loops and iterations– while and do/while– for and foreach loops– list and each functions

PHP Control Structures

Page 12: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

foreach ($myArray As $myArrayElement) {

…iterate through each $myArrayElement

}

foreach ($myArray As $myArrayIndex => $myArrayElement) {

…iterate through each $myArrayIndex that

corresponds to each $myArrayElement

}

PHP: foreach Loops

Page 13: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• Useful for iterating through associative and nonsequential arrays:

while (list($index, $elementData) = each($theArray)) {

…iterate through the array, with access

to the indices and elements

}

PHP: list and each Functions

Page 14: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• PHP “modules” are functions– Pass variables by value or by reference– No function overloading, but default values for

parameters can be defined

• “Include” files– Simple import of code from other files– Works within conditional constructs

• Limited support for objects

Modularization and OO Support

Page 15: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• PHP was not originally intended to be OO

• Now supports classes– Class and instance variables– Inheritance (subclasses)– Limited polymorphism (no overriding)

• Objects can be serialized/deserialized (example: session management)

PHP Objects

Page 16: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• Allow pattern recognition in data

• Similar notation to Perl or Python

• Convenient for string manipulation:– Escaping special characters (i.e. backslashes)– Parsing and matching text– Data verification (URLs, e-mail, passwords)

Regular Expressions in PHP

Page 17: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

$validEmail = eregi (

"^[a-z0-9]+([\.][a-z0-9]+)*@" .

"[a-z0-9]+([\.][a-z0-9]+)*" .

"\.[a-z]{2,}", $theEmailAddress);

Regular Expressions in PHP

Huh…?

Page 18: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

$validEmail = eregi (

"^[a-z0-9]+([\.][a-z0-9]+)*@" .

"[a-z0-9]+([\.][a-z0-9]+)*" .

"\.[a-z]{2,}", $theEmailAddress);

Regular Expressions in PHP

[email protected]

Page 19: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

$validEmail = eregi (

"^[a-z0-9]+([\.][a-z0-9]+)*@" .

"[a-z0-9]+([\.][a-z0-9]+)*" .

"\.[a-z]{2,}", $theEmailAddress);

Regular Expressions in PHP

[email protected]

The string $theEmailAddress must begin with a block of textContaining alphanumeric characters (A to Z, a to z, 0-9)…

Page 20: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

$validEmail = eregi (

"^[a-z0-9]+([\.][a-z0-9]+)*@" .

"[a-z0-9]+([\.][a-z0-9]+)*" .

"\.[a-z]{2,}", $theEmailAddress);

Regular Expressions in PHP

[email protected]

…followed by zero or more blocks of text that must start with a periodand then at least one alphanumeric character…

Page 21: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

$validEmail = eregi (

"^[a-z0-9]+([\.][a-z0-9]+)*@" .

"[a-z0-9]+([\.][a-z0-9]+)*" .

"\.[a-z]{2,}", $theEmailAddress);

Regular Expressions in PHP

[email protected]

…followed by the @ symbol…

Page 22: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

$validEmail = eregi (

"^[a-z0-9]+([\.][a-z0-9]+)*@" .

"[a-z0-9]+([\.][a-z0-9]+)*" .

"\.[a-z]{2,}", $theEmailAddress);

Regular Expressions in PHP

[email protected]

…followed by one or more blocks of textusing the same patterns used before (the server/domain names)…

Page 23: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

$validEmail = eregi (

"^[a-z0-9]+([\.][a-z0-9]+)*@" .

"[a-z0-9]+([\.][a-z0-9]+)*" .

"\.[a-z]{2,}", $theEmailAddress);

Regular Expressions in PHP

[email protected]

…followed by a block of text that must begin with a periodand then consist of at least two letters (the domain suffix).

Page 24: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

$validEmail = eregi (

"^[a-z0-9]+([\.][a-z0-9]+)*@" .

"[a-z0-9]+([\.][a-z0-9]+)*" .

"\.[a-z]{2,}", $theEmailAddress);

Regular Expressions in PHP

[email protected]

If the pattern matches, $validEmail equals true; otherwise it’s false.(In reality, we’d also check for characters like underscores and hyphens.)

Page 25: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• PHP provides connectivity for several database systems and sources, including (in part):– Unified ODBC

– Microsoft SQL Server

– MySQL

– Oracle

– Sybase

– Informix

PHP and Databases

Page 26: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• PHP doesn’t require client-side capabilities besides an HTML-enabled browser.

• PHP requires less communication overhead than Java and JSP.

• PHP lacks some features like custom tags and JavaBeans.

• Better OO in Java and JSP

PHP vs. Java and JSP

Page 27: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• Consider Java/JSP for larger projects requiring reusable modules and large libraries.

• Consider PHP when processing performance is a primary concern, or when rapid application development is needed.

PHP vs. Java and JSP

Page 28: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• PHP uses a C-like syntax; ASP is driven by VBScript.

• ASP is generally slower due to overhead; PHP uses its own memory space.

• PHP is open source (read: free) while ASP is a proprietary commercial product.

PHP vs. ASP

Page 29: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

Revised Alta Vista results, March 6, 2002. Source: http://php.weblogs.com/popularity

Market Share

Page 30: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

October 12, 2000 March 4, 2002 Growth Share

php 84,296 4,409,034 5130%

php3 49,906 2,308,381 4525%

phtml 23,268 831,815 3475%

TotalPHP

157,470 7,549,230 4694% 30%

asp 3,166,710 11,958,185 278% 48%

jsp 24,435 413,827 1594% 2%

cfm 936,223 4,950,133 429% 20%

Market Share

Revised Alta Vista results, March 6, 2002. Source: http://php.weblogs.com/popularity

Page 31: PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003

• The official PHP site: www.php.net

• PHP Builder: http://www.phpbuilder.com

• PHP Developer’s Network: http://www.devnetwork.net

• PHPFreaks.com: http://www.phpfreaks.com

• Codewalkers.com: http://codewalkers.com

PHP Resources