developing web applications with php rad for the world wide web

33
Developing Web Developing Web Applications with PHP Applications with PHP RAD for the World Wide Web

Upload: branden-quinn

Post on 01-Jan-2016

237 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Developing Web Applications with PHP RAD for the World Wide Web

Developing Web Developing Web Applications with PHPApplications with PHP

RAD for the World Wide Web

Page 2: Developing Web Applications with PHP RAD for the World Wide Web

AgendaAgenda

– Introduction– PHP Language Basics– Built-in Functions– Tricks and Tips– PHP 5

Page 3: Developing Web Applications with PHP RAD for the World Wide Web

IntroductionIntroduction

• What is PHP?– PHP stands for "PHP Hypertext

Preprocessor”– An embedded scripting language for HTML

like ASP or JSP– A language that combines elements of

Perl, C, and Java

Page 4: Developing Web Applications with PHP RAD for the World Wide Web

IntroductionIntroduction

• History of PHP– Created by Rasmus Lerdorf in 1995 for

tracking access to his resume– Originally a set of Perl scripts known as the

“Personal Home Page” tools– Rewritten in C with database functionality– Added a forms interpreter and released as

PHP/FI: includes Perl-like variables, and HTML embedded syntax

Page 5: Developing Web Applications with PHP RAD for the World Wide Web

IntroductionIntroduction

• History of PHP (cont.)– Version 5.0 will include version 2.0 of the

Zend Engine• New object model is more powerful and

intuitive• Objects will no longer be passed by value; they

now will be passed by reference• Increases performance and makes OOP more

attractive

Page 6: Developing Web Applications with PHP RAD for the World Wide Web

IntroductionIntroduction

• Netcraft Statistics– 11,869,645 Domains, 1,316,288 IP Addresses

Page 7: Developing Web Applications with PHP RAD for the World Wide Web

IntroductionIntroduction

• Performance*– Zdnet Statistics

• PHP pumped out about 47 pages/second • Microsoft ASP pumped out about 43 pages/second • Allaire ColdFusion pumped out about 29

pages/second • Sun Java JSP pumped out about 13 pages/second

* From PHP HOWTO, July 2001

Page 8: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• The Script Tags– All PHP code is contained in one of several

script tags:• <?

// Some code?>

• <?php// Some code here?>

Page 9: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• The Script Tags (cont.)• <script language=“PHP">

// Some code here</script>

– ASP-style tags• Introduced in 3.0; may be removed in the future • <%

// Some code here%>

Page 10: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• The Script Tags (cont.)– “Echo” Tags

– <table><tr> <td>Name:</td><td><?= $name ?></td></tr><tr> <td>Address:</td><td><?= $address ?></td></tr></table>

Page 11: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Hello World!: An Example– Like Perl, there is more than one way to do

it• <?php echo “Hello World!”; ?> • <?php

$greeting = “Hello World!” printf(“%s”, $greeting);php?>

Page 12: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Hello World!: An Example (cont.)• <script language=“PHP”>

$hello = “Hello”; $world = “World!”; print $hello . $world</script>

Page 13: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Constants, Data Types and Variables– Constants define a string or numeric value– Constants do not begin with a dollar sign– Examples:

• define(“COMPANY”, “Acme Enterprises”);

• define(“YELLOW”, “#FFFF00”);

• define(“PI”, 3.14);

• define(“NL”, “<br>\n”);

Page 14: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Constants, Data Types and Variables– Using a constant

• print(“Company name: “ . COMPANY . NL);

Page 15: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Constants, Data Types and Variables– Data types

• Integers, doubles and strings– isValid = true; // Boolean– 25 // Integer– 3.14 // Double– ‘Four’ // String– “Total value” // Another string

Page 16: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Constants, Data Types and Variables– Data types

• Strings and type conversion– $street = 123;– $street = $street . “ Main Street”;– $city = ‘Naperville’;

$state = ‘IL’;– $address = $street;– $address = $address . NL . “$city, $state”;– $number = $address + 1; // $number equals 124

Page 17: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Constants, Data Types and Variables– Data types

• Arrays– Perl-like syntax

• $arr = array("foo" => "bar", 12 => true); – same as

• $arr[“foo”] = “bar”;• $arr[12] = true;

Page 18: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Constants, Data Types and Variables

• Arrays (cont.)– <?php

$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));

echo $arr["somearray"][6]; // 5 echo $arr["somearray"][13]; // 9 echo $arr["somearray"]["a"]; // 42?>

Page 19: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Constants, Data Types and Variables– Objects

– Currently not much more advanced than than associative arrays Using constants

– Before version 5.0, objects are passed by value• Slow• Functions can not easily change object variables

Page 20: Developing Web Applications with PHP RAD for the World Wide Web

PHP Language BasicsPHP Language Basics

• Constants, Data Types and Variables– Operators

– Contains all of the operators like in C and Perl (even the ternary)

– Statements– if, if/elseif– Switch/case– for, while, and do/while loops– Include and require statements for code reuse

Page 21: Developing Web Applications with PHP RAD for the World Wide Web

Built-in FunctionsBuilt-in Functions

• What comes In the box?– Array Manipulator Functions

• sort, merge, push, pop, slice, splice, keys, count

– CCVS: Interface to Red Hat’s credit system– COM functions: Interface to Windows COM

objects– Date and Time Functions

• getdate, mkdate, date, gettimeofday, localtime, strtotime, time

Page 22: Developing Web Applications with PHP RAD for the World Wide Web

Built-in FunctionsBuilt-in Functions

• What comes In the box?– Directory Functions

• Platform independent

– Error Handling Functions• Recover from warnings and errors

– Filesystem Functions• Access flat files• Check directory, link, and file status information• Copy, delete, and rename files

Page 23: Developing Web Applications with PHP RAD for the World Wide Web

Built-in FunctionsBuilt-in Functions

• What comes In the box?– IMAP Functions

• Manipulate mail boxes via the IMAP protocol

– LDAP Functions• Works with most LDAP servers

– Mail Functions• mail($recipient, $subject, $message)

Page 24: Developing Web Applications with PHP RAD for the World Wide Web

Built-in FunctionsBuilt-in Functions

• What comes In the box?– Database Functions

• dba: dbm-style abstraction layer• dBase• Frontbase• Informix• Ingres II• Interbase• mSQL

Page 25: Developing Web Applications with PHP RAD for the World Wide Web

Built-in FunctionsBuilt-in Functions

• What comes In the box?– Database Functions (cont.)

• MySQL• Oracle• PostgreSQL• SQL Server

– MING• Macromedia Flash

– PDF• Create/manipulate PDF files dynamically

Page 26: Developing Web Applications with PHP RAD for the World Wide Web

Built-in FunctionsBuilt-in Functions

• What comes In the box?– POSIX Functions

• Manipulate process information

– Regular Expression Functions• Uses POSIX regex

– Semaphore and Socket Functions• Available only on Unix

– Session Management Functions

Page 27: Developing Web Applications with PHP RAD for the World Wide Web

PHP on Linux and WindowsPHP on Linux and Windows

• Code Portability– The obvious: don’t use Unix or Windows

specific functions– Create a reusable module for file system

differences, for example:– if( PHP_OS == "Linux" )

{ $ConfigPath = "/var/www/conf"; $DataPath = "/var/www/data";}

Page 28: Developing Web Applications with PHP RAD for the World Wide Web

PHP on Linux and WindowsPHP on Linux and Windows

• Code Portability– if( ereg("WIN", PHP_OS) )

{ $ApachePath= “C:/Program Files/Apache Group/Apache”; $ConfigPath = ”$ApachePath/htdocs/conf"; $DataPath = "$ApachePath/htdocs/data";}

$ConfigFile = "$ConfigPath/paperwork.conf";$CountryList = "$DataPath/countries.txt";$StateAbbrList = "$DataPath/usstateabbrs.txt";$StateNameList= "$DataPath/usstatenames.txt";

Page 29: Developing Web Applications with PHP RAD for the World Wide Web

Tricks and TipsTricks and Tips

• Coding– Prototype your web pages first

• Separate the design of the site from the coding

– Turn repetitive code into functions• Makes for more maintainable and reusable

code

– Turn grunt code into functions• Database access, configuration file access

Page 30: Developing Web Applications with PHP RAD for the World Wide Web

Tricks and TipsTricks and Tips

• Debugging– Feature: PHP is not a strongly typed

language• Variables can be created anywhere in your

code

– Undocumented Feature: PHP is not a strongly typed language

• Typos in variable names will cause stuff to happen

Page 31: Developing Web Applications with PHP RAD for the World Wide Web

Tricks and TipsTricks and Tips

• Debugging– Use scripts to dump form and session

variables• Write scripts to dump data to discover bad or

missing data

Page 32: Developing Web Applications with PHP RAD for the World Wide Web

Tricks and TipsTricks and Tips

• Development Tools– Color coding editors

• vim, Emacs, Visual SlickEdit

– IDEs• Windows

– Macromedia Dreamweaver– Allaire Homesite

– Zend’s PHPEdit

• Linux– ???

Page 33: Developing Web Applications with PHP RAD for the World Wide Web

PHP 5PHP 5

• Features– Complete objects

• Objects with constructors• Abstract classes• Private, protected and abstract functions• Private, protected and constant variables• Namespaces• Exception handling with try/catch blocks