Peachpit Press Visual QuickStart Guide PHP for the Web 3rd (2009).pdf

(7488 KB) Pobierz
664873474 UNPDF
VISUAL QUICKStArt GUIDE
php
for THE WEb
third Edition
Larry Ullman
Peachpit Press
664873474.001.png 664873474.002.png
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
he 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. his book is not oicially endorsed by nor ailiated 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
identiied throughout this book are used in editorial fashion only and for the beneit 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 ailiation with this book.
ISBN 13: 978-0-321-44249-9
ISBN 10: 0-321-44249-0
9 8 7 6 5 4 3 2 1
Printed and bound in the United States of America
Special thanks
Many, many thanks to everyone at Peachpit
Press for their assistance and hard work,
especially:
he best darn editor in the world, Rebecca
Gulick. hanks 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 stuf 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. hanks 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
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
664873474.003.png
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
he if Conditional..............................121
Validation Functions ...........................124
Using else......................................127
More Operators ................................130
Using elseif ....................................138
he Switch Conditional ........................142
he 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 Bufering...............................227
Manipulating HTTP Headers...................231
vi
664873474.004.png
 
Zgłoś jeśli naruszono regulamin