Alan Forbes - The Joy of PHP 1
The Joy of Programming PHP Copyright 2013 by Alan Forbes. All rights reserved, including the right to reproduce this book, or portions thereof, in any form. The scanning, hosting, downloading, or otherwise sharing this book over the Internet without the permission of the author is illegal and punishable by law and not very nice. Please encourage me to write another book by buying your own copy. This ebook is for your personal use only. This ebook may not be re-sold or given away to other people. If you would like to share this book with another person, please purchase an additional copy for each person you share it with. If you are reading this book and did not purchase it, or it was not purchased for your exclusive use only, then you should purchase your own copy. Thank you for respecting the hard work of the author who put hundreds of hours of effort into making it all look easy. Alan Forbes - The Joy of PHP 3
Table of Contents Introduction...10 Introduction...10 Praise for The Joy of PHP...12 Foreword...13 Sponsors...14 CWS Technology...14 Site5...14 Matraex...14 mindworks GmbH...15 Other Kickstarter Backers of Note...15 What is PHP?...16 Introducing PHP...17 Example...18 A little history...19 Installing and Configuring PHP...21 Introduction...21 Windows Users...21 Using WampServer...22 Mac Users...23 How Do I Know it is Working?...23 Oracle VM Virtual Box...25 Installing for a Web Site...25 Exercise...26 Introduction to HTML...27 Introduction...27 Basic HTML...27 Basic Elements of HTML...28 Tags...28 Nested Tags...29 Required tags...29 Alan Forbes - The Joy of PHP 4
DocType...29 Head...30 Optional Tags...30 Meta Tags...30 Useful Tags...30 Headers...30 DIV...31 Images/Picture...31 Links...31 Lists...32 Exercise...32 HTML Tables...32 Table Example...33 HTML Table Headers...33 HTML Forms...34 The Input tag...35 HTML Form Actions & Methods...36 Basic PHP Syntax...38 Introduction - Basic PHP Syntax...38 Your first PHP-enabled page Hello World!...38 Some Fun Right Away...40 A Countdown Counter...40 Exercise...42 Editors and Staying Organized...43 Editors...43 Microsoft Expression Web...43 phpstorm...44 EditRocket...45 Free Editors...45 Includes...45 Basic example...46 HTML example...46 Code example...47 Alan Forbes - The Joy of PHP 5
Best Practices...47 Variables, Numbers, Dates, and Strings...48 Variables...48 Variable Naming Conventions and Best Practices...49 Numbers...50 Basic Arithmetic...50 Common Arithmetic Shortcuts...51 Useful Numeric Functions...51 Strings...51 Useful String Functions...52 Sources of Documentation...52 Exercise...52 How to Interpret PHP.NET documentation...52 Return Value...54 Function Name...54 Parameters...54 Dates...55 Example: Calculating Age in Years...57 Variable Scope...58 Control Structures...59 Introduction...59 if...59 if... else...60 Exercise: Open Hours...60 PHP Switch...61 PHP Looping...61 PHP while...61 PHP for...62 How to use a database, such as mysql...64 Introduction...64 What are Databases?...64 Getting Started with phpmyadmin...66 What is phpmyadmin?...67 Alan Forbes - The Joy of PHP 6
Using phpmyadmin to create a database...67 Introduction to SQL...68 Using phpmyadmin to create a Table in a database...68 Defining our first table...70 What defines an automobile?...70 Datatypes...71 Numbers...71 Characters...71 Dates...71 Exercise: Create a Table...74 Working with SQL Statements...75 INSERT Statements...75 SELECT Statements...76 WHERE Statements...78 Comparison Operators...80 ORDER BY...80 UPDATE Statements...81 DELETE Statements...81 Using mysql and PHP Together...83 Introduction...83 Code!...83 Code Listing: createdb.php...84 Code Explained: createdb.php...87 Hey, where's the HTML?...90 Creating forms to Display, Add, Edit, and Delete data...91 Introduction...91 Forms that Add Data to a Database...91 A Basic Form...91 HTML Code...92 Form Action...92 PHP Code...93 A Brief Time Out...include files...97 Forms that Display Summary Data...99 Code...100 Alan Forbes - The Joy of PHP 7
Code Explained...101 Exercise: Tweaking the SELECT...103 Improving the look of the table with CSS...104 CSS Explained...105 Modifying the form to link to the detail page...106 Forms that Display Detail Data...107 Code...108 Code Explained...109 Forms that Edit Data...110 Forms that Delete Data...110 Code to delete data...111 Code Explained...111 Exercise...112 Session Variables...113 Introduction...113 Sessions...114 Starting a PHP Session...115 Using Session Variables...115 Store a variable...115 Retrieve a variable...115 Checking for a variable...115 Destroying a Session...116 Working with Images...117 Introduction...117 Exercise: Viewing Images...119 Pulling an unknown number of images from a database...119 Exercise: Create a Database Table to store images...119 Exercise: Modify the viewcar.php page to show multiple images...120 Code explained...120 PHP File Uploads...122 Introduction...122 Create an Upload File form...122 Create a Script to Process the Uploaded File...123 Alan Forbes - The Joy of PHP 8
Code: ViewCarsAddImage.php...124 Code Explained...125 PHP Quirks and Tips...129 Introduction...129 Single Quotes vs Double Quotes...129 The Equal Sign...130 Comparison Operators...132 Security Considerations...133 Introduction...133 Balancing Security and Usability...133 SQL Injection...134 Additional Resources...136 Appendix A: Installing PHP on a Website...137 How to install on a Windows Server...137 How to install on a Linux Server...137 Alan Forbes - The Joy of PHP 9