PDF PHP for the Web 3rd Edition Larry Ullman download
PDF PHP for the Web 3rd Edition Larry Ullman download
com
https://ebookname.com/product/php-for-the-web-3rd-edition-
larry-ullman/
OR CLICK BUTTON
DOWNLOAD EBOOK
https://ebookname.com/product/php-fast-easy-web-development-1st-
edition-matt-telles/
ebookname.com
https://ebookname.com/product/web-programming-for-business-php-object-
oriented-programming-with-oracle-1st-edition-david-paper/
ebookname.com
https://ebookname.com/product/beginning-php-apache-mysql-web-
development-michael-k-glass/
ebookname.com
https://ebookname.com/product/the-enemy-at-the-gate-andrew-wheatcroft/
ebookname.com
Exploratory Data Analysis with MATLAB Second Edition
Chapman Hall CRC Computer Science Data Analysis Wendy L.
Martinez
https://ebookname.com/product/exploratory-data-analysis-with-matlab-
second-edition-chapman-hall-crc-computer-science-data-analysis-wendy-
l-martinez/
ebookname.com
https://ebookname.com/product/the-spitfire-story-2rev-ed-edition-
alfred-price/
ebookname.com
https://ebookname.com/product/spinoza-and-german-idealism-first-
paperback-edition-forster/
ebookname.com
https://ebookname.com/product/teacher-leadership-and-behaviour-
management-1st-edition-dr-bill-rogers/
ebookname.com
https://ebookname.com/product/handling-peer-pressure-character-
education-1st-edition-kate-stevenson-clark/
ebookname.com
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 errata@peachpit.com.
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 permissions@peachpit.com.
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
v
Table of Contents
vi
Table of Contents
Table of Contents
Creating and Using Functions That
Return a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Understanding Variable Scope. . . . . . . . . . . . . . . . . 291
vii
Table of Contents
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?
x
Introduction
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
xii
Introduction
xiii
Introduction
xiv
Introduction
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
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
xvii
Introduction
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
xix
Introduction
Figure i.10 By viewing the source code received by the Web browser, you can see the HTML created
by PHP and sent by the server.
xx
Introduction
xxi
Introduction
xxii
Introduction
Questions, comments, or
suggestions?
If you have a PHP-specific question, there
are newsgroups, mailing lists, and question-
and-answer sections available on PHP-
related Web sites for you to turn to. These
are discussed in more detail in Appendix B.
Browsing through these references or search-
ing the Internet will almost always provide
you with the fastest answer.
You can also direct your questions, com-
xxiii
This page intentionally left blank
Random documents with unrelated
content Scribd suggests to you:
»Kolmasti olen vuoden kuluessa saanut häneltä sanomia», vastasi
Mary. »Kahdessa ensimmäisessä kirjeessään hän puhui ainoastaan
Roger de Condésta, ihmetellen, minkä tähden viimemainittu ei tullut
hänen jäljessään Ranskaan; mutta viimeisessä kirjeessään hän ei
maininnut Roger de Condén nimeä, vaan puhui hänen ja prinssi
Filipin lähestyvästä avioliitosta.»
»Te ette tiedä», vastasi Roger, »enkä minä saa teille puhua. Mutta
pyydän teitä uskomaan minua, kun sanon, että juuri hänen
mielenrauhansa ja onnensa vuoksi en seurannut häntä Ranskaan.
Mutta keskustellaan muista asioista. Suru on minun, enkä tahdo
tyrkyttää sitä muille. Halusin vain tietää, että hän voi hyvin ja
toivoakseni on onnellinen. Minun ei ole koskaan suotu tehdä
onnelliseksi häntä tai ketään muutakaan naista. Toivoisin, etten olisi
lainkaan sotkeutunut hänen elämäänsä, mutta en tiennyt, mitä tein.
Ja hänen kauneutensa ja hyvyytensä himousvoima kiehtoi minua
voimakkaasti, joten olin heikko enkä jaksanut vastustaa sitä tunnetta,
jota en ollut koskaan ennen eläissäni tuntenut — rakkautta.»
»Minä en häntä rakasta», virkkoi hän, »ja olen hyvilläni siitä, ettet
sinäkään rakasta, sillä tiedän Bertraden rakastavan häntä ja hänen
vain lyhyt vuosi sitten vannoneen kuolematonta rakkautta
Bertradelle. Unohdetaan, että olemme kajonneet tähän asiaan.»
Juuri näihin aikoihin kuninkaan sotilaat hävittelivät kapinallisten
ylimyksien maa-alueita ja kiskoivat raskaita veroja aikaisemmin
samana vuonna Rochesterin luona kärsimänsä kirvelevän tappionsa
kostoksi, joten pienten seurueiden oli tuskin turvallista uskaltautua
maanteille, jolleivät ne mielineet joutua Henrik kolmannen
palkkasoturien käsiin.
»Keitä olette?»
Joan de Tany istui ratsunsa selässä niin, että hän erotti Roger de
Condén kasvot, ja hänen sydämensä paisui ylpeydestä ja
rohkeudesta, kun hän näki ja ymmärsi sen tyydytyksen hymyn
häiveen, joka levisi Rogerin huulille hänen kuullessaan miehen
taisteluhaasteen ja laskiessaan oman keihäänsä tanaan.
»En tiedä hänestä muuta kuin sen», vastasi Joan de Tany, »että
hän on ihmeteltävin miekankäyttäjä ja uljain mies, mitä minun on
milloinkaan suotu nähdä. Hän nimitti itseään Roger de Condéksi,
mutta en tiedä hänestä mitään paitsi sitä, että — hän näyttää
prinssiltä ja taistelee kuin paholainen. Luultavasti hän on ihan
puolueeton, mylord, ja koska te varmastikaan ette käy sotaa naisia
vastaan, sallinette meidän niin ollen jatkaa rauhassa matkaamme,
jonka sotilaanne aiheettomalla hyökkäyksellään keskeyttivät.»
Hän oli sotilas, ja hänen kupeellaan riippui miekka. Siinä oli kylliksi
Tornin paholaiselle — juuri miekkaa hän eniten kaipasi; ja ennenkuin
mies sai hitaan järkensä tajuamaan, että vuode oli tyhjä, puristuivat
teräksenlujat sormet hänen kurkkuunsa, ja hän nujertui lattialle
henkipaton jättiläisruhon alle.
*****
Norman nyökkäsi.
»Älä puhu sellaista!» kielsi tyttö. »Ei milloinkaan ennen ole koko
maailmassa nähty sellaista uljuutta ja sellaista miekankäyttelyä eikä
sellaista miestä.»