- Shopping Bag ( 0 items )
What is this book about?
Beginning PHP5 is a complete tutorial in PHP5's language features and functionality, beginning with the basics and building up to the design and construction of complex data-driven Web sites. Fully functioning applications are developed through the course of the book. Other features of the book include ...
What is this book about?
Beginning PHP5 is a complete tutorial in PHP5's language features and functionality, beginning with the basics and building up to the design and construction of complex data-driven Web sites. Fully functioning applications are developed through the course of the book. Other features of the book include installation guide and troubleshooting tips, introduction to relational databases, practical working examples and applications, and a detailed language reference.
Here are the new topics in this edition:
Chapter 1: Getting Up and Running.
Chapter 2: Writing Simple Programs.
Chapter 3: PHP, HTML, and State.
Chapter 4: Decisions, Loops, and Arrays.
Chapter 5: Robust and Healthy Code.
Chapter 6: Writing High-Quality Code.
Chapter 7: Files and Directories.
Chapter 8: XML.
Chapter 9: An Introduction to Databases and SQL.
Chapter 10: Retrieving Data from MySQL Using PHP.
Chapter 11: Using PHP to Manipulate Data in MySQL.
Chapter 12: An Introduction to Object-Oriented Programming.
Chapter 13: Working with UML and Classes.
Chapter 14: PEAR.
Chapter 15: PHP5 and E-Mail.
Chapter 16: Generating Graphics.
Chapter 17: Case Study: A PHP Logging Agent.
Appendix A: Answers.
Appendix B: PHP Functions Reference.
Appendix C: Using SQLite.
Appendix D: ODBC.
Appendix E: PHP CLI.
Appendix F: Configuring PHP5.
PHP, which stands for HyperText Preprocessor, is widely used for creating programmed features for Web sites because it is easy to learn and also because PHP syntax is drawn from other widely used languages, making it familiar to many programmers. In this chapter we present a very brief history of PHP, and then discuss the nature of PHP as it relates to the Web.
Before you can get into the nitty-gritty of programming with PHP5, you need a clear understanding of how PHP programs work across the Web, and that obviously implies knowledge of the Web protocol called HyperText Transfer Protocol (HTTP). HTTP is the language or format for communications from browser to Web server and back, and is therefore fundamental to many aspects of PHP. HTTP gets some coverage in this chapter, and quite a bit more in Chapter 3.
You'll see how to properly setup PHP on a Linux server, and on a Windows server as well. PHP programs run in conjunction with Web pages, which in turn run (or are distributed by) Web server software (such as Apache or IIS), which in turn run on top of an operating system (such as Linux or Windows). Although it's not strictly necessary to know everything about network operating systems to build good PHP programs, there are many aspects of PHP that are controlled or affected by the Web server. If you're unfamiliar with server computers, Web servers, and the like, don't worry. You'll soon see how they work, and look at the requirements and process of installing basic Web server software.
This chapter leads you through installing PHP on a Red Hat Linux machine running Apache, and through installing PHP on a Windows 2000 machine running IIS. Just pick the one that's right for you.
You'll also examine the contents of the PHP configuration file php.ini with you, and test your PHP installation.
Obviously there's a lot of work for you in this chapter, so let's get started.
The Roots of PHP
PHP is a programming language designed to work with HTML, but unlike HTML, PHP has data processing capabilities. If you are familiar with HTML, you know that it is not really a programming language, but more of a rendering language-that is, HTML enables you to write Web pages using code that creates a pleasing (hopefully) display of text, graphics, and links within a browser. Although there are a few helpful features of HTML (such as the capability to cause a form submission), for the most part HTML does nothing programmatically. For example, there are no HTML commands that enable you to add two numbers together, or access a database.
If you remember the Web back in the early '90s, you may recall that early Web pages were made from HTML code written as plain text source files. When you made a connection to a Web site with your browser, the Web server software sent these plain text HTML files to be processed and rendered into Web pages. Your browser actually did the rendering process (and still does, to be sure), but if you clicked View[right arrow]Page Source, you'd see the raw HTML code.
So where does PHP fit in with HTML? PHP began as PHP/FI, developed in 1995 by Rasmus Lerdorf from some Perl scripts he had created for tracking accesses to his online resume. Eventually, Rasmus wrote an implementation in C, released the source code to the public, and by the beginning of 1998 version 3.0 of PHP was released (written by Rasmus Lerdorf, Andi Gutmans, and Zeev Suraski), the first version that is very similar to the current releases of PHP.
The main goal of PHP is to enable users to easily develop dynamic Web pages. The difference between dynamic Web pages and static Web pages is that the content and structure of dynamic Web pages may change each time they are accessed (that's what the back-end programming is for) whereas the content and structure of static Web pages is fixed and does not change unless the designer manually changes them.
Unlike many other languages, PHP can be embedded directly into HTML, making it quite easy for those familiar with HTML to grasp how to add back-end, programmatic functionality to their Web pages. This single capability is one of the most important factors in PHP's usefulness, and thereby its popularity. But have no doubt that PHP is growing into a much more full-features language going well beyond the initial intentions of its authors. PHP intends to be the primary language for a great variety of online and offline applications, and PHP5 is showing every sign of doing just that.
And you shouldn't forget how well PHP works with HTTP (HyperText Transfer Protocol), the communications protocol (pre-agreed format for data communications) for Web. Whenever you click a link or enter a Web address into your browser, a request in HTTP format is sent to the Web server, which responds by sending back the Web page. If the Web page isn't found, you'll probably get the "404 Not Found" error. Sending back the correct page or sending an error if the page is not found are HTTP functions. We discuss HTTP thoroughly in Chapter 2 because several important aspects of PHP applications depend on HTTP.
Installing, Configuring, and Running PHP
Before you can write a PHP application that works with your Web pages, you need to have PHP installed and configured. Because you'll be writing a Web application, it's a given that you'll need a Web server and some Web pages (a short HTML primer is provided in Chapter 3, although it's assumed that you know or can easily pick up how to make basic Web pages). You'll also need to download, install, and configure PHP, so we provide complete instructions about how to do these things in the coming sections. Note that some configuration options for PHP are related to very specific application requirements (you don't need to worry about them unless you need them) so many of the options aren't discussed until you reach the appropriate chapter.
To run the code in this book you will need at least the following software:
Server software (an operating system such as Windows 2000 or Linux)
A PHP-compatible Web server (such as Apache or Internet Information Server (IIS)
PHP5 (get the download from php.net)
A relational database system (starting at Chapter 9, we use SQLite or MySQL)
A Web browser (such as IE, Mozilla, and so on)
A text editor, such as Notepad, Emacs, vi, BBEdit, and so on.
You shouldn't have to worry about hard drive space or RAM, unless you are working on a very old system, or one that is overloaded. PHP doesn't take up much room, and runs very efficiently.
You can run all of the software listed here on the same computer, for development purposes. If you have access to several networked computers, you may want to install all of your server software on one (typically either a UNIX or Windows NT/2000 computer), and use another networked computer as your client machine. For the purposes of this book, we will generally assume you are running all of the software on a single computer. This is the configuration used by most Web developers.
php.ini, the PHP Configuration File
There are two examples of PHP configuration files that come with PHP when you download it: php.ini-dist and php.ini-recommended. After you download and install PHP, there will be one file named php.ini strategically placed on your system, and each time PHP starts it will read this file and set itself up accordingly. The php.ini file can be written out by hand, but of course most of us just modify either the dist or recommended file to suit our needs, and then copy and rename it into the appropriate folder.
However, you should note the following lines in the top of the dist file:
; This is the default settings file for new PHP installations.
; By default, PHP installs itself with a configuration suitable for
; development purposes, and *NOT* for production purposes.
The settings in the dist file are used for nearly all of the examples in this book and we'll let you know whenever the configuration settings are changed. But you will want to use the recommended file when you complete your applications and copy them over to your production server, and you should be aware that you may need to rewrite your code a little bit to work properly with the recommended file's configuration settings. We'll discuss this more as we go along.
Setting Up a Test Machine
In this chapter, we'll walk through setting up PHP5 on a Red Hat Linux machine running the Apache Web server, as well as on a Windows 2000 machine running Internet Information Server (IIS). You can run PHP5 with many other operating systems and Web servers, so see the PHP5 documentation for installation and configuration on other servers. And there are a variety of installation methods you can use. For example, there is an automatic installer for the Windows version, whereas you can install the Linux version using RPMs (for some versions of Linux), and you can also download and compile the Linux versions from the original source code if you like. None of the installations are all that difficult if you follow procedures correctly, and the examples we provide are a good starting point for many of the installations available.
There are some third-party installers (often open-source and free) out there, if you want to look for them. For instance, you might try PHPTriad or Foxserv in Google.
If you don't already know, a computer doesn't need to be attached to the Internet, or even to a network, to run Web server software. If you install a Web server on a computer, it's always possible to access that Web server from a Web browser running on the same machine, even if it doesn't have a network card or modem. Of course, to download and install the software you need, you have to have access to an Internet connection. But you don't need it to be active just because you're running your Web server.
Once you have a Web server installed and running, you'll install PHP5 alongside it. There's some configuration required to tell the Web server how to run PHP programs, and we'll walk through that process before we start PHP. There is an automatic installer to be found with most distributions of PHP; we'll use a primarily manual process to illustrate what's happening during installation.
What if it goes wrong? The README and INSTALL files that are included in most PHP downloads, as well as the PHP manual at php.net/manual/, provide detailed information which may be more up-to-date than the information here, which covers the PHP5.0.2 release.
Where Do You Start?
There are two main installation paths from which to choose, and each simply depends on which operating system you're using:
Installing PHP5 with the Apache Web Server on Linux (we use Red Hat Fedora Linux)
Installing PHP5 with Microsoft Internet Information Server on Windows (we use Windows 2000)
PHP5 can be installed on a great variety of Web server/operating system combinations, including under Apache on Windows. The two systems we're using are the easiest to get working. If neither of them suits you, of course you can install whatever other configuration you want-you should still be able to run all of the examples in the book. Refer to the PHP5 manual for more general installation instructions.
One of the basic choices to make when installing PHP5 with your Web server is whether to run it as a CGI binary or as a separate static or dynamic module. CGI (Common Gateway Interface) is a very useful way to run interpreters such as PHP5. Because of security risks (see the "Running as a CGI" section later in this chapter for more information), compiling PHP5 as a static or dynamic module is recommended under most circumstances. Our installations (on Linux and on Windows) load PHP as a separate SAPI (Server Application Programming Interface) module. On Windows, the ISAPI filter was used to run PHP as a SAPI module.
Although it is most common to run PHP in conjunction with a Web server, so that Web pages with a file extension such as .php are processed through the PHP interpreter before the finished page is sent back to the browser, there is also a command line utility that enables you to run PHP code from the command line. It is present from any of the installation types we demonstrate. You can find plenty of documentation about it on the PHP site (php.net).
Creating and running PHP Web applications in a satisfactory way implies that you are running (or have access to) a Web server upon which PHP is (or can be) installed, and that the installation has been tested and runs properly. It also implies that PHP has been (or can be) configured to support the needs of your PHP programs. There are a couple scenarios under which these requirements can be achieved:
You are running a desktop or server machine, operating system, and Web server compatible with PHP, and PHP has been installed and configured.
You are running a desktop or server machine connected to the Internet, with access to a Web hosting account supported by a Web server with which PHP has been installed and configured.
The vast majority of desktop machines run Windows 98, NT, 2000, 2003, and XP. In many cases you can get a free copy of Personal Web Server (PWS) and install it on a machine running one of these operating systems. PHP is compatible with PWS, so you can install and configure PHP on desktop machines running basic operating systems such as Windows 98. Server operating systems such as Windows NT, 2000, and 200, come with Internet Information Server (IIS). PHP is compatible with IIS, and you can install and configure PHP on these machines. Our Windows 2000 installation of PHP5 uses IIS as a Web server.
The majority of Web-hosting computers run some version of Linux, such as Debian, RedHat, FreeBSD, and so on. The Web server of choice for these machines is Apache. PHP is compatible with Linux and Apache, and you can install and configure PHP on these systems, but if you are not in charge of the Web-hosting computer (and many times you won't be) you'll probably have little control over the installation and configuration.
Excerpted from Beginning PHP5 by Dave W. Mercer Allan Kent Steven D. Nowicki David Mercer Dan Squier Wankyu Choi Heow Eide-Goodman Ed Lecky-Thompson Clark Morgan Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
Posted November 6, 2004
The PHP code in this book (which can be downloaded from the publisher's site in .zip or .rar files) can be used on a local machine as well as uploaded to your server and used in the real world. You may need to make modifications to the code to reflect the domain name, etc. of your particular server. With that out of the way, let me say that this book has been extremely useful to me (a beginner). Not only does it start with the fundamentals of PHP, including installation, it continues through many advanced areas of programming so that you do not need to rush out and buy another book to complete your project. It even covers some basic HTML integration for those that are new to that too. All in all, I have not seen any other book on PHP5 with the depth and ease-of-use of this one.Was this review helpful? Yes NoThank you for your feedback. Report this reviewThank you, this review has been flagged.
Posted August 22, 2011
No text was provided for this review.