PHP
Advanced Detailed Approach to Master PHP Programming Language for Web Development
Table of Contents
PHP
Table of Contents
Introduction
Chapter 1: Styles of Coding in PHP
Chapter 2: Object-Oriented Programming using Design Patterns
Chapter 3: Handling Errors
Chapter 4: Running PHP-Templates and the Web
Chapter 5: PHP Standalone Scripts
Chapter 6: Controlling the Development Environment
Chapter 7: Designing a Great API
Conclusion
Description
Copyright 2019 by _____MG Martin_______ - All rights reserved.
The following eBook is reproduced below with the goal of providing information that is as accurate and reliable as possible. Regardless, purchasing this eBook can be seen as consent to the fact that both the publisher and the author of this book are in no way experts on the topics discussed within and that any recommendations or suggestions that are made herein are for entertainment purposes only. Professionals should be consulted as needed prior to undertaking any of the actions endorsed herein.
This declaration is deemed fair and valid by both the American Bar Association and the Committee of Publishers Association and is legally binding throughout the United States.
Furthermore, the transmission, duplication, or reproduction of any of the following work including specific information will be considered an illegal act irrespective of whether it is done electronically or in print. This extends to creating a secondary or tertiary copy of the work or a recorded copy and is only allowed with the express written consent from the Publisher. All additional rights reserved.
The information in the following pages is broadly considered to be a truthful and accurate account of facts, and as such any inattention, use or misuse of the information in question by the reader will render any resulting actions solely under their purview. There are no scenarios in which the publisher or the original author of this work can be, in any fashion, deemed liable for any hardship or damages that may befall them after undertaking information described herein.
Additionally, the information in the following pages is intended only for informational purposes and should thus be thought of as universal. As befitting its nature, it is presented without assurance regarding its prolonged validity or interim quality. Trademarks are mentioned without written consent and can in no way be considered an endorsement from the trademark holder.
Introduction
This book aims to make you an expert in PHP. Being an expert in programming does not mean that you have mastered the syntax and properties of the language. It simply means you can use the language to create solutions to problems. Once you finish reading this book, you should have a good ground to expand your knowledge into deeper PHP concepts. Additionally, you should be in a better position to handle problems both inside and outside the web domain.
This book strives to be concept-focused by reviewing the general problems and applying examples as a demonstration. The purpose of this book is to present you the tools to use to solve any problem and the knowledge to select the right tool for the job.
It is easier to learn by example, and this book provides you practical examples that implement all the concepts that it discusses.
PHP is not a glue language that is applied by a small-time enthusiast. Instead, it has become a powerful scripting language whose design is perfect for solving problems in the web domain.
Chapter 1: Styles of Coding in PHP
Regardless of your PHP proficiency, regardless of how far you have mastered the language internals or the syntaxes, it is easy to create a sloppy code. A hard-to-read code is difficult to manage and debug. A poor coding style demonstrates a lack of professionalism.
If you were to remain at work for the entire period of your life, and no one else had to manage your code, it will still not be okay to create a poorly structured code. It is difficult to troubleshoot or augment libraries that you wrote two or even three years ago. It will always take time to understand the logic.
To worsen the situation, no one codes in a vacuum. The codes require to be maintained by current and future peers. The merging of two independently readable styles can be unreadable and left without maintenance such that there is no style guide at all. As a result, it is important that we apply a style that anyone can understand, and the style should be consistent across all the developers collaborating.
Picking a style that is right for you
When it comes to choosing a style to code, you need to consider it carefully. Keep in mind that the code lives past us, and changing a style later is more challenging than it is worth. Code that includes different styles with every new developer can quickly turn into a mess.
The same way it is important to select a new style in a project that has none, you also need to beware of other standards. Remember, there is no perfect standard. The preference of someone determines a coding style. Much more important than selecting the perfect style is picking a consistent style across all your code. You should not be too quick to change a consistent style you do not like.
The code format and layout
This includes indentation, the use of whitespace, line length, and application of Structured Query Language (SQL), which is the most basic tool you can apply to cement the logical structure of your code.
Indentation
This book will apply indentation to arrange the code and represent blocks of code. The significance of indentation for code organization cannot be exaggerated. Most programmers take it seriously that the Python Scripting language applies indentation as the syntax; if the Python code isnt indented correctly, the program will not parse.
While in PHP, indentation is not a prerequisite; it is still a great visual organization tool that you need to use in your code always.
See the following code:
Now, compare that with this block of code that is identical, apart from the indentation.
In the last version, it is easier to differentiate the flow of logic than in the first version.
When you use tabs to indent your code, you need to make a firm decision whether the tabs should be soft or hard. Hard tabs refer to regular tabs. On the other hand, soft tabs arent tabs at all. Each soft tab is defined with a specific number of regular spaces. The advantage of applying soft tabs is that they always appear similar no matter what the setting of the editors tab-spacing is. Most people prefer to use soft tabs. When the soft tabs are set, it is easy to ensure consistency in indentation and whitespace application in the entire code. When you enforce hard tabs, especially when multiple developers are working on different editors, it is easy for different levels of indentation to be created.
You also need to select the tab width that you want to apply. A tab width of four spaces displays a code that is easy to read and offers a reasonable amount of nesting. Since book pages are a bit smaller than terminal windows, you should apply two space tab-widths.