PHP For The Web: Visual QuickStart Guide

2y ago
54 Views
8 Downloads
3.18 MB
57 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Rafael Ruffin
Transcription

V I S UA L Q U I C K S TA R T G U I D EPHP forthe WebFifth EditionLARRY ULLMANPeachpit Press

Visual QuickStart GuidePHP for the Web, Fifth EditionLarry UllmanPeachpit Press1301 Sansome StreetSan Francisco, CA 94111Find us on the web at: www.peachpit.comTo report errors, please send a note to: errata@peachpit.comPeachpit Press is a division of Pearson Education.Copyright 2016 by Larry UllmanSenior Editor: Karyn JohnsonDevelopment Editor: Robyn G. ThomasCopyeditor: Liz WelchTechnical Reviewer: Paul ReinheimerProofreader: Scout FestaProduction Coordinator: David Van NessCompositor: WolfsonDesignIndexer: Valerie Haynes PerryNotice of RightsAll 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. ForInformation on getting permission for reprints and excerpts, contact permissions@peachpit.com.Notice of LiabilityThe information in this book is distributed on an “As Is” basis, without warranty. While every precaution hasbeen taken in the preparation of the book, neither the author nor Peachpit Press shall have any liability to anyperson or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by theinstructions contained in this book or by the computer software and hardware products described in it.TrademarksVisual QuickStart Guide is a registered trademark of Peachpit Press, a division of Pearson Education. Macintoshand Mac OS X are registered trademarks of Apple Computer, Inc. Microsoft and Windows are registeredtrademarks of Microsoft Corp. Other product names used in this book may be trademarks of their own respectiveowners. Images of websites in this book are copyrighted by the original holders and are used with their kindpermission. 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 astrademarks. 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 servicesidentified throughout this book are used in editorial fashion only and for the benefit of such companies with nointention of infringement of the trademark. No such use, or the use of any trade name, is intended to conveyEndorsement or other affiliation with this book.ISBN-13: 978-0-134-29125-3ISBN-10: 0-134-29125-59 8 7 6 5 4 3 2 1Printed and bound in the United States of America

DedicationFor 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 assistanceand hard work, especially:Robyn Thomas, for managing the project adeptly, and for knowing whento 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 intoa book.Thanks 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 andmy other books. Thanks for your feedback and support and for keepingme in business.Rasmus Lerdorf (who got the PHP ball rolling), the people at PHP.netand Zend.com, those who frequent the various newsgroups and mailinglists, 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.

Table of ContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . ixChapter 1Getting Started with PHP . . . . . . . . . . . . . . . . . 1Basic HTML Syntax . . . . . . . . . . . . . . . . . . . . . . 2Basic PHP Syntax . . . . . . . . . . . . . . . . . . . . . . . 7Using SFTP . . . . . . . . . . . . . . . . . . . . . . . . . . 10Testing Your Script . . . . . . . . . . . . . . . . . . . . . . 12Sending Text to the Browser . . . . . . . . . . . . . . . . 15Using the PHP Manual . . . . . . . . . . . . . . . . . . . . 18Sending HTML to the Browser . . . . . . . . . . . . . . . 21Adding Comments to Scripts . . . . . . . . . . . . . . . . 24Basic Debugging Steps . . . . . . . . . . . . . . . . . . . 27Review and Pursue . . . . . . . . . . . . . . . . . . . . . 29Chapter 2Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31What Are Variables? . . . . . . . . . . . . . . . . . . . . . 32Variable Syntax . . . . . . . . . . . . . . . . . . . . . . . 36Types of Variables . . . . . . . . . . . . . . . . . . . . . . 38Variable Values . . . . . . . . . . . . . . . . . . . . . . . . 41Understanding Quotation Marks . . . . . . . . . . . . . 44Review and Pursue . . . . . . . . . . . . . . . . . . . . . 48Chapter 3HTML Forms and PHP . . . . . . . . . . . . . . . . . . . 49Creating a Simple Form . . . . . . . . . . . . . . . . . . . 50Choosing a Form Method . . . . . . . . . . . . . . . . . . 54Receiving Form Data in PHP . . . . . . . . . . . . . . . . 58Displaying Errors . . . . . . . . . . . . . . . . . . . . . . 63Error Reporting . . . . . . . . . . . . . . . . . . . . . . . 65Manually Sending Data to a Page . . . . . . . . . . . . . 68Review and Pursue . . . . . . . . . . . . . . . . . . . . . .73Table of Contentsv

Chapter 4Using Numbers . . . . . . . . . . . . . . . . . . . . . . . 75Creating the Form . . . . . . . . . . . . . . . . . . . . . .Performing Arithmetic . . . . . . . . . . . . . . . . . . . .Formatting Numbers . . . . . . . . . . . . . . . . . . . .Understanding Precedence . . . . . . . . . . . . . . . .Incrementing and Decrementing a Number . . . . . . .Review and Pursue . . . . . . . . . . . . . . . . . . . . .Chapter 5767983868892Using Strings . . . . . . . . . . . . . . . . . . . . . . . . . 93Creating the HTML Form . . . . . . . . . . . . . . . . . . 94Concatenating Strings . . . . . . . . . . . . . . . . . . . .97Handling Newlines . . . . . . . . . . . . . . . . . . . . . 101HTML and PHP . . . . . . . . . . . . . . . . . . . . . . . 104Encoding and Decoding Strings . . . . . . . . . . . . . 108Finding Substrings . . . . . . . . . . . . . . . . . . . . . 113Replacing Parts of a String . . . . . . . . . . . . . . . . . 117Review and Pursue . . . . . . . . . . . . . . . . . . . . . 120Chapter 6Control Structures . . . . . . . . . . . . . . . . . . . . . 121Creating the HTML Form . . . . . . . . . . . . . . . . . . 122The if Conditional . . . . . . . . . . . . . . . . . . . . . . 125Validation Functions . . . . . . . . . . . . . . . . . . . . 128Using else . . . . . . . . . . . . . . . . . . . . . . . . . . 132More Operators . . . . . . . . . . . . . . . . . . . . . . . 135Using elseif . . . . . . . . . . . . . . . . . . . . . . . . . 144The Switch Conditional . . . . . . . . . . . . . . . . . . . 148The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . 152Review and Pursue . . . . . . . . . . . . . . . . . . . . . 157Chapter 7Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . 159What Is an Array? . . . . . . . . . . . . . . . . . . . . . . 160Creating an Array . . . . . . . . . . . . . . . . . . . . . . 162Adding Items to an Array . . . . . . . . . . . . . . . . . . 166Accessing Array Elements . . . . . . . . . . . . . . . . . 170Creating Multidimensional Arrays . . . . . . . . . . . . . 173Sorting Arrays . . . . . . . . . . . . . . . . . . . . . . . . 178Transforming Between Strings and Arrays . . . . . . . . 182Creating an Array from a Form . . . . . . . . . . . . . . 186Review and Pursue . . . . . . . . . . . . . . . . . . . . . 191vi Table of Contents

Chapter 8Creating Web Applications . . . . . . . . . . . . . . . 193Creating Templates . . . . . . . . . . . . . . . . . . . . . 194Using External Files . . . . . . . . . . . . . . . . . . . . 201Using Constants . . . . . . . . . . . . . . . . . . . . . . 207Working with the Date and Time . . . . . . . . . . . . . . 211Handling HTML Forms with PHP, Revisited . . . . . . . 214Making Forms Sticky . . . . . . . . . . . . . . . . . . . .220Sending Email . . . . . . . . . . . . . . . . . . . . . . . . 228Output Buffering . . . . . . . . . . . . . . . . . . . . . . 233Manipulating HTTP Headers . . . . . . . . . . . . . . . 237Review and Pursue . . . . . . . . . . . . . . . . . . . . . 241Chapter 9Cookies and Sessions . . . . . . . . . . . . . . . . . . 243What Are Cookies? . . . . . . . . . . . . . . . . . . . . . 244Creating Cookies . . . . . . . . . . . . . . . . . . . . . . 246Reading from Cookies . . . . . . . . . . . . . . . . . . . 251Adding Parameters to a Cookie . . . . . . . . . . . . . . 254Deleting a Cookie . . . . . . . . . . . . . . . . . . . . . . 257What Are Sessions? . . . . . . . . . . . . . . . . . . . . .260Creating a Session . . . . . . . . . . . . . . . . . . . . . 261Accessing Session Variables . . . . . . . . . . . . . . . 264Deleting a Session . . . . . . . . . . . . . . . . . . . . . 266Review and Pursue . . . . . . . . . . . . . . . . . . . . . 268Chapter 10Creating Functions . . . . . . . . . . . . . . . . . . . . 269Creating and Using Simple Functions . . . . . . . . . . 270Creating and Calling Functions That Take Arguments . 276Setting Default Argument Values . . . . . . . . . . . . . 282Creating and Using Functions That Return a Value . . . 285Understanding Variable Scope . . . . . . . . . . . . . .290Review and Pursue . . . . . . . . . . . . . . . . . . . . .296Chapter 11Files and Directories . . . . . . . . . . . . . . . . . . . 297File Permissions . . . . . . . . . . . . . . . . . . . . . . . 298Writing to Files . . . . . . . . . . . . . . . . . . . . . . . 303Locking Files . . . . . . . . . . . . . . . . . . . . . . . . 310Reading from Files . . . . . . . . . . . . . . . . . . . . . 313Handling File Uploads . . . . . . . . . . . . . . . . . . . 316Navigating Directories . . . . . . . . . . . . . . . . . . . 325Table of Contentsvii

Creating Directories . . . . . . . . . . . . . . . . . . . . 330Reading Files Incrementally . . . . . . . . . . . . . . . . 338Review and Pursue . . . . . . . . . . . . . . . . . . . . . 343Chapter 12Intro to Databases . . . . . . . . . . . . . . . . . . . . 345Introduction to SQL . . . . . . . . . . . . . . . . . . . . . 346Connecting to MySQL . . . . . . . . . . . . . . . . . . . 348MySQL Error Handling . . . . . . . . . . . . . . . . . . . 352Creating a Table . . . . . . . . . . . . . . . . . . . . . . . 355Inserting Data into a Database . . . . . . . . . . . . . . 360Securing Query Data . . . . . . . . . . . . . . . . . . . . 366Retrieving Data from a Database . . . . . . . . . . . . . 371Deleting Data in a Database . . . . . . . . . . . . . . . . 376Updating Data in a Database . . . . . . . . . . . . . . . 382Review and Pursue . . . . . . . . . . . . . . . . . . . . . 388Chapter 13Putting It All Together . . . . . . . . . . . . . . . . . 389Getting Started . . . . . . . . . . . . . . . . . . . . . . .390Connecting to the Database . . . . . . . . . . . . . . . . 392Writing the User-Defined Function . . . . . . . . . . . . 393Creating the Template . . . . . . . . . . . . . . . . . . . 396Logging In . . . . . . . . . . . . . . . . . . . . . . . . . .400Logging Out . . . . . . . . . . . . . . . . . . . . . . . . . 404Adding Quotes . . . . . . . . . . . . . . . . . . . . . . . 405Listing Quotes . . . . . . . . . . . . . . . . . . . . . . . .409Editing Quotes . . . . . . . . . . . . . . . . . . . . . . . 412Deleting Quotes . . . . . . . . . . . . . . . . . . . . . . 418Creating the Home Page . . . . . . . . . . . . . . . . . . 422Review and Pursue . . . . . . . . . . . . . . . . . . . . . 426Appendix A Installation and Configuration . . . . . . . . . . . . 427Appendix B Resources and Next Steps . . . . . . . . . . . . . . . 449Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459viii Table of Contents

IntroductionWhen I began the first edition of this bookin 2000, PHP was a little-known opensource project. It was adored by technicalpeople in the know but not yet recognizedas the popular choice for web developmentthat it is today. When I taught myself PHP,very little documentation was available onthe language—and that was my motivationfor writing this book in the first place.Today things are different. The Internethas gone through a boom and a bust andhas righted itself. Furthermore, PHP is nowthe reigning king of dynamic web designtools and has expanded somewhat beyondthe realm of just web development. Butdespite PHP’s popularity and the increasein available documentation, sample code,and examples, a good book discussing thelanguage is still relevant. Although PHP isin the beginnings of its sixth major release,a book such as this—which teaches thelanguage in simple but practical terms—can still be your best guide in learning theinformation you need to know.This book will teach you PHP, providingboth a solid understanding of the fundamentals and a sense of where to look formore advanced information. Although itisn’t a comprehensive programming reference, this book, through demonstrationsand real-world examples, provides theknowledge you need to begin buildingdynamic websites and web applicationsusing PHP.What Is PHP?PHP originally stood for Personal HomePage. It was created in 1994 by RasmusLerdorf to track the visitors to his onlinerésumé. As its usefulness and capabilitiesgrew (and as it began to be utilized in moreprofessional situations), PHP came to meanPHP: Hypertext Preprocessor. The definition basically means that PHP handles databefore it becomes HTML—which stands forHypertext Markup Language.

According to the official PHP website,found at www.php.net A, PHP is “a popular general-purpose scripting languagethat is especially suited to web development.” More specifically, PHP is a scriptinglanguage commonly embedded withinHTML. Let’s examine what this means inmore detail.To say that PHP can be embedded intoHTML means that PHP code can be writtenwithin your HTML code—HTML being thelanguage with which all web pages arebuilt. Therefore, programming with PHPstarts off as only slightly more complicatedthan hand-coding HTML.Also, PHP is a scripting language, asopposed to a compiled language. Thismeans that PHP is designed to do something only after an event occurs—forexample, when a user submits a form orgoes to a URL (Uniform Resource Locator—the technical term for a web address).Another popular example of a scriptinglanguage is JavaScript, which commonlyhandles events that occur within thebrowser. Both PHP and JavaScript can alsobe described as interpreted, because thecode must be run through an executable,such as the PHP module or the browser’sJavaScript component. Conversely, compiled languages such as C and C can beused to write stand-alone applications thatcan act independently of any event.A As of this writing, this is the appearance ofthe official PHP website, located at www.php.net.Naturally, this should be the first place you lookto address most of your PHP questions andcuriosities.PHP 6?Yes, as of this writing, the current versions of PHP were 5 and 7, but not 6!There’s a long and amusing story here,but the short version is that PHP 6 wasactively developed for a while. After hitting many snags, the development washalted and the created work was rolledinto PHP 5.When it became time to work on thenext major version, after much debateit was decided that that version wouldbe named PHP 7. So although there wasonce a beta version of PHP 6, no finalrelease ever saw the light of day.xChapter

You should also understand that PHP is aserver-side technology. This refers to thefact that everything PHP does occurs onthe server (as opposed to on the client,which is the computer being used by theperson viewing the website). A serveris just a computer set up to provide thepages you see when you go to a webaddress with your browser. I’ll discuss thisprocess in more detail later in this introduction (see “How PHP Works”).B This Zend website contains useful software aswell as a code gallery and well-written tutorials.What PHP Is NotThe thing about PHP that confusesmost new learners is what PHP can’t do.Although you can use the language foran amazing array of tasks, its main limitation is that PHP cannot be used for clientside features found in some websites.Using a client-side technology likeJavaScript, you can create a newbrowser window, make pop-up dialogs,dynamically generate and alter forms,and much more. None of these tasks canbe accomplished using PHP becausePHP is server-side, whereas those areclient-side issues. But you can use PHPto create JavaScript, just as you can usePHP to create HTML.When it comes time to develop your ownPHP projects, remember that you canuse PHP only to send information (HTMLand such) to the browser. You can’t doanything else within the browser untilanother request from the server hasbeen made (a form has been submittedor a link has been clicked).Finally, PHP is cross-platform, meaningthat it can be used on machines runningUnix, Windows, Macintosh, and other operating systems. Again, we’re talking aboutthe server’s operating system, not the client’s

PHP for the Web Fifth Edition LARRY ULLMAN. Visual QuickStart Guide PHP for the Web, Fifth Edition . PHP originally stood for Personal Home Page. It was created in 1994 by Rasmus Lerdorf to track the visitors to his online . You should also understand that PHP is a server-side technology. This refers

Related Documents:

May 02, 2018 · D. Program Evaluation ͟The organization has provided a description of the framework for how each program will be evaluated. The framework should include all the elements below: ͟The evaluation methods are cost-effective for the organization ͟Quantitative and qualitative data is being collected (at Basics tier, data collection must have begun)

Silat is a combative art of self-defense and survival rooted from Matay archipelago. It was traced at thé early of Langkasuka Kingdom (2nd century CE) till thé reign of Melaka (Malaysia) Sultanate era (13th century). Silat has now evolved to become part of social culture and tradition with thé appearance of a fine physical and spiritual .

PHP is FREE to download from the official PHP resource: www.php.net PHP is easy to learn and runs efficiently on the server side Where to Start? To get access to a web server with PHP support, you can: Install Apache (or IIS) on your own server, install PHP, and MySQL Or find a web hosting plan with PHP and

Dr. Sunita Bharatwal** Dr. Pawan Garga*** Abstract Customer satisfaction is derived from thè functionalities and values, a product or Service can provide. The current study aims to segregate thè dimensions of ordine Service quality and gather insights on its impact on web shopping. The trends of purchases have

PHP is an acronym for "PHP: Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use What is a PHP File? PHP files can contain text, HTML, CSS, JavaScript, and PHP code PHP code are executed on the server, and the result is returned to the browser .

php architect's Guide to PHP Design Patterns A Practical Approach to Design Patterns for the PHP 4 and PHP 5 Developer Jason E. Sweat USA 21.99 Canada 29.99 U.K. 16.99 Net php architect's Guide to PHP Design Patterns Design patterns are comprehensive, well-tested solutions to common problems that developers everywhere encounter each day.

On an exceptional basis, Member States may request UNESCO to provide thé candidates with access to thé platform so they can complète thé form by themselves. Thèse requests must be addressed to esd rize unesco. or by 15 A ril 2021 UNESCO will provide thé nomineewith accessto thé platform via their émail address.

̶The leading indicator of employee engagement is based on the quality of the relationship between employee and supervisor Empower your managers! ̶Help them understand the impact on the organization ̶Share important changes, plan options, tasks, and deadlines ̶Provide key messages and talking points ̶Prepare them to answer employee questions