PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003.

Post on 19-Dec-2015

217 views 0 download

Tags:

Transcript of PHP Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003.

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?

• 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?

Web Server/Browser Interact with PHP

• 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?

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

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

<html><head></head>

<body>

<?php

$myClass = “CS616”;

echo “Welcome to $myClass”;

?>

</body>

</html>

A Very Simple Example

• 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

• Numerically-Indexed Arrays

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

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

• Multidimensional Arrays

PHP: Arrays

• 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

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

• 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

• 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

• 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

• 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

$validEmail = eregi (

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

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

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

Regular Expressions in PHP

Huh…?

$validEmail = eregi (

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

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

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

Regular Expressions in PHP

kathleen.chou@thewebgoddess.org

$validEmail = eregi (

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

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

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

Regular Expressions in PHP

kathleen.chou@thewebgoddess.org

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

$validEmail = eregi (

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

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

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

Regular Expressions in PHP

kathleen.chou@thewebgoddess.org

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

$validEmail = eregi (

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

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

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

Regular Expressions in PHP

kathleen.chou@thewebgoddess.org

…followed by the @ symbol…

$validEmail = eregi (

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

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

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

Regular Expressions in PHP

kathleen.chou@thewebgoddess.org

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

$validEmail = eregi (

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

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

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

Regular Expressions in PHP

kathleen.chou@thewebgoddess.org

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

$validEmail = eregi (

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

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

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

Regular Expressions in PHP

kathleen.chou@thewebgoddess.org

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

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

– Microsoft SQL Server

– MySQL

– Oracle

– Sybase

– Informix

PHP and Databases

• 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

• 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

• 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

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

Market Share

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

• 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