310 likes | 484 Views
PHP. Presented by Christine Fang and Ian Stuart ● CS616 Spring 2003. What is PHP?. PHP - the “ P HP H ypertext P reprocessor” 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.
E N D
PHP Presented by Christine Fang and Ian Stuart ●CS616 Spring 2003
What is PHP? • 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.
How does it work? • 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.
What can we do 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
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
Data Types 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
A Very Simple Example <html><head></head> <body> <?php $myClass = “CS616”; echo “Welcome to $myClass”; ?> </body> </html>
Handling HTML Forms • 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.
PHP: Arrays • Numerically-Indexed Arrays • Associative (String-Indexed) Arrays $treeMember["YearOfBirth"] = 1913; • Non-Sequential Arrays $employeeNames[268] = "Fang"; $employeeNames[145] = "Stuart"; • Multidimensional Arrays
PHP Control Structures • 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: foreach Loops foreach ($myArray As $myArrayElement) { …iterate through each $myArrayElement } foreach ($myArray As $myArrayIndex => $myArrayElement) { …iterate through each $myArrayIndex that corresponds to each $myArrayElement }
PHP: list and each Functions • 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 }
Modularization and OO Support • 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
PHP Objects • 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)
Regular Expressions in PHP • 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); Huh…?
Regular Expressions in PHP $validEmail = eregi ( "^[a-z0-9]+([\.][a-z0-9]+)*@" . "[a-z0-9]+([\.][a-z0-9]+)*" . "\.[a-z]{2,}", $theEmailAddress); kathleen.chou@thewebgoddess.org
Regular Expressions in PHP $validEmail = eregi ( "^[a-z0-9]+([\.][a-z0-9]+)*@" . "[a-z0-9]+([\.][a-z0-9]+)*" . "\.[a-z]{2,}", $theEmailAddress); kathleen.chou@thewebgoddess.org The string $theEmailAddress must begin with a block of text Containing alphanumeric characters (A to Z, a to z, 0-9)…
Regular Expressions in PHP $validEmail = eregi ( "^[a-z0-9]+([\.][a-z0-9]+)*@" . "[a-z0-9]+([\.][a-z0-9]+)*" . "\.[a-z]{2,}", $theEmailAddress); kathleen.chou@thewebgoddess.org …followed by zero or more blocks of text that must start with a period and then at least one alphanumeric character…
Regular Expressions in PHP $validEmail = eregi ( "^[a-z0-9]+([\.][a-z0-9]+)*@" . "[a-z0-9]+([\.][a-z0-9]+)*" . "\.[a-z]{2,}", $theEmailAddress); kathleen.chou@thewebgoddess.org …followed by the @ symbol…
Regular Expressions in PHP $validEmail = eregi ( "^[a-z0-9]+([\.][a-z0-9]+)*@" . "[a-z0-9]+([\.][a-z0-9]+)*" . "\.[a-z]{2,}", $theEmailAddress); kathleen.chou@thewebgoddess.org …followed by one or more blocks of text using the same patterns used before (the server/domain names)…
Regular Expressions in PHP $validEmail = eregi ( "^[a-z0-9]+([\.][a-z0-9]+)*@" . "[a-z0-9]+([\.][a-z0-9]+)*" . "\.[a-z]{2,}", $theEmailAddress); kathleen.chou@thewebgoddess.org …followed by a block of text that must begin with a period and then consist of at least two letters (the domain suffix).
Regular Expressions in PHP $validEmail = eregi ( "^[a-z0-9]+([\.][a-z0-9]+)*@" . "[a-z0-9]+([\.][a-z0-9]+)*" . "\.[a-z]{2,}", $theEmailAddress); 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 and Databases • PHP provides connectivity for several database systems and sources, including (in part): • Unified ODBC • Microsoft SQL Server • MySQL • Oracle • Sybase • Informix
PHP vs. Java and JSP • 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. ASP • 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.
Market Share Revised Alta Vista results, March 6, 2002. Source: http://php.weblogs.com/popularity
Market Share Revised Alta Vista results, March 6, 2002. Source: http://php.weblogs.com/popularity
PHP Resources • 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