Visual QuickStart Guide
PHP for the Web
Fifth Edition
LARRY ULLMAN
Visual QuickStart Guide
PHP for the Web, Fifth Edition
Larry Ullman
Peachpit Press
1301 Sansome Street
San Francisco, CA 94111
Find us on the web at: www.peachpit.com
To report errors, please send a note to:
Peachpit Press is a division of Pearson Education.
Copyright 2016 by Larry Ullman
Senior Editor: Karyn Johnson
Development Editor: Robyn G. Thomas
Copyeditor: Liz Welch
Technical Reviewer: Paul Reinheimer
Proofreader: Scout Festa
Production Coordinator: David Van Ness
Compositor: WolfsonDesign
Indexer: Valerie Haynes Perry
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 .
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 websites 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-134-29125-3
ISBN-10: 0-134-29125-5
9 8 7 6 5 4 3 2 1
Printed and bound in the United States of America
Dedication
For Jessica, Gina, and Rich, with gratitude for all their love and support.
Special Thanks to:
Many, many thanks to everyone at Peachpit Press for their assistance and hard work, especially:
Robyn Thomas, for managing the project adeptly, and for knowing when to push and poke.
Liz Welch, for fine-tuning my prose with her copyediting skills.
Paul Reinheimer, for the superlative technical review, keeping me honest, and finding things to improve even in a fifth edition.
Scout Festa, for the sharp proofreading eye.
David Van Ness, who takes a bunch of disparate stuff and turns it into a book.
Thanks for doing whats 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.
Rasmus Lerdorf (who got the PHP ball rolling), the people at PHP.net and Zend.com, those who frequent the various newsgroups and mailing lists, and the greater PHP and open source communities for developing, improving upon, and supporting such wonderfully useful technology.
Zoe and Sam, for continuing to be the kid epitome of awesomeness.
Jessica, for doing everything you do and everything you can.
Introduction
When I began the first edition of this book in 2000, PHP was a little-known open source project. It was adored by technical 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 languageand 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 expanded somewhat beyond the realm of just web development. But despite PHPs popularity and the increase in available documentation, sample code, and examples, a good book discussing the language is still relevant. Although PHP is in the beginnings of its sixth major release, a book such as thiswhich teaches the language in simple but practical termscan still 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 fundamentals and a sense of where to look for more advanced information. Although it isnt a comprehensive programming reference, this book, through demonstrations and real-world examples, provides the knowledge you need to begin building dynamic websites and web applications using PHP.
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 rsum. 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 definition basically means that PHP handles data before it becomes HTMLwhich stands for Hypertext Markup Language.
According to the official PHP website, found at www.php.net , PHP is a popular general-purpose scripting language that is especially suited to web development. More specifically, PHP is a scripting language commonly embedded within HTML. Lets examine what this means in more detail.
As of this writing, this is the appearance of the official PHP website, located at www.php.net. Naturally, this should be the first place you look to address most of your PHP questions and curiosities.
To say that PHP can be embedded into HTML means that PHP code can be written within your HTML codeHTML being the language with which all web pages are built. Therefore, programming with PHP starts off as only slightly more complicated than hand-coding HTML.
Also, PHP is a scripting language, as opposed to a compiled language. This means that PHP is designed to do something only after an event occursfor example, when a user submits a form or goes to a URL (Uniform Resource Locatorthe technical term for a web address). Another popular example of a scripting language is JavaScript, which commonly handles events that occur within the browser. Both PHP and JavaScript can also be described as interpreted, because the code must be run through an executable, such as the PHP module or the browsers JavaScript component. Conversely, compiled languages such as C and C++ can be used to write stand-alone applications that can act independently of any event.