PHP 6/MySQL ® Programming for the Absolute Beginner Andy Harris Course Technology PTR A part of Cengage Learning Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States PHP 6/MySQL Programming for the © 2009 Course Technology, a part of Cengage Learning. Absolute Beginner: Andy Harris ALL RIGHTS RESERVED. No part of this work covered by the copyright Publisher and General Manager, Course herein may be reproduced, transmitted, stored, or used in any form or by Technology PTR: Stacy L. Hiquet any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, Associate Director of Marketing: information networks, or information storage and retrieval systems, except Sarah Panella as permitted under Section 107 or 108 of the 1976 United States Copyright Manager of Editorial Services: Act, without the prior written permission of the publisher. Heather Talbot Marketing Manager: Mark Hughes For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706 Acquisitions Editor: Mitzi Koontz For permission to use material from this text or product, submit all Project Editor: Jenny Davidson requests online at cengage.com/permissions Further permissions questions can be emailed to [email protected] Technical Reviewer: Matt Telles PTR Editorial Services Coordinator: PHP is a copyright of the PHP Group. MySQL is a registered trademark of Erin Johnson MySQL AB in the United States, the European Union and other countries. Interior Layout Tech: Value Chain Firefox and Maguma Open Studio are registered trademarks of the Mozilla Foundation. HTML Validator is a registered trademark of the Massachusetts Cover Designer: Mike Tanamachi Institute of Technology, European Research Consortium for Informatics and CD-ROM Producer: Brandon Penticuff Mathematics, or Keio University on behalf of the World Wide Web Consor- tium. Aptana is a registered trademark of Aptana, Inc. Indexer: Larry Sweazy All other trademarks are the property of their respective owners. Proofreader: Sara Gullion Library of Congress Control Number: 2008928831 ISBN-13: 978-1-59863-798-4 ISBN-10: 1-59863-798-3 eISBN-10: 1-59863-826-2 Course Technology 25 Thomson Place Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com Printed in Canada 1 2 3 4 5 6 7 11 10 09 To Heather, Elizabeth, Matthew, Jacob, and Benjamin, and to all those who have called me Teacher. A CKNOWLEDGMENTS F irst, I thank my Lord and Savior Jesus Christ. Heather, you always work harder on these books than I do. Thank you for your love and your support. Thank you Elizabeth, Matthew, Jacob, and Benjamin for understanding why Dad was typing all the time. Thanks to the Open Source community for creating great free software like PHP and MySQL. Thank you, Stacy Hiquet, for your continued support and encouragement on this and other projects. Thanks to Mitzi Koontz, for seeing this project through, and to Jenny Davidson for your work as the project editor. Special thanks to Matt Telles for the outstanding technical edit. You made the book far better than it would have been before. Thanks to all who worked on the previous two editions. Your hard work was the foundation for something that might be even better. Thank you to the many members of the Course Technology PTR team who worked on this book. A huge thanks goes to my students throughout the years and the many people who sent in comments and advice from the first two editions. Thank you for being patient with my manuscript, for helping me spot many errors, and for providing invaluable advice. I learned as much from you as you did from me. A A BOUT THE UTHOR A ndy Harris began his teaching career as a high-school special education teacher. During that time, he taught himself enough computing to do part- time computer consulting and database work. He began teaching comput- ing at the university level in the late 1980s as a part-time job. Since 1995 he has been a full-time lecturer in the Computer Science Department of Indiana University/Purdue University–Indianapolis, where he serves as a senior lecturer and teaches classes in several programming languages. His primary interests are web development, PHP, Java, game programming, virtual reality, portable devices, and streaming media. He has written numerous books on these and other tech- nology topics. This page intentionally left blank Table of Contents Chapter 1 Exploring the Environment..................................... 1 Programming on the Web Server...................................................................................... 3 Installing PHP and Apache......................................................................................... 4 Using an Existing Server.............................................................................................. 4 Installing Your Own Development Environment................................................. 5 Installing with XAMPP................................................................................................. 7 Starting Your Server..................................................................................................... 8 Checking Your Security Settings............................................................................... 9 Configuring Apache.................................................................................................... 10 Running Your Local Server................................................................................................ 11 Adding PHP to Your Pages......................................................................................... 11 Examining the Results............................................................................................... 12 Display Errors............................................................................................................... 16 Windows Extensions.................................................................................................. 17 Changes in PHP 6......................................................................................................... 19 Safe Mode....................................................................................................................... 19 Register Globals........................................................................................................... 19 Creating the Tip of the Day Program............................................................................. 19 Summary................................................................................................................................ 20 Chapter 2 Using Variables and Input...................................... 21 Introducing the Story Program........................................................................................ 21 Using Variables in Your Scripts........................................................................................ 23 Introducing the Hi Jacob Program......................................................................... 23 Creating a String Variable................................................................................................. 25 Naming Your Variables.............................................................................................. 25 Assigning a Value to a Variable............................................................................... 25 Printing a Variable’s Value....................................................................................... 26 Using Variables for More Complex Pages...................................................................... 28 Building the Row Your Boat Page........................................................................... 28 Creating Multi-Line Strings...................................................................................... 30 Working with Numeric Variables.................................................................................... 31 Making the ThreePlusFive Program....................................................................... 31 viii PHP 6/MySQL Programming for the Absolute Beginner Assigning Numeric Values........................................................................................ 33 Using Mathematical Operators................................................................................ 33 Creating a Form to Ask a Question................................................................................. 34 Building an HTML Page with a Form..................................................................... 35 Setting the Action Attribute to a Script File........................................................ 36 Writing a Script to Retrieve the Data.................................................................... 37 Sending Data without a Form.......................................................................................... 39 Understanding the get Method............................................................................... 39 Using a URL to Embed Form Data........................................................................... 40 Working with Multiple Field Queries.................................................................... 42 Building a Pre-formatted Query.............................................................................. 42 Reading Input from Other Form Elements.......................................................... 43 Introducing the borderMaker Program......................................................................... 43 Building the borderMaker.html Page.................................................................... 44 Reading the Form Elements..................................................................................... 47 Reading Select Elements........................................................................................... 48 Reading Radio Groups................................................................................................ 49 Returning to the Story Program...................................................................................... 50 Designing the Story.................................................................................................... 50 Building the HTML Page............................................................................................ 52 Building the Story....................................................................................................... 54 Summary................................................................................................................................ 56 Chapter 3 Controlling Your Code with Conditions and Functions.................................................................. 57 Examining the Petals around the Rose Game.............................................................. 57 Creating a Random Number............................................................................................. 58 Viewing the Roll Em Program.................................................................................. 58 Printing a Corresponding Image............................................................................. 60 Using the if Statement to Control Program Flow....................................................... 61 Introducing the Ace Program.................................................................................. 61 Creating a Condition.................................................................................................. 63 Exploring Comparison Operators........................................................................... 64 Creating an if Statement........................................................................................... 65 Working with Negative Results....................................................................................... 66 Demonstrating the Ace or Not Program........................................................................ 66 Using the else Clause.................................................................................................. 68 Working with Multiple Values......................................................................................... 69 Writing the Binary Dice Program........................................................................... 69 Using Multiple else if Clauses.................................................................................. 71 Using the switch Structure to Simplify Programming.............................................. 72 Building the Switch Dice Program......................................................................... 73 Using the switch Structure....................................................................................... 74 Contents ix Combining a Form and Its Results.................................................................................. 75 Responding to Checkboxes................................................................................................ 78 Using Functions to Encapsulate Parts of the Program.............................................. 81 Examining the This Old Man Program.................................................................. 82 Creating New Functions............................................................................................ 84 Using Parameters and Function Values......................................................................... 84 Examining the Param.php Program...................................................................... 85 Looking at Encapsulation in the Main Code Body............................................. 87 Returning a Value: The chorus() Function............................................................ 88 Accepting a Parameter in the verse() Function................................................... 89 Managing Variable Scope................................................................................................... 90 Looking at the Scope Demo...................................................................................... 91 Returning to the Petals Game.......................................................................................... 93 Starting HTML.............................................................................................................. 94 Main Body Code........................................................................................................... 94 The printGreeting() Function................................................................................... 95 The printDice() Function........................................................................................... 96 The showDie() Function............................................................................................. 97 The calcNumPetals() Function................................................................................. 97 The printForm() Function.......................................................................................... 98 The Ending HTML Code............................................................................................. 99 Summary................................................................................................................................ 99 Chapter 4 Loops and Arrays................................................... 101 Introducing the Poker Dice Program........................................................................... 102 Counting with the for Loop............................................................................................ 103 Initializing a Sentry Variable................................................................................. 104 Setting a Condition to Finish the Loop............................................................... 105 Changing the Sentry Variable................................................................................ 105 Building the Loop...................................................................................................... 106 Modifying the for Loop............................................................................................ 106 Using a while Loop............................................................................................................ 110 Repeating Code with a while Loop....................................................................... 110 Recognizing Endless Loops..................................................................................... 111 Building a Well-Behaved Loop............................................................................... 113 Working with Basic Arrays.............................................................................................. 114 Generating a Basic Array......................................................................................... 116 Using a Loop to Examine an Array’s Contents.................................................. 117 Using the array() Construct to Preload an Array............................................... 117 Detecting the Size of an Array............................................................................... 118 Improving This Old Man with Arrays and Loops...................................................... 118 Building the place Array.......................................................................................... 120 Writing Out the Lyrics............................................................................................. 121
Description: