Product Details
Upgrading to PHP 5

Upgrading to PHP 5
By A Tractenberg

List Price: £22.99
Price: £19.54 & eligible for FREE Super Saver Delivery. Details

Availability: Usually dispatched within 24 hours
Dispatched from and sold by Amazon.co.uk

24 new or used available from £6.20

Average customer review:

Product Description

If you're using PHP 4, then chances are good that an upgrade to PHP 5 is in your future. The more you've heard about the exciting new features in PHP 5, the sooner that upgrade is probably going to be. Although an in-depth, soup-to-nuts reference guide to the language is good to have on hand, it's not the book an experienced PHP programmer needs to get started with the latest release. What you need is a lean and focused guide that answers your most pressing questions: what's new with the technology, what's different, and how do I make the best use of it? In other words, you need a copy of Upgrading to PHP 5. This new book is targeted toward PHP developers who are already familiar with PHP 4. Rather than serve as a definitive guide to the entire language, the book zeroes in on PHP 5's new features, and covers these features definitively. You'll find a concise appraisal of the differences between PHP 4 and PHP 5, a detailed look at what's new in this latest version, and you'll see how PHP 5 improves on PHP 4 code. See PHP 4 and PHP 5 code side-by-side, to learn how the new features make it easier to solve common PHP problems. Each new feature is shown in code, helping you understand why it's there, when to use it, and how it's better than PHP 4. Short, sample programs are included throughout the book. Topics covered in Upgrading to PHP 5 include: The new set of robust object-oriented programming features; An improved MySQL extension, supporting MySQL 4.1, prepared statements, and bound parameters; Completely rewritten support for XML: DOM, XSLT, SAX, and SimpleXML; Easy web services with SOAP; SQLite, an embedded database library bundled with PHP 5; Cleaner error handling with exceptions; Other new language features, such as iterators, streams, and more. Upgrading to PHP 5 won't make you wade through information you've covered before. Written by Adam Trachtenberg, coauthor of the popular PHP Cookbook, this book will take you straight into the heart of all that's new in PHP 5. By the time you've finished, you'll know PHP 5 in practice as well as in theory.


Product Details

  • Amazon Sales Rank: #388362 in Books
  • Published on: 2004-08-03
  • Original language: English
  • Number of items: 1
  • Binding: Paperback
  • 327 pages

Editorial Reviews

From the Publisher
This new book is targeted toward PHP developers who are already familiar with PHP 4. Upgrading to PHP 5 offers a concise appraisal of the differences between PHP 4 and PHP 5, a detailed look at what's new in this latest version, and an explanation of how these changes affect you. The book also covers more advanced features and provides hands-on experienced through short, sample programs included throughout.

About the Author
Adam Trachtenberg has an MBA from Columbia Business School. At business school, he focused on general management and operations, with an emphasis on the field of technology. Adam also has a BA from Columbia University. As an undergraduate, he majored in mathematics and his other studies included computer science and Chinese. Before returning to school, he co-founded and served as Vice President for Development at two companies, Student.Com and TVGrid.Com. At both firms, he led the front- and middle-end web site design and development, worked on corporate planning and strategy, and served as liaison between the product and marketing teams. During study breaks, Adam enjoys playing squash, reading fiction, and eating in New York City's many wonderful restaurants. He wishes he was a better at playing pool, knew the constellations, and was handy around the house.

Excerpted from Upgrading to PHP 5 by Adam Trachtenberg. Copyright © 2004. Reprinted by permission. All rights reserved.
Chapter 4 SQLite

Substituting text files for a database is like cutting a fish with a hammer. You might get it to work, but it’s going to be a really messy process. When your application needs a server-side storage mechanism but you can’t rely upon the presence of a specific database, turn to SQLite. It correctly handles locking and concurrent accesses, the two big headaches with homebrewed flat files.

Since the SQLite database is bundled with PHP 5, now every PHP 5 script can read, write, and search data using SQL. SQLite differs from most databases because it is not a separate application. Instead, SQLite is an extension that reads from and writes to regular files on the hard drive. Any PHP users who have permission to manipulate files can use SQLite to edit a database, just like they can use GD to edit images.

Although the name SQLite hints at a less than full-featured product, SQLite actually supports almost all of SQL92, the SQL standard specification. Besides the usual INSERTs and SELECTs, with SQLite you can also use transactions, query using subselects, define your own functions, and invoke triggers.

SQLite actually performs most actions more quickly than many other popular databases. In particular, SQLite excels at SELECTing data. If your application does an initial (or periodic) data INSERT and then reads many times from the database, SQLite is an excellent choice. The PHP web site uses SQLite to handle some forms of searches.

Unfortunately, SQLite has some downsides. Specifically, when you update the database by adding new data, SQLite must lock the entire file until the alteration completes. Therefore, it does not make sense in an environment where your data is constantly changing. SQLite does not have any replication support, because there’s no master program to handle the communication between the master database and its slaves.

Additionally, SQLite has no concept of access control, so the GRANT and REVOKE keywords are not implemented. This means you cannot create a protected table that only certain users are allowed to access. Instead, you must implement access control by using the read and write permissions of your filesystem.

SQLite is not for sites that are flooded with heavy traffic or that require access permissions on their data. But for low-volume personal web sites and small business intranet applications, SQLite lets you do away with the burden of database administration. SQLite is also perfect for log file analysis scripts and other applications that benefit from a database but whose authors don’t want to require the user to install one. SQLite is bundled with PHP 5, so unless it has been specifically omitted, it’s part of every PHP 5 installation.

The SQLite home page has more details about SQLite’s features, limitations, and internals. A list of PHP’s SQLite functions is online.

This chapter starts off with SQLite basics: creating databases, passing SQL queries to SQLite, and retrieving results—everything you need to start using SQLite. It then moves on to alternative SQLite retrieval functions and interfaces, including a nifty object-oriented interface. After covering how to talk with SQLite, this chapter shows how to improve SQLite performance with indexes and how to gracefully handle errors. It closes with a few advanced features: transactions and user-defined functions, which help keep your data consistent and extend SQLite, respectively.

SQLite Basics
It’s easy to get up and running with SQLite. Its design eliminates the need for any configuration variables, such as a database server name or a database username and password. All you need is the name of a file where the data is stored:

$db = sqlite_open('/www/support/users.db');
sqlite_query($db, 'CREATE TABLE users(username VARCHAR(100),
password VARCHAR(100))');

This creates a users table stored in the database file located at /www/support/users.db. When you try to open a database file that doesn’t already exist, SQLite automatically creates it for you; you don’t need to execute a special command to initialize a new database.

If you cannot seem to get SQLite to work, make sure you have both read and write permission for the location on the filesystem where you’re trying to create the database.

SQLite has even fewer data types than PHP—everything’s a string. While you can define a column as INTEGER, SQLite won’t complain if you then INSERT the string PHP into that column. This feature (the SQLite manual declares this a feature, not a bug) is unusual in a database, but PHP programmers frequently use this to their advantage in their scripts, so it’s not a completely crazy idea. A column’s type matters only when SQLite sorts its records (what comes first: 2 or 10?) and when you enforce UNIQUEness (0 and 0.0 are different strings, but the same integer).

The table created in this example has two columns: username and password. The columns’ fields are all declared as VARCHARs because they’re supposed to hold text. Although it doesn’t really matter what type you declare your fields, it can be easier to remember what they’re supposed to hold if you give them explicit types.

Inserting Data
Add new rows to the database using INSERT and sqlite_db_query( ):

$username = sqlite_escape_string($username);
$password = sqlite_escape_string($password);

sqlite_query($db, "INSERT INTO users VALUES ('$username', '$password')");

You must call sqlite_escape_string( ) to avoid the usual set of problems with single quotes and other special characters. Otherwise, a password of abc'123 will cause a parser error. Don’t use addslashes( ) instead of sqlite_ escape_string( ), because the two functions are not equivalent.

Retrieving Data
To retrieve data from an SQLite database, call sqlite_query( ) with your SELECT statement and iterate through the results:

$r = sqlite_query($db, 'SELECT username FROM users');
while ($row = sqlite_fetch_array($r)) {
// do something with $row
}

By default, sqlite_fetch_array( ) returns an array with the fields indexed as both a numeric array and an associative array. For example, if this query returned one row with a username of rasmus, the preceding code would print:

Array (
[0] => rasmus
[username] => rasmus
)


Customer Reviews

A very comprehensive guide for the practiced5
Having used PHP4 to develop a couple of web sites, I decided that I would keep myself abreast of what is coming with the advent of PHP5. I had already purchased 'Learning PHP5' from the same publishers, in order to remind myself of the basics and also to see if the fundamentals were any different! This book augments this beautifully, going into the next level of detail from the aforementioned book. It covers a lot of the new PHP5 functionality, and illustrates PHP4 examples and their PHP5 counterparts very well. If you're just starting out, I'd definitely go for either 'Learning PHP5' or the 'PHP 5 and MySQL Bible'. If you're already fairly PHP savvy, then this book will prove highly useful! A great purchase.

No fluff guide to new features in PHP 55
I was fortunate to start working with PHP after it moved to version 5. Although they are separate books, each with a very different style, I came to regard "Upgrading to PHP 5" as Part 2 of O'Reilly's two main PHP 5 instructional volumes (Part 1 being "Learning PHP 5" by David Sklar). "Upgrading to PHP 5" is fast-paced, wastes no space on elaborate or unnecessary narrative and provides short but helpful examples for each significant issue. It's a relatively small book and is less expansive than than the more detailed books in O'Reilly's "Programming" series, but it contains more detailed explanation and examples than is typically the case with the terse Nutshell books. (IMHO anyway). In particular I found the chapters on MySQLi (especially the sections on prepared statements, SQLite and error handling very useful. It's definitely for those serious about learning the "new" features in version 5 of the language. (scare-quotes around "new" because it's now June, 2007)

While "Upgrading to PHP 5" provides a good overview of the features unique to PHP 5, its text and examples will be too pithy for most starting out with object-oriented programming. For getting to grips with OOP using PHP 5, I wholeheartedly recommend "PHP 5 Objects, Patterns and Practice", a superb work by Matt Zandstra (Apress). Not only is it very well-written and contains a great discussion of general OOP principles, but there's also plenty of consideration of OOP application design specifically in relation to PHP's 'per-request' nature (i.e. PHP's eschewal of a persistent memory scope in which to 'cache' your OOP class definitions and object instances). That is, Zandstra's OOP is crafted mindful of the fact that (where 3rd party memory caching is not used) your OOP application design will need to run with each request and thus will need to avoid the kind of OO complexity you typically encounter in J2EE or even OO ColdFusion application designs.

To sum up, if you're new to PHP I recommend all three books. If you're already a black belt in PHP 4, then I suspect that the Trachtenberg and Zandstra books will be all you need to move to both PHP 5 and OOP. (Although I should probably warn that the Zandstra book may be a bit advanced for absolute OOP noobs...). Finally, if you can afford it, get the excellent second edition of the "PHP Cookbook" too! It'll probably save you many hours of rooting around on well-meaning but aesthetically challenging PHP-related bulletin boards.