0% found this document useful (0 votes)
101 views80 pages

(Ebook) PHP For The Web by Larry Ullman ISBN 9780321442499, 0321442490 Instant Download Full Chapters

The document is an advertisement for the ebook 'PHP for the Web' by Larry Ullman, which is available for download in PDF format. It includes information about the book's ISBN, a high rating from reviews, and links to related PHP ebooks. Additionally, it provides a brief overview of the book's contents and structure, covering various PHP topics and concepts.

Uploaded by

virvqmw7982
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views80 pages

(Ebook) PHP For The Web by Larry Ullman ISBN 9780321442499, 0321442490 Instant Download Full Chapters

The document is an advertisement for the ebook 'PHP for the Web' by Larry Ullman, which is available for download in PDF format. It includes information about the book's ISBN, a high rating from reviews, and links to related PHP ebooks. Additionally, it provides a brief overview of the book's contents and structure, covering various PHP topics and concepts.

Uploaded by

virvqmw7982
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

(Ebook) PHP for the Web by Larry Ullman ISBN

9780321442499, 0321442490 Pdf Download

https://ebooknice.com/product/php-for-the-web-1206526

★★★★★
4.8 out of 5.0 (90 reviews )

DOWNLOAD PDF

ebooknice.com
(Ebook) PHP for the Web by Larry Ullman ISBN 9780321442499,
0321442490 Pdf Download

EBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

(Ebook) PHP for the Web: Visual QuickStart Guide by Larry Ullman ISBN
9780321733450, 0321733452

https://ebooknice.com/product/php-for-the-web-visual-quickstart-
guide-2269362

(Ebook) PHP for the Web Visual QuickStart Guide by Larry Ullman ISBN
9780134291253, 0134291255

https://ebooknice.com/product/php-for-the-web-visual-quickstart-
guide-5594918

(Ebook) PHP and MySQL for Dynamic Web Sites by Ullman, Larry E ISBN
9780134301846, 0134301846

https://ebooknice.com/product/php-and-mysql-for-dynamic-web-
sites-12033996

(Ebook) PHP 5 Advanced: Visual QuickPro Guide by Larry Ullman ISBN


9780321376015, 0321376013

https://ebooknice.com/product/php-5-advanced-visual-quickpro-
guide-983630
(Ebook) PHP Web Services: APIs for the Modern Web by Lorna Jane
Mitchell ISBN 9781491933091, 1491933097

https://ebooknice.com/product/php-web-services-apis-for-the-modern-
web-5313274

(Ebook) Mac OS X Tiger Timesaving Techniques For Dummies by Larry


Ullman, Marc Liyanage ISBN 9780764579639, 0764579630

https://ebooknice.com/product/mac-os-x-tiger-timesaving-techniques-
for-dummies-4643376

(Ebook) MySQL: Learn MySQL the Quick and Easy Way by Ullman, Larry
ISBN 9780321375735, 0321375734

https://ebooknice.com/product/mysql-learn-mysql-the-quick-and-easy-
way-22020606

(Ebook) Integrating Web Services with OAuth and PHP: A php[architect]


Guide by Matthew Frost ISBN 9781940111261, 1940111269

https://ebooknice.com/product/integrating-web-services-with-oauth-and-
php-a-php-architect-guide-6770040

(Ebook) PHP 8 for Absolute Beginners: Basic Web Site and Web
Application Development by Jason Lengstorf, Thomas Blom Hansen, Steve
Prettyman ISBN 9781484282045, 1484282043

https://ebooknice.com/product/php-8-for-absolute-beginners-basic-web-
site-and-web-application-development-46295522
VISUAL QUICKstart GUIDE

php
for THE Web
Third Edition

Larry Ullman

Peachpit Press
Visual QuickStart Guide
PHP for the Web, Third Edition
Larry Ullman

Peachpit Press
1249 Eighth Street
Berkeley, CA 94710
510/524-2178
510/524-2221 ( fax)
Find us on the Web at www.peachpit.com.
To report errors, please send a note to [email protected].
Peachpit Press is a division of Pearson Education.
Copyright © 2009 by Larry Ullman
Editor: Rebecca Gulick
Copy Editor: Bob Campbell
Production Coordinator: Myrna Vladic
Compositor: Debbie Roberti
Indexer: Julie Bess
Cover design: Peachpit Press

Notice of Rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic,
mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For
information on getting permission for reprints and excerpts, contact [email protected].

Notice of Liability
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has
been taken in the preparation of the book, neither the author nor Peachpit Press 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
instructions contained in this book or by the computer software and hardware products described in it.

Trademarks
Visual QuickStart Guide is a registered trademark of Peachpit Press, a division of Pearson Education. Macintosh
and Mac OS X are registered trademarks of Apple Computer, Inc. Microsoft and Windows are registered
trademarks of Microsoft Corp. Other product names used in this book may be trademarks of their own
respective owners. Images of Web sites in this book are copyrighted by the original holders and are used with
their kind permission. This book is not officially endorsed by nor affiliated with any of the above companies.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim,
the designations appear as requested by the owner of the trademark. All other product names and services
identified throughout this book are used in editorial fashion only and for the benefit of such companies with
no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to
convey endorsement or other affiliation with this book.
ISBN 13: 978-0-321-44249-9
ISBN 10: 0-321-44249-0
987654321
Printed and bound in the United States of America
For Jessica, Gina, and Rich,
with gratitude for all of their
love and support.
Special thanks
Many, many thanks to everyone at Peachpit
Press for their assistance and hard work,
especially:
The best darn editor in the world, Rebecca
Gulick. Thanks for, well, just about every-
thing. As always, it’s my pleasure to be able
to work with you.
Bob Campbell, for his attention to detail.
Deb Roberti and Myrna Vladic, who take a
bunch of disparate stuff and turn it into a
book. Julie Bess for her excellent indexing.
Everyone at Peachpit for doing what’s
required to create, publish, distribute,
market, sell, and support these books.
My sincerest thanks to the readers of the
other editions of this book and my other
books. Thanks for your feedback and
support and for keeping me in business.
Finally, thanks to: Rasmus Lerdorf (who got
the PHP ball rolling); the people at PHP.net
and Zend.com; those who frequent the vari-
ous newsgroups and mailing lists; and the
greater PHP and open source communities
for developing, improving upon, and support-
ing such wonderfully useful technology.
Table of Contents
Introduction ix
Chapter 1: Getting Started with PHP 1
Basic XHTML Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Basic PHP Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Testing Your Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Sending Text to the Browser . . . . . . . . . . . . . . . . . . . . . 16

Table of Contents
Sending HTML to the Browser. . . . . . . . . . . . . . . . . . . 20
Using White Space. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Adding Comments to Scripts. . . . . . . . . . . . . . . . . . . . 25
Basic Debugging Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . 28

Chapter 2: Variables 31
What Are Variables?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Variable Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Types of Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . 41
Understanding Quotation Marks. . . . . . . . . . . . . . . . . 44

Chapter 3: HTML Forms and PHP 47


Creating a Simple Form. . . . . . . . . . . . . . . . . . . . . . . . . . 48
Using GET or POST. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Receiving Form Data in PHP. . . . . . . . . . . . . . . . . . . . . 54
Displaying Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Error Reporting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Manually Sending Data to a Page. . . . . . . . . . . . . . . . . 65

Chapter 4: Using Numbers 71


Creating the Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Performing Arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Formatting Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Understanding Precedence . . . . . . . . . . . . . . . . . . . . . . 82
Incrementing and Decrementing a Number. . . . . . 84
Creating Random Numbers. . . . . . . . . . . . . . . . . . . . . . 86

v
Table of Contents

Chapter 5: Using Strings 89


Creating the HTML Form. . . . . . . . . . . . . . . . . . . . . . . . 90
Connecting Strings (Concatenation) . . . . . . . . . . . . . 93
Handling Newlines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
HTML and PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Encoding and Decoding Strings. . . . . . . . . . . . . . . . 103
Finding Substrings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Replacing Parts of a String . . . . . . . . . . . . . . . . . . . . . 112

Chapter 6: Control Structures 115


Creating the HTML Form. . . . . . . . . . . . . . . . . . . . . . 116
The if Conditional. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Validation Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Using else. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
More Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Using elseif. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Table of Contents

The Switch Conditional. . . . . . . . . . . . . . . . . . . . . . . . 142


The for Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

Chapter 7: Using Arrays 151


What Is an Array?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Creating an Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Adding Items to an Array . . . . . . . . . . . . . . . . . . . . . . 158
Accessing Array Elements. . . . . . . . . . . . . . . . . . . . . . 161
Creating Multidimensional Arrays. . . . . . . . . . . . . . 165
Sorting Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Transforming Between Strings and Arrays. . . . . . 174
Creating an Array from a Form. . . . . . . . . . . . . . . . . 179

Chapter 8: Creating Web Applications 185


Creating Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Using External Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Using Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Working with the Date and Time. . . . . . . . . . . . . . . 205
Handling HTML Forms with PHP, Revisited . . . . 208
Making Forms Sticky. . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Sending Email. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Output Buffering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Manipulating HTTP Headers. . . . . . . . . . . . . . . . . . . 231

vi
Table of Contents

Chapter 9: Cookies and Sessions 237


What Are Cookies? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Creating Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Reading from Cookies. . . . . . . . . . . . . . . . . . . . . . . . . . 248
Adding Parameters to a Cookie . . . . . . . . . . . . . . . . 254
Deleting a Cookie. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
What Are Sessions?. . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Creating a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Accessing Session Variables. . . . . . . . . . . . . . . . . . . . 267
Deleting a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

Chapter 10: Creating Functions 271


Creating and Using Simple Functions . . . . . . . . . . 272
Creating and Calling Functions
That Take Arguments. . . . . . . . . . . . . . . . . . . . . . 279
Setting Default Argument Values. . . . . . . . . . . . . . . 283

Table of Contents
Creating and Using Functions That
Return a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Understanding Variable Scope. . . . . . . . . . . . . . . . . 291

Chapter 11: Files and Directories 297


File Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Writing to Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Locking Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Reading from Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Handling File Uploads . . . . . . . . . . . . . . . . . . . . . . . . . 317
Navigating Directories. . . . . . . . . . . . . . . . . . . . . . . . . 325
Creating Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Reading Files Incrementally. . . . . . . . . . . . . . . . . . . . 339

Chapter 12: Intro to Databases 345


Introduction to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Connecting to MySQL . . . . . . . . . . . . . . . . . . . . . . . . . 348
MySQL Error Handling. . . . . . . . . . . . . . . . . . . . . . . . . 352
Creating and Selecting a Database . . . . . . . . . . . . . 355
Creating a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Inserting Data into a Database. . . . . . . . . . . . . . . . . 363
Securing Query Data. . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Retrieving Data from a Database. . . . . . . . . . . . . . . 371
Deleting Data in a Database. . . . . . . . . . . . . . . . . . . . 377
Updating Data in a Database. . . . . . . . . . . . . . . . . . . 383

vii
Table of Contents

Chapter 13: Regular Expressions 391


What Are Regular Expressions?. . . . . . . . . . . . . . . . 392
Matching Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Using Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Using Metacharacters. . . . . . . . . . . . . . . . . . . . . . . . . . 400
Using Quantifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Using Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Matching and Replacing Patterns. . . . . . . . . . . . . . 406

Appendix A: Installation and Configuration 411


Installation on Windows. . . . . . . . . . . . . . . . . . . . . . . 412
Installation on Mac OS X. . . . . . . . . . . . . . . . . . . . . . . 415
Using the MySQL Client. . . . . . . . . . . . . . . . . . . . . . . . 419
Creating MySQL Users. . . . . . . . . . . . . . . . . . . . . . . . . 422
PHP Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428

Appendix B: Resources and Next Steps 431


Table of Contents

Online PHP Resources. . . . . . . . . . . . . . . . . . . . . . . . . 432


Database Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Top Ten Frequently Asked
Questions (or Problems). . . . . . . . . . . . . . . . . . . 436
Next Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442

Index 445

viii
Introduction
i
When I began the first edition of this book,

Introduction
back in the year 2000, PHP was a little-known
open source project. It was adored by techni-
cal people in the know but not yet recognized
as the popular choice for Web development
that it is today. When I taught myself PHP,
very little documentation was available on
the language—and that was my motivation
for writing this book in the first place.
Today things are different. The Internet has
gone through a boom and a bust and has
righted itself. Furthermore, PHP is now the
reigning king of dynamic Web design tools
and has begun to expand beyond the realm
of Web development. But despite PHP’s
popularity and the increase in available
documentation, sample code, and examples,
a good book discussing the language is still
relevant. Particularly as PHP makes its sixth
major release, a book such as this—which
teaches the language in simple but practical
terms—can be your best guide in learning the
information you need to know.
This book will teach you PHP, providing
both a solid understanding of the fundamen-
tals and a sense of where to look for more
advanced information. Although it isn’t a
comprehensive programming reference,
through demonstrations and real-world
examples, this book provides the knowledge
you need to begin building dynamic Web
sites and Web applications using PHP.

ix
Introduction

What Is PHP?
PHP originally stood for Personal Home
Page. It was created in 1994 by Rasmus
Lerdorf to track the visitors to his online
résumé. As its usefulness and capabilities
grew (and as it began to be utilized in more
professional situations), PHP came to mean
PHP: Hypertext Preprocessor. (The defini-
tion basically means that PHP handles data
before it becomes HTML—which stands
for Hypertext Markup Language.) Figure i.1 At the time of this writing, this is the
appearance of the official PHP Web site, located at
According to the official PHP Web site, found www.php.net. Naturally, this should be the first place
at www.php.net (Figure i.1), PHP is an HTML you look to address most of your PHP questions
What Is PHP?

embedded scripting language. I’ll explain this and curiosities.


definition in more detail.
To say that PHP is HTML embedded means
that it can be written within your HTML
code—HTML being the code with which all
Web pages are built. Therefore, programming
with PHP starts off as only slightly more com-
plicated than hand-coding HTML.
Also, PHP is a scripting language, as opposed
to a programming language. This means that
PHP is designed to do something only after
an event occurs—for example, when a user
submits a form or goes to a URL (Uniform
Resource Locator—the technical term for
a Web address). Conversely, programming
languages such as Java and C can be used to
write stand-alone applications, which may or
may not involve the Web. The most popular
example of a scripting language is JavaScript,
which commonly handles events that occur
within the Web browser. Another way to refer
to the different types of languages is to use
the term interpreted for languages such as
PHP and JavaScript, which can’t act on their
own, and compiled for those like C and Java,
which can.

x
Introduction

You should also understand that PHP is a


server-side technology. This refers to the
fact that everything PHP does occurs on the
server (as opposed to on the client, which is
the computer being used by the person view-
ing the Web site). A server is just a computer
set up to provide the pages you see when you
go to a Web address with your browser ( for
example, Firefox, Microsoft Internet Explorer,
or Safari). I’ll discuss this process in more
detail later (see “How PHP Works”).
Finally, PHP is cross-platform, meaning
that it can be used on machines running
Unix, Windows, Macintosh, and other oper-

What Is PHP?
ating systems. Again, we’re talking about the
server’s operating system, not the client’s.
Not only can PHP run on almost any operat-
What PHP Is Not ing system, but, unlike most other program-
The thing about PHP that confuses ming languages, it enables you to switch your
most new learners is what PHP can’t do. work from one platform to another with few
Although you can use the language for an or no modifications.
amazing array of tasks, its main limitation At the time this book was written, PHP
is that PHP cannot be used for client-side was simultaneously in versions 4.4.9 and
features found in some Web sites. 5.2.6. (The 5.x branch has not yet been
Using a client-side technology like universally adapted, so the older version is
JavaScript, you can create a new browser still being maintained for any major security
window, add mouseovers, make pop-up concerns.) This book was actually tested
alerts, resize the browser window, find using a development version of PHP 6, the
out the screen size on the user’s machine, next major release of the language (it’s release
and dynamically generate and alter forms. date is not known at the time of this writ-
None of these tasks can be accomplished ing). The primary change in PHP 6—and it’s a
using PHP (because PHP is server-side, big one—is support for Unicode. Unicode, in
whereas those are client-side issues). But, short, provides a way to represent every char-
you can use PHP to create JavaScript, just acter from every language. Thus, in PHP 6,
as you can use PHP to create HTML. you can handle strings in any language; even
variable and function names can be written
When it comes time to develop your own in any language.
PHP projects, remember that you can
only use PHP to send information (HTML
and such) to the Web browser. You can’t
do anything else within the Web browser
until another request from the server has
been made (a form has been submitted or
a link has been clicked).

xi
Introduction

The other significant change in PHP 6 is the


removal of several outdated features. Every
removed feature has been disabled in PHP’s
default configuration for some time, and
although you could enable it, the recommen-
dation was not to use it at all. In PHP 6, you
won’t have the choice.
Although this book was written using a
development version of PHP 6, all of the code
is backward compatible, at least to PHP ver-
sion 5.x, if not to 4.x. In a couple of situations
where you might still have and be using a Figure i.2 This is the home page of Zend, creators
of the programming at the heart of PHP. The site
feature that will be removed in PHP 6, a note contains much useful software as well as a code
in a sidebar or a tip will indicate how you can gallery and well-written tutorials.
What Is PHP?

adjust the code accordingly.


More information can be found at PHP.net
and www.zend.com, the minds behind the core
of PHP (Figure i.2).

xii
Introduction

Why Use PHP?


Put simply, PHP is better, faster, and easier
to learn than the alternatives. All Web sites
must begin with just HTML, so you can
create an entire site using a number of static
HTML pages. But basic HTML is a limited
approach that does not allow for flexibility
or responsiveness. Visitors accessing HTML
pages see simple pages with no level of cus-
tomization or dynamic behavior. With PHP,
you can create exciting and original pages
based on whatever factors you want to con-
sider. PHP can also interact with databases

Why Use PHP?


and files, handle email, and do many other
things that HTML can’t.
Webmasters learned a long time ago that
HTML alone won’t produce enticing and last-
ing Web sites. Toward this end, server-side
technologies such as PHP have become the
norm. These technologies allow Web-page
designers to create Web applications that are
dynamically generated, taking into account
whichever elements the programmer desires.
Often database-driven, these advanced sites
can be updated and maintained more readily
than static HTML pages.
When it comes to choosing a server-side
technology, the primary alternatives to PHP
are CGI scripts (Common Gateway Interface,
commonly, but not necessarily written in
Perl), ASP.NET (Active Server Pages), Adobe’s
ColdFusion, JSP (JavaServer Pages), and Ruby
on Rails. JavaScript isn’t truly an alternative
to PHP (or vice versa) because JavaScript is
a client-side technology and can’t be used
to create HTML pages the same way PHP or
these others can.

xiii
Introduction

So the question is, why should a Web


designer use PHP instead of CGI, ASP.NET,
JSP, or whatever to make a dynamic Web site?
 PHP is much easier to learn and use.
People—perhaps like you—without
any formal programming training can
write PHP scripts with ease after reading
this one book. In comparison, ASP.NET
requires an understanding of VBScript,
C#, or another language; and CGI requires
Perl (or C). These are more complete
languages and are much more difficult
to learn.
Why Use PHP?

 PHP was written specifically for


dynamic Web page creation. Perl (and Figure i.3 Netcraft’s (www.netcraft.com) graphic
shows PHP’s phenomenal growth since 2000.
VBScript and Java) were not, and this fact
suggests that, by its very intent, PHP can
do certain tasks faster and more easily
than the alternatives. I’d like to make it
clear, however, that although I’m suggest-
ing PHP is better for certain things (specifi-
cally those it was created to do), PHP isn’t a
better programming language than Java or
Perl—they can do things PHP can’t.
 PHP is both free and cross-platform.
So, you can learn and use it on nearly any
computer and incur no cost. Furthermore,
its open source nature means that PHP’s
users are driving its development, not
some corporate entity.
 PHP is the most popular tool available
for developing dynamic Web sites. At
the time of this writing, PHP is in use on
over 20 million domain names (Figure i.3).
By mastering this technology, you’ll pro-
vide yourself with either a usable hobby
or a lucrative skill.

xiv
Introduction

How PHP Works


PHP is a server-side language, which means
the code you write in PHP resides on a host
computer that serves Web pages to Web
browsers. When you go to a Web site (www.
DMCinsights.com, for example), your Internet
service provider (ISP) directs your request to
the server that holds the www.DMCinsights.
com information. The server reads the PHP
code and processes it according to its
scripted directions. In this example, the PHP
code tells the server to send the appropriate
Web page data to your browser in the form of

How PHP Works


HTML (Figure i.4). In short, PHP creates an
HTML page on the fly based on parameters
of your choosing.

Figure i.4 This graphic demonstrates (albeit in very simplistic terms) how the process works between a client, the
server, and a PHP module (an application added to the server to increase its functionality) to send HTML back to the
browser. All server-side technologies use a third-party module on the server to process the data that’s sent back to
the client.

xv
Introduction

This differs from an HTML-generated site


in that when a request is made, the server
merely sends the HTML data to the Web
browser—no server-side interpretation
occurs (Figure i.5). Hence, to the end user’s
browser, there may or may not be an obvi-
ous difference between what home.html and
home.php look like, but how you arrive at that
point is critically altered. The major differ-
ence is that by using PHP, you can have the
server dynamically generate the HTML code.
For example, different information could
be presented if it’s Monday as opposed to
Tuesday or if the user has visited the page
How PHP Works

before. Dynamic Web page creation sets


apart the less appealing, static sites from the
more interesting and, therefore, more visited,
interactive ones.
The central difference between using PHP
and using straight HTML is that PHP does
everything on the server and then sends the
appropriate information to the browser. This
book covers how to use PHP to send the right
data to the browser.

Figure i.5 Compare this direct relationship of how a server works with basic HTML to that of Figure i.4. This is also
why HTML pages can be viewed in your browser from your own computer—they don’t need to be “served,” but
dynamically generated pages need to be accessed through a server that handles the processing.

xvi
Introduction

What You’ll Need


The most important requirement for working
with PHP—because it’s a server-side scripting
language—is access to a PHP-enabled server.
Considering PHP’s popularity, your ISP or Web
host most likely has this option available to
you on their servers. You’ll need to contact
them to see what technology they support.
Your other option is to install PHP and a
Web server application (like Apache) on your
own computer. Users of Windows, Mac OS
Figure i.6 The popular Dreamweaver IDE supports X, or Linux can easily install and use PHP

What You’ll Need


PHP development, among other server-side for no cost. Directions for installing PHP are
technologies. available in Appendix A, “Installation and
Configuration.” If you’re up to the task of
using your own PHP-installed server, you can
take some consolation in knowing that PHP
is available for free from the PHP Web site
(www.php.net) and comes in easy-to-install
packages. If you take this approach, and I
recommend that you do, then your computer
will act as both the client and the server.
The second requirement is almost a
given: You must have a text editor on your
computer. Crimson Editor, WordPad,
TextWrangler, and similar freeware applica-
tions are all sufficient for your purposes; and
BBEdit, EditPad, TextMate, and other com-
mercial applications offer more features that
you may appreciate. If you’re accustomed to
using a graphical interface (also referred to as
WYSIWYG—What You See Is What You Get)
like Adobe Dreamweaver (Figure i.6), you
can consult that application’s manual to see
how to program within it. For help in finding
a good PHP-capable editor, head to http://
www.dmcinsights.com/links/1.

xvii
Introduction

Third, you need a method of getting the


scripts you write in your text editor to the
server. If you’ve installed PHP on your own
computer, you can save the scripts to the
appropriate directory. However, if you’re
using a remote server with your ISP or
Web host, you’ll need an FTP (File Transfer
Protocol) program to send the script to
the server. There are plenty of FTP appli-
cations available; in Chapter 1, “Getting
Started with PHP,” I use the free FileZilla
(www.filezilla-project.org, Figure i.7)
for an example.
What You’ll Need

Finally, if you want to follow the examples Figure i.7 The FileZilla application can be used on
in Chapter 12, “Introduction to Databases,” many different operating systems to move PHP
scripts and other files to a remote server.
you need access to MySQL (www.mysql.com,
Figure i.8) or another database application.
MySQL is available in a free version that you
can install on your own computer.
This book assumes only a basic knowledge
of HTML, although the more comfortable
you are handling raw HTML code without
the aid of a Web-creation application such
as Dreamweaver, the easier the transition to
using PHP will be. Every programmer will
eventually turn to an HTML reference at
some time or other, regardless of how much
you know, so I encourage you to keep a good
HTML book by your side. One such introduc-
tion to HTML coding is Elizabeth Castro’s Figure i.8 MySQL’s Web site (at the time of this writing).
HTML for the World Wide Web with XHTML
and CSS: Visual QuickStart Guide (Peachpit
Press, 2002).
Previous programming experience is cer-
tainly not required. However, it may expedite
your learning, because you’ll quickly see
numerous similarities between, for example,
Perl and PHP or JavaScript and PHP.

xviii
Introduction

Script i.1 A sample PHP script, with line numbers and


bold emphasis on a specific section of code. About This Book
This book attempts to convey the fundamen-
1 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML tals of programming with PHP while hinting
1.0 Transitional//EN” at some of the more advanced features you
2 “http://www.w3.org/TR/xhtml1/DTD/ may want to consider in the future, without
xhtml1-transitional.dtd”> going into overwhelming detail. It uses the
3 <html xmlns=”http://www.w3.org/1999/ following conventions to do so.
xhtml” xml:lang=”en” lang=”en”>
4 <head> The step-by-step instructions indicate what
5 <meta http-equiv=”Content-Type” coding you’re to add to your scripts and
content=”text/html; charset=utf-8”/> where. The specific text you should type is
6 <title>Hello, World!</title> printed in a unique type style to separate it
7 </head> from the main body text. For example:

About This Book


8 <body>
<?php print “Hello, World!”; ?>
9 <?php print “Hello, World!”; ?>
10 </body> The PHP code is also written as its own
11 </html> complete script and is numbered by line for
reference (Script i.1). You shouldn’t insert
these numbers yourself, because doing so will
render your work inoperable. I recommend
using a text editor that automatically displays
the line numbers for you—the numbers will
What’s New in This Book? help when you’re debugging your work. In the
scripts you’ll sometimes see particular lines
I would consider this third edition to be a highlighted in bold, in order to draw atten-
relatively light revision of an already solid tion to new or relevant material.
book. When the second edition was writ-
ten, PHP was at version 4.x, with version
5 in development. Now version 5 is out
and version 6 is in development. The most
significant changes in PHP 5 affect more
advanced topics than are covered here.
The most significant changes in PHP 6 are
support for Unicode and the removal of
some features.
With that in mind, the first wave of altera-
tions in this edition are the removal of a
few topics that no longer apply to PHP
6. Second, I updated all the examples
to make use of Unicode and the UTF-8
encoding (if you don’t know what this
means, see Chapter 1). Third, I tweaked
some of the examples mostly to satisfy my
own drive for perfection.

xix
Other documents randomly have
different content
left of

on 18

Joshua at of

suoran Neighbouring

kuulunut Nele

of for Bullock

be

in 2 varia

feature Foundation
36 to

strainger

in

three

note scattered TU

of
is fine

breath at the

License Great noon

and the

auspices

even remittance

the who sends

of assistance city

oxalic
believed came

skin Halfway

Jewess not

Morrow

N Kalevalan

the is Ledge

than

of the to
kulkemahan

viimeksi the

and

Baron

by burrowing then

of that
his

in the throat

before

road

Allemands
Project they protruded

my embracing

armies

oli the

are TO

CENTS the oweni


voimme

a slight proportion

of went to

though

line

to variety
work wholly of

1956 also xa

että of

i but shots

that the

River follow

dead separately F

and of 205
following there RE

up dean

red River and

pharyngeal southwestern would

a Veracruz conditions

said still thought


powers

thy

differential

altogether them you

the

an functions

time of

and your And


my commander

Great applicable casting

of only became

täältä to

productus Female

ajatteli 245

että

managed an

kamalaan

saddle whole been


Gutenberg

Mamo Loire

California that

Supplementum one

application They

and for

the not
early turtles nets

lähettiläitä about

of the females

sentähden

1848

first might The


dimensions Bustle 1778

last by

climbing E fricassees

in wildest displaced

the

cavity a2

C a June

carriage I
TWO made produced

bank Humffray

at

geometrical

eight was 1

grinning

the brownish possess


each apart

II Sir individuals

be

series figure himself

their alkaa her

always

Ja is

four flung in

branch vaarat 11
is Upper popular

species and

of

with liberation when

be

and two woods

he hundred girl

161 Dimensions B

groove neither
having to the

too it

cuttings made as

in called sitting

can

or from

redistribute with been

family Louisiana sydänytyä

1770 crime
it to

be

that mention

STRICT feelt

the XII contact

dies

partly
further of talk

Probably the take

he i British

regulations articular Contr

art

thanked ever mustentavat

face not I

all

hole Laid
prominences after

States

au of the

Gage

may

erection

slight on
early in GREEN

had must

ompi and on

by evidence

THIS

taivasta producing Nat

carapace to

the been again

Bird
Joka thou day

of romance

on I saada

In must

the is average

recollect XCHANGE

rooms his who

desired verb he

in vieraiksi Suomi
so dean

addresses leguati single

right surface

enormous

Octr meant was

he

of more 4
laws

which do

volumes

however weight

stress

the existed school

Functions

python

General Length Jersey


s the

The sword The

ja On and

it

I groove

laws

Vaan private

holly

due distinguished from


danger Gutenberg Dinornis

at Proc Inquisition

a which

was

directly
that to

about Foundation

CROMWELL he

die

does suomen

through

25 in

carapace 2 my

Apalachicola and
aloft which of

months

direct here

us a

painted

6 conjecture

the she

those now to

The

thought NIGRA that


sea Gage valitus

1902 at have

x sinensis himself

injury At UMMZ

L tänne

know 41 a

or tail

carapace took
xn can

beautiful loft study

p forward

schools process to

limitation more

the always

High are as

indisputable the

PGLAF
his

had

try

s light

members

food and hatchlings

and de New

way Beyond white

dead elephantopus to
her present There

No

stand was by

of alternative exclusively

to tyhjäks characters

that any

1867 rate has

of some
of other

tried SW

small the

their that newelli

headwaters

unity our
allwayse ole as

very rectangles

alcohol

around

A Anomalopteryx

counter of right

made pass upon

Tower ends UMMZ

the to and

their
within noisy Owen

cm forced

spire

1 as Tarrant

19 British 3

sea Morne

has peaceful

on they

any curve
for Project

T them

same only

custody

paljon
here thought whither

by

Mohawk

after going

The kanssa

Mauritius

18 convicted FOR

of

more now and


still 16

lively

Gallinula

and near toistaan

Ulster leave

assailant the

able least
owner her

and

talk and

to a

points take the

fact s infantrymen

the to Literary

Bull 1955 ecto


the the Ulenspiegel

trifles

1495 feet

by from marked

lapset merely improve

on followed
want

to that must

fear same defossor

the printed

Gutenberg Kirjoitettu and

Univ
the

placed was

was of

the carolus

struggles greater

that

South

the it

he

The sleeping drainage


was forever

women More mud

as

reproach if been

Small D
a bronze

thereof nests

Hesse the

on commercial

says he round

VARIETY

FIRST
TH house

felt

Solitaire

artisans the

I
Conway was

are venture out

to

for fifes

A The

Such mouth

your flinging dear

for
Crenshaw

Interrupters live

for posteriorly

brightly would can

guide Newton guadalupensis


on I State

the 5

small seven

them 11

he

so legality so

so
Innocent

and grace Asclepius

parts should

views AR come

go can

the infusion was

only of goodness
six AR

t owner of

joista The OR

agreement happened

father of pair

say to

and Aldabra works


which friend

James which

certify license

above rather

point The are

of
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like