Beginning PHP and Oracle
From Novice to Professional
■ ■ ■
W. Jason Gilmore and Bob Bryla
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-770-5 ISBN-10 (pbk): 1-59059-770-2
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
Lead Editor: Jonathan Gennick Technical Reviewer: Matt Wade
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jason Gilmore, Jonathan Hassell, Chris Mills, Matthew Moodie, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Kylie Johnston
Copy Editors: Jennifer Whipple, Kim Wimpsett Assistant Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist
Compositor: Susan Glinert Stevens Proofreader: April Eddy
Indexer: John Collin Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://
www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com in the Source Code/Download section.
—W. Jason Gilmore
To CRB and ESB, even with my long hours we had a great summer of fun!
—Bob Bryla
v
About the Authors . . . xxvii
About the Technical Reviewer . . . xxix
Acknowledgments . . . xxxi
Introduction . . . .xxxiii
■ CHAPTER 1 Introducing PHP
. . . 1■ CHAPTER 2 Configuring Your Environment
. . . 9■ CHAPTER 3 PHP Basics
. . . 39■ CHAPTER 4 Functions
. . . 81■ CHAPTER 5 Arrays
. . . 91■ CHAPTER 6 Object-Oriented PHP
. . . 117■ CHAPTER 7 Advanced OOP Features
. . . 139■ CHAPTER 8 Error and Exception Handling
. . . 151■ CHAPTER 9 Strings and Regular Expressions
. . . 163■ CHAPTER 10 Working with the File and Operating System
. . . 195■ CHAPTER 11 PEAR
. . . 219■ CHAPTER 12 Date and Time
. . . 229■ CHAPTER 13 Forms
. . . 249■ CHAPTER 14 Authentication
. . . 261■ CHAPTER 15 Handling File Uploads
. . . 277■ CHAPTER 16 Networking
. . . 287■ CHAPTER 17 PHP and LDAP
. . . 305■ CHAPTER 18 Session Handlers
. . . 319■ CHAPTER 19 Templating with Smarty
. . . 339■ CHAPTER 20 Web Services
. . . 361■ CHAPTER 21 Secure PHP Programming
. . . 387■ CHAPTER 22 SQLite
. . . 407■ CHAPTER 23 Introducing PDO
. . . 425vi
■
■ CHAPTER 26 Introducing Oracle
. . . 463■ CHAPTER 27 Installing and Configuring Oracle Database XE
. . . 469■ CHAPTER 28 Oracle Database XE Administration
. . . 481■ CHAPTER 29 Interacting with Oracle Database XE
. . . 501■ CHAPTER 30 From Databases to Datatypes
. . . 513■ CHAPTER 31 Securing Oracle Database XE
. . . 535■ CHAPTER 32 PHP’s Oracle Functionality
. . . 565■ CHAPTER 33 Transactions
. . . 591■ CHAPTER 34 Using HTML_Table with Advanced Queries
. . . 601■ CHAPTER 35 Using Views
. . . 621■ CHAPTER 36 Oracle PL/SQL Subprograms
. . . 633■ CHAPTER 37 Oracle Triggers
. . . 649■ CHAPTER 38 Indexes and Optimizing Techniques
. . . 661■ CHAPTER 39 Importing and Exporting Data
. . . 675■ CHAPTER 40 Backup and Recovery
. . . 687■ INDEX
. . . 697vii
About the Authors . . . xxvii
About the Technical Reviewer . . . xxix
Acknowledgments . . . xxxi
Introduction . . . .xxxiii
■ CHAPTER 1 Introducing PHP
. . . 1History
. . . 1PHP 4
. . . 2PHP 5
. . . 3PHP 6
. . . 4General Language Features
. . . 5Practicality
. . . 5Power
. . . 5Possibility
. . . 6Price
. . . 6Summary
. . . 7■ CHAPTER 2 Configuring Your Environment
. . . 9Installation Prerequisites
. . . 9Downloading Apache
. . . 10Downloading PHP
. . . 11Obtaining the Documentation
. . . 11Installing Apache and PHP on Linux
. . . 12Installing Apache and PHP on Windows
. . . 13Installing IIS and PHP on Windows
. . . 15Installing IIS and PHP
. . . 15Configuring FastCGI to Manage PHP Processes
. . . 16Testing Your Installation
. . . 17Configuring PHP
. . . 19Configuring PHP at Build Time on Linux
. . . 19Customizing the Windows Build
. . . 19Run-Time Configuration
. . . 20Managing PHP’s Configuration Directives
. . . 20PHP’s Configuration Directives
. . . 22Choosing a Code Editor
. . . 35Adobe Dreamweaver CS3
. . . 35Notepad++
. . . 35PDT (PHP Development Tools)
. . . 35Zend Studio
. . . 36Choosing a Web Hosting Provider
. . . 36Seven Questions for Any Prospective Hosting Provider
. . . 37Summary
. . . 38■ CHAPTER 3 PHP Basics
. . . 39Embedding PHP Code in Your Web Pages
. . . 39Default Syntax
. . . 40Short-Tags
. . . 40Script
. . . 41ASP Style
. . . 41Embedding Multiple Code Blocks
. . . 41Commenting Your Code
. . . 42Single-Line C++ Syntax
. . . 42Shell Syntax
. . . 42Multiple-Line C Syntax
. . . 42Outputting Data to the Browser
. . . 43The print() Statement
. . . 43The printf() Statement
. . . 44The sprintf() Statement
. . . 46PHP’s Supported Datatypes
. . . 46Scalar Datatypes
. . . 46Compound Datatypes
. . . 47Converting Between Datatypes Using Type Casting
. . . 48Adapting Datatypes with Type Juggling
. . . 50Type-Related Functions
. . . 50Type Identifier Functions
. . . 51Identifiers
. . . 51Variables
. . . 52Variable Declaration
. . . 52Variable Scope
. . . 54PHP’s Superglobal Variables
. . . 56Variable Variables
. . . 60Constants
. . . 61Expressions
. . . 61Operands
. . . 62Operators
. . . 62String Interpolation
. . . 68Double Quotes
. . . 68Single Quotes
. . . 69Heredoc
. . . 69Control Structures
. . . 70Conditional Statements
. . . 70Looping Statements
. . . 72File Inclusion Statements
. . . 77Summary
. . . 80■ CHAPTER 4 Functions
. . . 81Invoking a Function
. . . 81Creating a Function
. . . 82Passing Arguments by Value
. . . 82Passing Arguments by Reference
. . . 83Default Argument Values
. . . 84Returning Values from a Function
. . . 85Recursive Functions
. . . 86Function Libraries
. . . 89Summary
. . . 90■ CHAPTER 5 Arrays
. . . 91What Is an Array?
. . . 91Creating an Array
. . . 92Creating Arrays with array()
. . . 93Extracting Arrays with list()
. . . 94Populating Arrays with a Predefined Value Range
. . . 94Testing for an Array
. . . 95Adding and Removing Array Elements
. . . 96Adding a Value to the Front of an Array
. . . 96Adding a Value onto the End of an Array
. . . 96Removing a Value from the Front of an Array
. . . 97Removing a Value from the End of an Array
. . . 97Locating Array Elements
. . . 97Searching an Array
. . . 97Retrieving Array Keys
. . . 98Retrieving Array Values
. . . 99Traversing Arrays
. . . 99Retrieving the Current Array Key
. . . 99Retrieving the Current Array Value
. . . 100Retrieving the Current Array Key and Value
. . . 100Moving the Array Pointer
. . . 100Passing Array Values to a Function
. . . 101Determining Array Size and Uniqueness
. . . 102Determining the Size of an Array
. . . 102Counting Array Value Frequency
. . . 103Determining Unique Array Values
. . . 103Sorting Arrays
. . . 104Reversing Array Element Order
. . . 104Flipping Array Keys and Values
. . . 104Sorting an Array
. . . 105Merging, Slicing, Splicing, and Dissecting Arrays
. . . 109Merging Arrays
. . . 109Recursively Appending Arrays
. . . 110Combining Two Arrays
. . . 110Slicing an Array
. . . 111Splicing an Array
. . . 112Calculating an Array Intersection
. . . 112Calculating Associative Array Intersections
. . . 113Calculating Array Differences
. . . 113Calculating Associative Array Differences
. . . 114Other Useful Array Functions
. . . 114Returning a Random Set of Keys
. . . 114Shuffling Array Elements
. . . 115Summary
. . . 116■ CHAPTER 6 Object-Oriented PHP
. . . 117The Benefits of OOP
. . . 117Encapsulation
. . . 118Inheritance
. . . 118Polymorphism
. . . 118Key OOP Concepts
. . . 119Classes
. . . 119Objects
. . . 120Fields
. . . 120Properties
. . . 123Constants
. . . 125Methods
. . . 126Constructors and Destructors
. . . 129Constructors
. . . 130Destructors
. . . 132Static Class Members
. . . 133The instanceof Keyword
. . . 134Helper Functions
. . . 135Autoloading Objects
. . . 136Summary
. . . 137■ CHAPTER 7 Advanced OOP Features
. . . 139Advanced OOP Features Not Supported by PHP
. . . 139Object Cloning
. . . 140Cloning Example
. . . 140The __clone() Method
. . . 141Inheritance
. . . 142Class Inheritance
. . . 143Inheritance and Constructors
. . . 145Interfaces
. . . 146Implementing a Single Interface
. . . 147Implementing Multiple Interfaces
. . . 148Abstract Classes
. . . 149Summary
. . . 150■ CHAPTER 8 Error and Exception Handling
. . . 151Configuration Directives
. . . 151Error Logging
. . . 154Exception Handling
. . . 156Why Exception Handling Is Handy
. . . 157PHP’s Exception-Handling Implementation
. . . 158Summary
. . . 162■ CHAPTER 9 Strings and Regular Expressions
. . . 163Regular Expressions
. . . 163Regular Expression Syntax (POSIX)
. . . 164PHP’s Regular Expression Functions (POSIX Extended)
. . . 166Regular Expression Syntax (Perl)
. . . 169Other String-Specific Functions
. . . 175Determining the Length of a String
. . . 175Comparing Two Strings
. . . 176Manipulating String Case
. . . 177Converting Strings to and from HTML
. . . 179Alternatives for Regular Expression Functions
. . . 183Padding and Stripping a String
. . . 190Counting Characters and Words
. . . 191Taking Advantage of PEAR: Validate_US
. . . 193Installing Validate_US
. . . 193Using Validate_US
. . . 194Summary
. . . 194■ CHAPTER 10 Working with the File and Operating System
. . . 195Learning About Files and Directories
. . . 195Parsing Directory Paths
. . . 196Calculating File, Directory, and Disk Sizes
. . . 197Determining Access and Modification Times
. . . 200Working with Files
. . . 201The Concept of a Resource
. . . 201Recognizing Newline Characters
. . . 202Recognizing the End-of-File Character
. . . 202Opening and Closing a File
. . . 202Reading from a File
. . . 204Writing a String to a File
. . . 209Moving the File Pointer
. . . 210Reading Directory Contents
. . . 210Executing Shell Commands
. . . 212System-Level Program Execution
. . . 213Sanitizing the Input
. . . 213PHP’s Program Execution Functions
. . . 214Summary
. . . 217■ CHAPTER 11 PEAR
. . . 219Popular PEAR Packages
. . . 219Preinstalled Packages
. . . 219Installer-Suggested Packages
. . . 220The Power of PEAR: Converting Numeral Formats
. . . 221Installing and Updating PEAR
. . . 222Installing PEAR
. . . 222PEAR and Hosting Companies
. . . 223Updating PEAR
. . . 223Using the PEAR Package Manager
. . . 223Viewing an Installed PEAR Package
. . . 224Learning More About an Installed PEAR Package
. . . 224Installing a PEAR Package
. . . 225Including a Package Within Your Scripts
. . . 226Upgrading Packages
. . . 227Uninstalling a Package
. . . 228Downgrading a Package
. . . 228Summary
. . . 228■ CHAPTER 12 Date and Time
. . . 229The Unix Timestamp
. . . 229PHP’s Date and Time Library
. . . 230Validating Dates
. . . 230Formatting Dates and Times
. . . 231Converting a Timestamp to User-Friendly Values
. . . 234Working with Timestamps
. . . 235Date Fu
. . . 236Displaying the Localized Date and Time
. . . 237Displaying the Web Page’s Most Recent Modification Date
. . . 240Determining the Number of Days in the Current Month
. . . 240Determining the Number of Days in Any Given Month
. . . 241Calculating the Date X Days from the Present Date
. . . 241Taking Advantage of PEAR: Creating a Calendar
. . . 242Date and Time Enhancements for PHP 5.1+ Users
. . . 245Introducing the DateTime Constructor
. . . 245Formatting Dates
. . . 245Setting the Date After Instantiation
. . . 245Setting the Time After Instantiation
. . . 246Modifying Dates and Times
. . . 246Summary
. . . 247■ CHAPTER 13 Forms
. . . 249PHP and Web Forms
. . . 249A Simple Example
. . . 250Passing Form Data to a Function
. . . 251Working with Multivalued Form Components
. . . 252Taking Advantage of PEAR: HTML_QuickForm
. . . 253Installing HTML_QuickForm
. . . 254Creating a Simple Form
. . . 254Using Auto-Completion
. . . 258Summary
. . . 259■ CHAPTER 14 Authentication
. . . 261HTTP Authentication Concepts
. . . 261PHP Authentication
. . . 262Authentication Variables
. . . 262Useful Functions
. . . 263PHP Authentication Methodologies
. . . 264Hard-Coded Authentication
. . . 264File-Based Authentication
. . . 265Database-Based Authentication
. . . 266IP-Based Authentication
. . . 268User Login Administration
. . . 270Testing Password Guessability with the CrackLib Library
. . . 270One-Time URLs and Password Recovery
. . . 272Summary
. . . 275■ CHAPTER 15 Handling File Uploads
. . . 277Uploading Files via HTTP
. . . 277Uploading Files with PHP
. . . 278PHP’s File Upload/Resource Directives
. . . 278The $_FILES Array
. . . 279PHP’s File-Upload Functions
. . . 280Upload Error Messages
. . . 281A Simple Example
. . . 282Taking Advantage of PEAR: HTTP_Upload
. . . 283Installing HTTP_Upload
. . . 283Uploading a File
. . . 283Learning More About an Uploaded File
. . . 284Uploading Multiple Files
. . . 285Summary
. . . 285■ CHAPTER 16 Networking
. . . 287DNS, Services, and Servers
. . . 287DNS
. . . 288Services
. . . 291Establishing Socket Connections
. . . 292Configuration Directives
. . . 294Sending E-mail Using a PHP Script
. . . 295Common Networking Tasks
. . . 299Pinging a Server
. . . 299Creating a Port Scanner
. . . 300Creating a Subnet Converter
. . . 301Testing User Bandwidth
. . . 302Summary
. . . 303■ CHAPTER 17 PHP and LDAP
. . . 305Using LDAP from PHP
. . . 306Connecting to an LDAP Server
. . . 306Retrieving LDAP Data
. . . 309Counting Retrieved Entries
. . . 312Sorting LDAP Records
. . . 312Inserting LDAP Data
. . . 313Updating LDAP Data
. . . 314Deleting LDAP Data
. . . 314Working with the Distinguished Name
. . . 315Error Handling
. . . 316Summary
. . . 317■ CHAPTER 18 Session Handlers
. . . 319What Is Session Handling?
. . . 319Configuration Directives
. . . 321Managing the Session Storage Media
. . . 321Setting the Session Files Path
. . . 321Automatically Enabling Sessions
. . . 322Setting the Session Name
. . . 322Choosing Cookies or URL Rewriting
. . . 322Automating URL Rewriting
. . . 322Setting the Session Cookie Lifetime
. . . 323Setting the Session Cookie’s Valid URL Path
. . . 323Setting Caching Directions for Session-Enabled Pages
. . . 323Working with Sessions
. . . 324Starting a Session
. . . 324Destroying a Session
. . . 325Setting and Retrieving the Session ID
. . . 325Creating and Deleting Session Variables
. . . 326Encoding and Decoding Session Data
. . . 326Practical Session-Handling Examples
. . . 328Automatically Logging In Returning Users
. . . 328Generating a Recently Viewed Document Index
. . . 330Creating Custom Session Handlers
. . . 332Tying Custom Session Functions into PHP’s Logic
. . . 333Custom Oracle-Based Session Handlers
. . . 333Summary
. . . 337■ CHAPTER 19 Templating with Smarty
. . . 339What’s a Templating Engine?
. . . 339Introducing Smarty
. . . 341Installing Smarty
. . . 342Using Smarty
. . . 343Smarty’s Presentational Logic
. . . 344Comments
. . . 345Variable Modifiers
. . . 345Control Structures
. . . 348Statements
. . . 352Creating Configuration Files
. . . 354config_load
. . . 355Referencing Configuration Variables
. . . 355Using CSS in Conjunction with Smarty
. . . 356Caching
. . . 357Working with the Cache Lifetime
. . . 357Eliminating Processing Overhead with is_cached()
. . . 358Creating Multiple Caches per Template
. . . 358Some Final Words About Caching
. . . 359Summary
. . . 360■ CHAPTER 20 Web Services
. . . 361Why Web Services?
. . . 361Real Simple Syndication
. . . 363RSS Syntax
. . . 365MagpieRSS
. . . 366SimpleXML
. . . 372Loading XML
. . . 373Parsing the XML
. . . 374SOAP
. . . 377SOAP Messages
. . . 378PHP’s SOAP Extension
. . . 378Summary
. . . 385■ CHAPTER 21 Secure PHP Programming
. . . 387Configuring PHP Securely
. . . 387Safe Mode
. . . 387Other Security-Related Configuration Parameters
. . . 390Hiding Configuration Details
. . . 391Hiding Apache
. . . 392Hiding PHP
. . . 393Hiding Sensitive Data
. . . 394Hiding the Document Root
. . . 394Denying Access to Certain File Extensions
. . . 394Sanitizing User Data
. . . 395File Deletion
. . . 395Cross-Site Scripting
. . . 395Sanitizing User Input: The Solution
. . . 397Taking Advantage of PEAR: Validate
. . . 399Data Encryption
. . . 400PHP’s Encryption Functions
. . . 401The MCrypt Package
. . . 403Summary
. . . 405■ CHAPTER 22 SQLite
. . . 407Introduction to SQLite
. . . 407Installing SQLite
. . . 407Using the SQLite Command-Line Interface
. . . 408PHP’s SQLite Library
. . . 409sqlite.assoc_case = 0 | 1 | 2
. . . 409Opening a Connection
. . . 410Creating a Table in Memory
. . . 411Closing a Connection
. . . 411Querying a Database
. . . 412Parsing Result Sets
. . . 413Retrieving Result Set Details
. . . 416Manipulating the Result Set Pointer
. . . 418Retrieving a Table’s Column Types
. . . 419Working with Binary Data
. . . 420Creating and Overriding SQLite Functions
. . . 421Creating Aggregate Functions
. . . 422Summary
. . . 423■ CHAPTER 23 Introducing PDO
. . . 425Another Database Abstraction Layer?
. . . 426Using PDO
. . . 427Installing PDO
. . . 427PDO’s Database Support
. . . 428Connecting to a Database Server and Selecting a Database
. . . 428Handling Errors
. . . 430Executing Queries
. . . 431Prepared Statements
. . . 433Retrieving Data
. . . 436Setting Bound Columns
. . . 439Transactions
. . . 440Summary
. . . 440■ CHAPTER 24 Building Web Sites for the World
. . . 441Approaches to Internationalizing and Localizing Applications
. . . 441Translating Web Sites with Gettext
. . . 442Localizing Dates, Numbers, and Times
. . . 446Summary
. . . 447■ CHAPTER 25 MVC and the Zend Framework
. . . 449Introducing MVC
. . . 449PHP’s Framework Solutions
. . . 451The CakePHP Framework
. . . 452The Solar Framework
. . . 452The symfony Framework
. . . 452The Zend Framework
. . . 453Introducing the Zend Framework
. . . 453Downloading and Installing the Zend Framework
. . . 454Creating Your First Zend Framework-Driven Web Site
. . . 455Searching the Web with Zend_Service_Yahoo
. . . 460Summary
. . . 462■ CHAPTER 26 Introducing Oracle
. . . 463Oracle’s Database Family
. . . 463Express Edition (XE)
. . . 463Standard Edition One
. . . 465Standard Edition
. . . 465Enterprise Edition
. . . 465Personal Edition
. . . 466Other Products in the Oracle Family
. . . 466Developer and Client-Side Tools
. . . 466Summary
. . . 467■ CHAPTER 27 Installing and Configuring Oracle Database XE
. . . 469Ensuring Installation Prerequisites
. . . 469Windows Installation Tasks
. . . 470Windows Prerequisites
. . . 470Downloading the Installation Files
. . . 470Performing the Installation
. . . 470Configuring Oracle and PHP
. . . 474Linux Installation Tasks
. . . 474Linux Prerequisites
. . . 475Downloading the Installation Files
. . . 476Performing the Installation
. . . 477Configuring Oracle and PHP
. . . 478Performing Post-Installation Tasks
. . . 479Creating User Accounts
. . . 479Summary
. . . 480■ CHAPTER 28 Oracle Database XE Administration
. . . 481Understanding the Oracle Architecture
. . . 481Oracle Storage Structures
. . . 482Oracle Memory Structures
. . . 485Initialization Parameters
. . . 487Connecting to the Database
. . . 489Running SQL*Plus from the Command Line
. . . 489Running SQL Commands Using the XE Home Page
. . . 492Starting and Stopping Oracle Database XE
. . . 494Starting Oracle Database XE
. . . 494Stopping Oracle Database XE
. . . 495Using Oracle-Supplied Utilities
. . . 496Administration
. . . 497Object Browser
. . . 497SQL
. . . 498Utilities
. . . 498Application Builder
. . . 498Troubleshooting in Oracle
. . . 499Summary
. . . 500■ CHAPTER 29 Interacting with Oracle Database XE
. . . 501XE Home Page
. . . 501Installing the Oracle Database XE Client
. . . 501Installing the Windows Client
. . . 502Installing the Linux Client
. . . 503Using SQL Command Line
. . . 504Using SQL Developer
. . . 504Using Application Express
. . . 506Using PHP
. . . 510Summary
. . . 511■ CHAPTER 30 From Databases to Datatypes
. . . 513Creating and Managing Tablespaces
. . . 513Tablespace Types
. . . 513Creating a New Tablespace
. . . 515Understanding Oracle Datatypes
. . . 516Built-in Datatypes
. . . 516ANSI-Supported Datatypes
. . . 522Creating and Maintaining Tables
. . . 523Creating a Table
. . . 523Using Constraints
. . . 525Setting Column Defaults
. . . 529Creating a Table Using a Query Against Another Table
. . . 529Modifying Table Characteristics
. . . 529Creating and Maintaining Indexes
. . . 531Using B-tree Indexes
. . . 531Using Bitmap Indexes
. . . 532Creating and Using Sequences
. . . 532Summary
. . . 534■ CHAPTER 31 Securing Oracle Database XE
. . . 535Security Terminology Overview
. . . 535Security First Steps
. . . 536Understanding Database Authentication
. . . 537Database Authentication Overview
. . . 537Database Administrator Authentication
. . . 537User Accounts
. . . 540Creating Users
. . . 540Altering Users
. . . 542Dropping Users
. . . 542Becoming Another User
. . . 543User-Related Data Dictionary Views
. . . 543Understanding Database Authorization Methods
. . . 544Profile Management
. . . 544Using System Privileges
. . . 549Using Object Privileges
. . . 551Creating, Assigning, and Maintaining Roles
. . . 553Using Database Auditing
. . . 558Auditing Locations
. . . 558Statement Auditing
. . . 559Privilege Auditing
. . . 561Schema Object Auditing
. . . 562Protecting the Audit Trail
. . . 563Summary
. . . 563■ CHAPTER 32 PHP’s Oracle Functionality
. . . 565Prerequisites
. . . 565Using Database Connections
. . . 565Connecting to the Database
. . . 566Database Connection Strings
. . . 567Disconnecting from the Database
. . . 570Retrieving and Modifying Data
. . . 570Preparing, Binding, and Executing Statements
. . . 571Retrieving Table Rows
. . . 573Inserting Rows
. . . 575Modifying Rows
. . . 578Deleting Rows
. . . 579Counting Rows Selected or Affected
. . . 581Retrieving Database Metadata
. . . 581Viewing Database Characteristics
. . . 582Viewing User Tables
. . . 583Viewing Table Columns and Column Characteristics
. . . 584Using Other Database Functions
. . . 587oci_error()
. . . 587oci_password_change()
. . . 588Summary
. . . 589■ CHAPTER 33 Transactions
. . . 591Using Transactions: Overview
. . . 591Understanding Transaction Components
. . . 592Explicit COMMIT Statement
. . . 592Implicit COMMIT Statement
. . . 594Explicit ROLLBACK Statement
. . . 594The SAVEPOINT Statement
. . . 595Performing Transactions Using PHP
. . . 597Summary
. . . 600■ CHAPTER 34 Using HTML_Table with Advanced Queries
. . . 601Using HTML_Table
. . . 601Installing HTML_Table
. . . 602Creating a Simple Table
. . . 603Creating More Readable Row Output
. . . 605Creating a Table from Database Data
. . . 606Leveraging Subqueries
. . . 607Performing Comparisons with Subqueries
. . . 608Determining Existence with Subqueries
. . . 609Database Maintenance with Subqueries
. . . 610Generalizing the Output Process
. . . 610Sorting Output
. . . 612Creating Paged Output
. . . 614Listing Page Numbers
. . . 617Summary
. . . 619■ CHAPTER 35 Using Views
. . . 621Introducing Views
. . . 621Creating and Executing User Views
. . . 622Modifying a View
. . . 625Deleting a View
. . . 626Updating a View
. . . 626Other View Types
. . . 627Data Dictionary Views
. . . 627Dynamic Performance Views
. . . 629Using Views to Restrict Data Access
. . . 629Incorporating Views into Web Applications
. . . 630Summary
. . . 631■ CHAPTER 36 Oracle PL/SQL Subprograms
. . . 633Should You Use PL/SQL Subprograms?
. . . 633Subprogram Advantages
. . . 634Subprogram Disadvantages
. . . 634How Oracle Implements Subprograms
. . . 635Creating a Stored Procedure
. . . 635Parameters
. . . 637Declaring and Setting Variables
. . . 638PL/SQL Constructs
. . . 639Creating and Using a Stored Function
. . . 644Modifying, Replacing, or Deleting Subprograms
. . . 644Integrating Subprograms into PHP Applications
. . . 645Summary
. . . 647■ CHAPTER 37 Oracle Triggers
. . . 649Introducing Triggers
. . . 649Taking Action Before an Event
. . . 650Taking Action After an Event
. . . 650Before Triggers vs. After Triggers
. . . 651Oracle’s Trigger Support
. . . 652Understanding Trigger Events
. . . 652Creating a Trigger
. . . 652Viewing Existing Triggers
. . . 656Modifying or Deleting a Trigger
. . . 657Leveraging Triggers in PHP Applications
. . . 658Summary
. . . 660■ CHAPTER 38 Indexes and Optimizing Techniques
. . . 661Understanding Oracle Index Types
. . . 661B-tree Indexes
. . . 662Bitmap Indexes
. . . 663Creating, Dropping, and Maintaining Indexes
. . . 663Monitoring Index Usage
. . . 668Using Oracle Text
. . . 669Summary
. . . 673■ CHAPTER 39 Importing and Exporting Data
. . . 675Exporting Data
. . . 675Using the SPOOL Command
. . . 675Exporting Using GUI Utilities
. . . 676Importing Data
. . . 680Summary
. . . 685■ CHAPTER 40 Backup and Recovery
. . . 687Backup and Recovery Best Practices
. . . 687Multiplexing Redo Log Files
. . . 688Multiplexing Control Files
. . . 690Enabling ARCHIVELOG Mode
. . . 691Backing Up the Database
. . . 693Manual Backups
. . . 693Automatic Backups
. . . 693Recovering Database Objects
. . . 694Summary
. . . 696■ INDEX
. . . 697xxvii
■W. JASON GILMORE has been obsessing over all things open source for more than ten years, with a primary focus on Web development technologies.
He has been extensively published in publications such as Developer.com, TechTarget, and Linux Magazine, with his writings adopted for use within the United Nations and Ford Foundation educational programs. Jason is the author of four books, including the best-selling Beginning PHP and MySQL 5, Second Edition (http://www.beginningphpandmysql.com/), published by Apress.
Jason spends his days running Apress’s open source program and his evenings writing, coding, and consulting. He’s a founding board member of CodeMash (http://www.codemash.org/), an organization dedicated to educating the development community. When not in front of the computer, Jason can typically be found dreaming up home-remodeling projects, playing chess, and making homemade pasta. In his effort to occasionally get away from the keyboard, he recently bought, of all things, a piano.
■BOB BRYLA is an Oracle 9i and 10g Certified Professional with more than 20 years of experience in database design, database application development, training, and database administration. He is an Internet database analyst and Oracle DBA at Lands’ End, Inc., in Dodgeville, Wisconsin. He is the author of several other Oracle DBA books for both the novice and seasoned professional.
xxix
■MATT WADE is a programmer, database developer, and system administrator.
He currently works for a large financial firm by day and freelances by night.
He has experience programming in several languages, though he most commonly utilizes PHP and C. On the database side of things, he regularly uses MySQL and Microsoft SQL Server. As an accomplished system adminis- trator, he regularly has to maintain Windows servers and Linux boxes and prefers to deal with FreeBSD.
Matt resides in Jacksonville, Florida, with his wife, Michelle, and their three children, Matthew, Jonathan, and Amanda. When not working, Matt can be found fishing, doing something at his church, or playing some video game. Matt was the founder of Codewalkers.com, a leading resource for PHP developers, and ran the site until 2007.
xxxi
A
lthough it’s the author who tends to receive all the credit, this material would never have seen the light of day without the tireless efforts of a truly talented supporting cast. Project managers Tracy Brown-Collins and Kylie Johnston deftly guided us through the wilderness from the very beginning, attempting to keep us on schedule despite our best efforts to do otherwise. Technical reviewer Matt Wade tracked down countless issues and provided invaluable feedback. Copy editor Jennifer Whipple did a fantastic job turning our gibberish into English. Editor and Oracle expert Jonathan Gennick helped improve both the book’s instructional and technical approaches throughout.I’d also like to especially thank Oracle oracle Bob Bryla for joining me on this long but exciting project. You did a tremendous job, and I look forward to working with you again!
Of course, this book wouldn’t exist without the amazing contributions of the open source community and the groundbreaking efforts of the Oracle Corporation. Thank you for making such amazing software available to the world.
I’d like to thank Apress cofounder and publisher Gary Cornell, assistant publisher Dominic Shakeshaft, associate publisher Grace Wong, assistant publisher Jeff Pepper, and my other Apress colleagues for yet another opportunity to work with the greatest publisher on the planet!
Finally, I’d like to thank my friends and family for their best attempts to occasionally pry me away from the laptop. At least you tried!
W. Jason Gilmore Columbus, Ohio
I would like to thank the many people at Apress for helping me along this new and winding road, especially Jonathan Gennick for convincing me to get on board in the first place, Kylie Johnston for her relentless but appreciated schedule reminders, Matt Wade for seeing things during the technical edit that would have slipped by me otherwise, Jennifer Whipple for reminding me of all those pesky grammar rules from college that I have long forgotten, and Kelly Winquist for making me appreciate Adobe Acrobat Professional.
Thanks also to all of my professional colleagues, both past and present, who provided me with inspiration, guidance, courage, and many other intangibles without which this book would not be possible. The list is long, but I would be remiss if I did not mention my co-workers, friends, and managers at Lands’ End who provided expertise, advice, and M&Ms: Phil DeKok, Brook Swenson, Martha Graber, Joe Johnson, Karen Shelton, and Amy Rees.
Bob Bryla
xxxiii
M
ahatma Gandhi once famously said, “First they ignore you, then they laugh at you, then they fight you, then you win.” Although there’s not yet any clear winner, the software industry seems to be following a similar path. Although the open source movement began back in the 1970s due to Richard Stallman’s printer-borne frustrations in an MIT computer lab, it wasn’t until the late 1990s that the community-driven approach to software development began to make any significant waves in the business environment.And with it came gasps of both horror and hilarity among the proprietary software elite. After all, a bunch of volunteers could hardly produce code of a quality approaching, let alone surpassing, that which is built in the hallowed cathedrals of software development, right? Such guffaws rang increasingly loudly despite numerous clear successes in the open source community, such as the Apache dominating position in the Web server market and Linux’s meteoric rise to become one of the world’s most popular operating systems.
But soon it became apparent this approach did work after all, as was evidenced by the rapid adoption of open source solutions for commonplace tasks such as code editing, FTP transfer, file compression, databasing, and word processing. The commercial software industry responded with overt attempts to discredit the competing open source competitors, highlighting feature deficiencies, scaling problems, lack of traditional user support, and anything else that would justify its products’
often hefty price tags.
Yet more recently, many traditional software developers are coming to the conclusion that a more cooperative attitude must be adopted if they are going to survive, let alone compete, in this brave new world. Many have even determined that open source is actually a beneficial part of the ecosystem and are making great strides toward not only making sure their software interoperates with open source projects but also offering considerable contributions by way of resources and even code.
One of the most exciting such instances of the opportunities that can arise from such efforts is the ability to use PHP, an open source project that also happens to be the world’s most popular programming language for dynamic Web development, with Oracle, a proprietary database that also happens to be the world’s most popular solution for managing data. Although for some time it has been possible to use PHP and Oracle together, only recently have these efforts really begun to pay off because of increased activities in both camps by way of not only improvements to the interface but also to the creation of learning resources, documentation, and other utilities.
It seems as with most things in life, the success of the software development industry does not lie squarely within one extreme approach but rather somewhere in between. We hope this book will highlight the riches that can be wrought from a successful collaboration between the two.
Who This Book Is For
Although this book presumes the reader has no prior experience using PHP or Oracle, seasoned users of these technologies may find it equally satisfactory because the authors have strived to create a book that strikes a balance between tutorial and reference. Our goal is to provide you with a resource that can be repeatedly referred to as you progress from a novice to an experienced developer.
Although basic introductions are often provided, this book does not seek to teach you funda- mental programming concepts. After all, the book is not titled Beginning Programming with PHP and Oracle. And it does not teach you HTML and Cascading Style Sheets (CSS). If you are a program- ming novice or are not yet versed in the aforementioned Web technologies, consider picking up one or several of the fine Apress books covering these topics.
Downloading the Code
Experimenting with the code found in this book is the most efficient way to understand the concepts presented within it. For your convenience, a ZIP file containing all of the examples is freely available for download from http://www.apress.com/.
Contacting the Authors
Jason loves corresponding with readers and invites you to e-mail him at [email protected].
Follow his latest activities at http://www.wjgilmore.com/.
To contact Bob Bryla, you can e-mail him at [email protected].
1
■ ■ ■
Introducing PHP
I
n many ways the PHP language is representative of the stereotypical open source project, created to meet a developer’s otherwise unmet needs and refined over time to meet the needs of its growing community. As a budding PHP developer, it’s important you possess some insight into how the language has progressed, as it will help you to understand the language’s strengths, and to some extent the reasoning behind its occasional idiosyncrasies.Additionally, because the language is so popular, having some understanding of the differences between the versions—most notably versions 4, 5, and 6—will help when evaluating Web hosting providers and PHP-driven applications for your own needs.
To help you quickly get up to speed in this regard, this chapter will get you acquainted with PHP’s features and version-specific differences. By the conclusion of this chapter, you’ll learn the following:
• How a Canadian developer’s Web page traffic counter spawned one of the world’s most popular scripting languages
• What PHP’s developers did to reinvent the language, making version 5 the best yet released
• Why PHP 6 is going to further propel PHP’s adoption in the enterprise
• Which features of PHP attract both new and expert programmers alike
■ Note
At the time of publication, PHP 6 was still a beta release, although many of the features are stable enough that they can safely be discussed throughout the course of the book. But be forewarned; some of these features could change before the final version is released.History
The origins of PHP date back to 1995 when an independent software development contractor named Rasmus Lerdorf developed a Perl/CGI script that enabled him to know how many visitors were reading his online résumé. His script performed two tasks: logging visitor information, and displaying the count of visitors to the Web page. Because the Web as we know it today was still young at that time, tools such as these were nonexistent, and they prompted e-mails inquiring about Lerdorf’s scripts.
Lerdorf thus began giving away his toolset, dubbed Personal Home Page (PHP).
The clamor for the PHP toolset prompted Lerdorf to continue developing the language, with perhaps the most notable early change being a new feature for converting data entered in an HTML form into symbolic variables, encouraging exportation into other systems. To accomplish this, he opted to continue development in C code rather than Perl. Ongoing additions to the PHP toolset culminated in November 1997 with the release of PHP 2.0, or Personal Home Page/Form Interpreter
(PHP/FI). As a result of PHP’s rising popularity, the 2.0 release was accompanied by a number of enhancements and improvements from programmers worldwide.
The new PHP release was extremely popular, and a core team of developers soon joined Lerdorf.
They kept the original concept of incorporating code directly alongside HTML and rewrote the parsing engine, giving birth to PHP 3.0. By the June 1998 release of version 3.0, more than 50,000 users were using PHP to enhance their Web pages.
Development continued at a hectic pace over the next two years, with hundreds of functions being added and the user count growing in leaps and bounds. At the beginning of 1999, Netcraft (http://www.netcraft.com/), an Internet research and analysis company, reported a conservative estimate of a user base of more than 1 million, making PHP one of the most popular scripting languages in the world. Its popularity surpassed even the greatest expectations of the developers, as it soon became apparent that users intended to use PHP to power far larger applications than originally anticipated. Two core developers, Zeev Suraski and Andi Gutmans, took the initiative to completely rethink the way PHP operated, culminating in a rewriting of the PHP parser, dubbed the Zend scripting engine. The result of this work was in the PHP 4 release.
■ Note
In addition to leading development of the Zend engine and playing a major role in steering the overall development of the PHP language, Suraski and Gutmans are cofounders of Zend Technologies Ltd. (http://www.zend.com/). Zend is the most visible provider of products and services for developing, deploying, and managing PHP applications. Check out the Zend Web site for more about the company’s offerings, as well as an enormous amount of free learning resources.
PHP 4
On May 22, 2000, roughly 18 months after the first official announcement of the new development effort, PHP 4.0 was released. Many considered the release of PHP 4 to be the language’s official debut within the enterprise development scene, an opinion backed by the language’s meteoric rise in popularity. Just a few months after the major release, Netcraft estimated that PHP had been installed on more than 3.6 million domains.
PHP 4 added several enterprise-level improvements to the language, including the following:
Improved resource handling: One of version 3.X’s primary drawbacks was scalability. This was largely because the designers underestimated how rapidly the language would be adopted for large-scale applications. The language wasn’t originally intended to run enterprise-class Web sites, and continued interest in using it for such purposes caused the developers to rethink much of the language’s mechanics in this regard.
Object-oriented support: Version 4 incorporated a degree of object-oriented functionality, although it was largely considered an unexceptional and even poorly conceived implementa- tion. Nonetheless, the new features played an important role in attracting users used to working with traditional object-oriented programming (OOP) languages. Standard class and object development methodologies were made available in addition to features such as object over- loading and run-time class information. A much more comprehensive OOP implementation has been made available in version 5 and is introduced in Chapter 6.
Native session-handling support: HTTP session handling, available to version 3.X users through the third-party package PHPLIB (http://phplib.sourceforge.net) was natively incorporated into version 4. This feature offers developers a means for tracking user activity and preferences with unparalleled efficiency and ease. Chapter 18 covers PHP’s session-handling capabilities.
Encryption: The MCrypt (http://mcrypt.sourceforge.net) library was incorporated into the default distribution, offering users both full and hash encryption using encryption algorithms including Blowfish, MD5, SHA1, and TripleDES, among others. Chapter 21 delves into PHP’s encryption capabilities.
ISAPI support: ISAPI support offered users the ability to use PHP in conjunction with Microsoft’s IIS Web server. Chapter 2 shows you how to install PHP on both the IIS and Apache Web servers.
Native COM/DCOM support: Another bonus for Windows users is PHP 4’s ability to access and instantiate COM objects. This functionality opened up a wide range of interoperability with Windows applications.
Native Java support: In another boost to PHP’s interoperability, support for binding to Java objects from a PHP application was made available in version 4.0.
Perl Compatible Regular Expressions (PCRE) library: The Perl language has long been heralded as the reigning royalty of the string parsing kingdom. The developers knew that powerful regular expression functionality would play a major role in the widespread acceptance of PHP and opted to simply incorporate Perl’s functionality rather than reproduce it, rolling the PCRE library package into PHP’s default distribution (as of version 4.2.0). Chapter 9 introduces this important feature in great detail and offers a general introduction to the often confusing regular expression syntax.
In addition to these features, literally hundreds of functions were added to version 4, greatly enhancing the language’s capabilities. Many of these functions are discussed throughout the course of the book.
PHP 4 represented a gigantic leap forward in the language’s maturity, offering new features, power, and scalability that swayed an enormous number of burgeoning and expert developers alike.
Yet the PHP development team wasn’t content to sit on their hands for long and soon set upon another monumental effort, one that could establish the language as the 800-pound gorilla of the Web scripting world: PHP 5.
PHP 5
Version 5 was yet another watershed in the evolution of the PHP language. Although previous major releases had enormous numbers of new library additions, version 5 contains improvements over existing functionality and adds several features commonly associated with mature programming language architectures:
Vastly improved object-oriented capabilities: Improvements to PHP’s object-oriented archi- tecture is version 5’s most visible feature. Version 5 includes numerous functional additions such as explicit constructors and destructors, object cloning, class abstraction, variable scope, and interfaces, and a major improvement regarding how PHP handles object management.
Chapters 6 and 7 offer thorough introductions to this topic.
Try/catch exception handling: Devising custom error-handling strategies within structural programming languages is, ironically, error-prone and inconsistent. To remedy this problem, version 5 supports exception handling. Long a mainstay of error management in many languages, such as C++, C#, Python, and Java, exception handling offers an excellent means for standard- izing your error-reporting logic. This convenient methodology is introduced in Chapter 8.
Improved XML and Web Services support: XML support is now based on the libxml2 library, and a new and rather promising extension for parsing and manipulating XML, known as SimpleXML, has been introduced. In addition, a SOAP extension is now available. In Chapter 20, these two exten- sions are introduced, along with a number of slick third-party Web Services extensions.
Native support for SQLite: Always keen on choice, the developers added support for the powerful yet compact SQLite database server (http://www.sqlite.org/). SQLite offers a convenient solu- tion for developers looking for many of the features found in some of the heavyweight database products without incurring the accompanying administrative overhead. PHP’s support for this powerful database engine is introduced in Chapter 22.
■ Note
The enhanced object-oriented capabilities introduced in PHP 5 resulted in an additional boost for the language: it opened up the possibility for cutting-edge frameworks to be created using the language. Chapter 25 introduces you to one of the most popular frameworks available today, namely the Zend Framework (http://framework.zend.com/).
With the release of version 5, PHP’s popularity hit what was at the time a historical high, having been installed on almost 19 million domains, according to Netcraft. PHP was also by far the most popular Apache module, available on almost 54 percent of all Apache installations, according to Internet services consulting firm E-Soft Inc. (http://www.securityspace.com/).
PHP 6
At press time, PHP 6 was in beta and scheduled to be released by the conclusion of 2007. The decision to designate this a major release (version 6) is considered by many to be a curious one, in part because only one particularly significant feature has been added— Unicode support. However, in the program- ming world, the word significant is often implied to mean sexy or marketable, so don’t let the addition of Unicode support overshadow the many other important features that have been added to PHP 6.
A list of highlights is found here:
• Unicode support: Native Unicode support has been added.
• Security improvements: A considerable number of security-minded improvements have been made that should greatly decrease the prevelance of security-related gaffes that to be frank aren’t so much a fault of the language, but are due to inexperienced programmers running with scissors, so to speak. These changes are discussed in Chapter 2.
• New language features and constructs: A number of new syntax features have been added, including, most notably, a 64-bit integer type, a revamped foreach looping construct for multidimensional arrays, and support for labeled breaks. Some of these features are discussed in Chapter 3.
At press time, PHP’s popularity was at a historical high. According to Netcraft, PHP has been installed on more than 20 million domains. According to E-Soft Inc., PHP remains the most popular Apache module, available on more than 40 percent of all Apache installations.
So far, this chapter has discussed only version-specific features of the language. Each version shares a common set of characteristics that play a very important role in attracting and retaining a large user base. In the next section, you’ll learn about these foundational features.
■ Note
You might be wondering why versions 4, 5, and 6 were mentioned in this chapter. After all, isn’t only the newest version relevant? While you’re certainly encouraged to use the latest stable version, versions 4 and 5 remain in widespread use and are unlikely to go away anytime soon. Therefore having some perspective regarding each version’s capabilities and limitations is a good idea, particularly if you work with clients who might not be as keen to keep up with the bleeding edge of PHP technology.General Language Features
Every user has his or her own specific reason for using PHP to implement a mission-critical applica- tion, although one could argue that such motives tend to fall into four key categories: practicality, power, possibility, and price.
Practicality
From the very start, the PHP language was created with practicality in mind. After all, Lerdorf’s orig- inal intention was not to design an entirely new language, but to resolve a problem that had no readily available solution. Furthermore, much of PHP’s early evolution was not the result of the explicit intention to improve the language itself, but rather to increase its utility to the user. The result is a language that allows the user to build powerful applications even with a minimum of knowledge.
For instance, a useful PHP script can consist of as little as one line; unlike C, there is no need for the mandatory inclusion of libraries. For example, the following represents a complete PHP script, the purpose of which is to output the current date, in this case one formatted like September 23, 2007:
<?php echo date("F j, Y");?>
Don’t worry if this looks foreign to you. In later chapters, the PHP syntax will be explained in great detail. For the moment just try to get the gist of what’s going on.
Another example of the language’s penchant for compactness is its ability to nest functions. For instance, you can effect numerous changes to a value on the same line by stacking functions in a particular order. The following example produces a string of five alphanumeric characters such as a3jh8:
$randomString = substr(md5(microtime()), 0, 5);
PHP is a loosely typed language, meaning there is no need to explicitly create, typecast, or destroy a variable, although you are not prevented from doing so. PHP handles such matters internally, creating variables on the fly as they are called in a script, and employing a best-guess formula for automatically typecasting variables. For instance, PHP considers the following set of statements to be perfectly valid:
<?php
$number = "5"; // $number is a string
$sum = 15 + $number; // Add an integer and string to produce integer $sum = "twenty"; // Overwrite $sum with a string.
?>
PHP will also automatically destroy variables and return resources to the system when the script completes. In these and in many other respects, by attempting to handle many of the administrative aspects of programming internally, PHP allows the developer to concentrate almost exclusively on the final goal, namely a working application.
Power
PHP developers have more than 180 libraries at their disposal, collectively containing well over 1,000 functions. Although you’re likely aware of PHP’s ability to interface with databases, manipulate form information, and create pages dynamically, you might not know that PHP can also do the following:
• Create and manipulate Adobe Flash and Portable Document Format (PDF) files
• Evaluate a password for guessability by comparing it to language dictionaries and easily broken patterns
• Parse even the most complex of strings using the POSIX and Perl-based regular expression libraries
• Authenticate users against login credentials stored in flat files, databases, and even Microsoft’s Active Directory
• Communicate with a wide variety of protocols, including LDAP, IMAP, POP3, NNTP, and DNS, among others
• Tightly integrate with a wide array of credit-card processing solutions
And this doesn’t take into account what’s available in the PHP Extension and Application Repository (PEAR), which aggregates hundreds of easily installable open source packages that serve to further extend PHP in countless ways. You can learn more about PEAR in Chapter 11. In the coming chapters you’ll learn about many of these libraries and several PEAR packages.
Possibility
PHP developers are rarely bound to any single implementation solution. On the contrary, a user is typically fraught with choices offered by the language. For example, consider PHP’s array of database support options. Native support is offered for more than 25 database products, including Adabas D, dBase, Empress, FilePro, FrontBase, Hyperwave, IBM DB2, Informix, Ingres, InterBase, mSQL, Microsoft SQL Server, MySQL, Oracle, Ovrimos, PostgreSQL, Solid, Sybase, Unix dbm, and Velocis.
In addition, abstraction layer functions are available for accessing Berkeley DB–style databases.
Several generalized database abstraction solutions are also available, among the most popular being PDO (http://www.php.net/pdo) and MDB2 (http://pear.php.net/package/MDB2). Finally, if you’re looking for an object relational mapping (ORM) solution, projects such as Propel (http://propel.
phpdb.org/trac/) should fit the bill quite nicely.
PHP’s flexible string-parsing capabilities offer users of differing skill sets the opportunity to not only immediately begin performing complex string operations but also to quickly port programs of similar functionality (such as Perl and Python) over to PHP. In addition to more than 85 string- manipulation functions, both POSIX- and Perl-based regular expression formats are supported.
Do you prefer a language that embraces procedural programming? How about one that embraces the object-oriented paradigm? PHP offers comprehensive support for both. Although PHP was orig- inally a solely functional language, the developers soon came to realize the importance of offering the popular OOP paradigm and took the steps to implement an extensive solution.
The recurring theme here is that PHP allows you to quickly capitalize on your current skill set with very little time investment. The examples set forth here are but a small sampling of this strategy, which can be found repeatedly throughout the language.
Price
PHP is available free of charge! Since its inception, PHP has been without usage, modification, and redistribution restrictions. In recent years, software meeting such open licensing qualifications has been referred to as open source software. Open source software and the Internet go together like bread and butter. Open source projects such as Sendmail, Bind, Linux, and Apache all play enormous roles in the ongoing operations of the Internet at large. Although open source software’s free availability has been the point most promoted by the media, several other characteristics are equally important if not more so:
Free of licensing restrictions imposed by most commercial products: Open source software users are freed of the vast majority of licensing restrictions one would expect of commercial counterparts. Although some discrepancies do exist among license variants, users are largely free to modify, redistribute, and integrate the software into other products.
Open development and auditing process: Although not without incidents, open source soft- ware has long enjoyed a stellar security record. Such high-quality standards are a result of the open development and auditing process. Because the source code is freely available for anyone to examine, security holes and potential problems are rapidly found and fixed. This advantage was perhaps best summarized by open source advocate Eric S. Raymond, who wrote “Given enough eyeballs, all bugs are shallow.”
Participation is encouraged: Development teams are not limited to a particular organization.
Anyone who has the interest and the ability is free to join the project. The absence of member restrictions greatly enhances the talent pool for a given project, ultimately contributing to a higher-quality product.
Summary
Understanding more about the PHP language’s history and widely used versions is going to prove quite useful as you become more acquainted with the language and begin seeking out both hosting providers and third-party solutions. This chapter satisfied that requirement by providing some insight into PHP’s history and an overview of version 4, 5, and 6’s core features.
In Chapter 2, prepare to get your hands dirty, as you’ll delve into the PHP installation and configu- ration process, and learn more about what to look for when searching for a Web hosting provider.
Although readers often liken these types of chapters to scratching nails on a chalkboard, you can gain a lot from learning more about this process. Much like a professional cyclist or race car driver, the programmer with hands-on knowledge of the tweaking and maintenance process often holds an advantage over those without by virtue of a better understanding of both the software’s behaviors and quirks. So grab a snack and cozy up to your keyboard—it’s time to build.