Steve Prettyman

Learn PHP 8

Using MySQL, JavaScript, CSS3, and HTML5

2nd ed.
Steve Prettyman
Key West, FL, USA
ISBN 978-1-4842-6239-9e-ISBN 978-1-4842-6240-5
© Steve Prettyman 2020
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

This edition is dedicated to all essential workers throughout the world who have helped provide a more safe world from illness (pandemics) and other natural disasters. Your dedication makes the world a better place. You support the Key West, Florida (USA), motto of “One Human Family.”

Introduction

Learn PHP 8: Using MySQL, JavaScript, CSS3, and HTML5 is intended for use as a beginner- and intermediate-level programming book. It is not the goal of this book to cover advanced techniques in the current versions of the PHP programming language. Some knowledge of general programming concepts is expected, but no actual programming experience or education is assumed.

All code examples in this book are compatible with PHP 8. Most examples are compatible with PHP 7. The newest (as of the publication date) methods (functions) available in PHP have been used to provide the reader with the most current coding techniques. The examples use core methods provided in the PHP language. PHP includes many additional methods to accomplish similar tasks. The reader may, and should, research additional ways of improving security, performance, and other techniques. It is the goal of this book to prompt users to always consider using the most secure and efficient methods of program development. The code in this book provides some examples of using these techniques. The user should remember that no program is 100% secure. The programmer can only strive to make an application as secure as possible. It takes a team of developers, network personnel, security administrators, data center personnel, and others working together to provide the safest environment.

A Different Approach

There are quite a number of PHP books on the market today. What makes this book any different than others?
  • This book uses the concept of “learning by doing,” which shows the reader how to develop applications with conditional statements, loops, arrays, and methods. Over 70 PHP methods (functions) are introduced and demonstrated in coding examples.

  • Very early in the book, the reader is introduced to object-oriented (OO) programming techniques. Many other books only briefly cover OO programming (if at all) in the final chapters.

  • Object-oriented set methods are used to verify and filter user input. Many other books simply show a set method accepting data and storing it.

  • A major objective of the book is to convince the reader to create all programs as secure and efficient as possible. The newest password encryption techniques (password_hash) are demonstrated.

  • The try and catch methods are introduced to capture exceptions and some errors. The newest versions of PHP have been created to handle exceptions and errors using this approach.

  • Multitier program design is introduced in the early chapters. This allows the reader to discover what logic and coding should take place in each tier. Many PHP books do not even cover this topic.

  • The majority of the examples in the book are used to develop one main application (ABC Canine Shelter Reservation System). As the book progresses, the application is built from the beginning, in stages, showing the reader that application development should be broken into stages. Only after each stage is completed and tested can the next stage begin. This approach works hand in hand with multitier design. Additional programming exercises and a term project are provided to enhance the understanding of development.

  • The creation of user, change, and error logs are introduced. This allows the reader to gain an understanding of how to provide backup and recovery ability to keep an application functioning properly when security breaches or exceptions occur.

  • The introduction of data objects and the data tier demonstrates to the reader the importance of creating an application that provides the ability to change data storage techniques and data storage location without requiring a major rewrite of the application. XML, JSON, and MySQL examples are provided.

  • A natural relationship between PHP, HTML5, CSS3, and JavaScript is demonstrated throughout the book. This relationship is one of the major strengths of PHP.

  • Throughout the book, web links are provided to point the user to additional resources to help understand the material or to dig deeper into the subject matter. Updates to link locations are provided on the book’s web site.

Special Note for Teachers

The design of the content of this book provides flexibility in teaching styles and approaches. Each college and university approaches the initial education of programming concepts in different ways. This book provides three different types of programming exercises, which allow teachers to pick and choose what would work best in their environment. “Do It” exercises are provided in each chapter to allow the student to gain hands-on experience with techniques shown by modifying existing examples to produce the desired results. These exercises provide a level of confidence before the student attempts to program exercises at the end of the chapters. In addition, a term project is provided that builds an application that uses the same types of algorithms and programming techniques shown in the book.

Code Examples, Images, and Links

Every effort has been made to catch any errors in code (and grammar). Please let us know if/when you discover problems in this book. Please send all corrections to Steve Prettyman ([email protected]).

All code examples, images, and links are available via the Download Source Code link on the book’s product page, located at www.apress.com/9781484262399 and the following location. Copying code from the book may cause errors due to format requirements for publishing.

Chapter Overview

Chapter 1: An Introduction to PHP 8

After completing this chapter, the student will be able to
  • Understand the difference between LAMP, WAMP, and MAMP

  • Successfully install a version of LAMP, WAMP, or MAMP

  • Search the Internet for troubleshooting problems

  • Explain the difference between a programming language and a scripting language

  • Create an error-free simple PHP program

Chapter 2: Interfaces, Platforms, and Three-Tier Programming

After completing this chapter, the student will be able to
  • Give examples of platforms or containers that can host PHP programs

  • Create a simple, dynamic web application using PHP

  • Explain three-tier design and determine what is contained in each tier

  • Design a three-tier application

  • Explain each step of the program development life cycle (PDLC)

  • Define and explain MVC and dependency injection

Chapter 3: The Basics: PHP 8 Syntax

After completing this chapter, the student will be able to
  • Create a simple, error-free PHP program

  • Understand the use and value of conditional statements

  • Understand the use and value of for, while, and foreach loops

  • Understand the use and value of functions

  • Understand the use and value of arrays

Chapter 4: Modular Programming

After completing this chapter, the student will be able to
  • Create an error-free, simple objected-oriented (OO) modular PHP program

  • Create a PHP class and make an instance of the class (object)

  • Create an OO PHP encapsulated program, including get and set methods

  • Create PHP methods (functions) that accept parameters and return information

  • Create PHP public and private properties (variables)

  • Import existing PHP code from another file or library into a program

  • Validate information received using ternary (conditional) operators

Chapter 5: Secured User Interfaces

After completing this chapter, the student will be able to
  • Explain why user input must be validated in the interface and business rules tiers

  • Explain why user input must be filtered in the business rules tier

  • Use HTML5 code to validate user input

  • Use JavaScript code to validate user input

  • Use PHP if statements (conditional statements) to validate and filter input

  • Use the foreach loops to dynamically create an HTML select box from an XML file

  • Use simple arrays for filtering and validation

  • Pass simple arrays into methods (functions)

  • Understand how to use dependency injection to control code version changes

Chapter 6: Handling and Logging Exceptions

After completing this chapter, the student will be able to
  • Explain the difference between errors and exceptions

  • Create a PHP program that can handle general exceptions

  • Create a PHP program that can create, raise, and handle user exceptions

  • Explain and use a switch and/or embedded if/else statement

  • Create a PHP program that uses the while loop and/or the for loop

  • Create a program that reads/updates a text file using a two-dimensional array

  • Create a PHP program that logs exceptions and emails support personnel

Chapter 7: Data Objects

After completing this chapter, the student will be able to
  • Create a data class that inserts, updates, and deletes XML or JSON data

  • Explain how to create a data class that updates MySQL Data using a SQL Script

  • Create a PHP program that creates a change backup log

  • Create a PHP program that can recover data from a previous backup

  • Apply changes to create up-to-date valid information

  • Use dependency injection to attach a data class to another class in the BR tier

  • Create a three-tier PHP application

Chapter 8: Authentication

After completing this chapter, the student will be able to
  • Define sessions and explain how they are used for authentication

  • Create a PHP program that authenticates user logon

  • Create a PHP program that registers users

  • Create a PHP program that will allow users to change their passwords

  • Create a PHP program that logs invalid login attempts

Chapter 9: Multifunctional Interfaces

After completing this chapter, the student will be able to
  • Create a complete PHP application that deletes, updates, and inserts data

  • Create a professional look to a completed application using CSS

  • Use JavaScript to accept and manipulate data from another program

  • Secure all programs within an application requiring user IDs/passwords

  • Populate HTML objects with values from a JSON object

Acknowledgments

Thank you to everyone who has helped put this book together. Special thanks to the Introduction to PHP classes that have been the true testers and debuggers for this journey.

Special acknowledgment to all the open source developers and providers of free tutorials and training to every Internet user who wants to learn more about programming.

Table of Contents
Index 419
About the Author
Steve Prettyman

earned his bachelor of arts degree in education from Oglethorpe University in 1979. He quickly began his teaching career as a high-school mathematics instructor while continuing his education by earning a master’s degree in business information systems from Georgia State University (1985). Since then, Steve has spent over 30 years in the IT industry. He has also been a professor at Chattahoochee Technical College, Kennesaw State University, and Southern Polytechnic State University for over 25 years. His primary teaching responsibilities include programming, web design, and web application development. Steve, his wife Beverly, and their two dogs (Pixee and Buster) currently reside in Paradise (Key West, Florida).

 
About the Technical Reviewer
Satej Kumar Sahu

works in the role of Senior Enterprise Architect at Honeywell. He is passionate about technology, people, and nature. He believes through technology and conscientious decision making, each of us has the power to make this world a better place. In his free time, he can be seen reading books, playing basketball, and having fun with friends and family.

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.196.211