Book Image

Learning PHP 7 High Performance

Book Image

Learning PHP 7 High Performance

Overview of this book

PHP is a great language for building web applications. It is essentially a server-side scripting language that is also used for general-purpose programming. PHP 7 is the latest version, providing major backward-compatibility breaks and focusing on high performance and speed. This fast-paced introduction to PHP 7 will improve your productivity and coding skills. The concepts covered will allow you, as a PHP programmer, to improve the performance standards of your applications. We will introduce you to the new features in PHP 7 and then will run through the concepts of object-oriented programming (OOP) in PHP 7. Next, we will shed some light on how to improve your PHP 7 applications' performance and database performance. Through this book, you will be able to improve the performance of your programs using the various benchmarking tools discussed. At the end, the book discusses some best practices in PHP programming to help you improve the quality of your code.
Table of Contents (17 chapters)
Learning PHP 7 High Performance
Credits
About the Author
Acknowledgement
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

The PHP community faced a huge problem over decades: performance. No matter how powerful hardware they got, in the end, PHP was a bottleneck in itself. With PHP 5.4.x, 5.5.x, and 5.6.x, PHP's performance started to improve, but still it was a huge problem in high-load applications. The community developed caching tools such as Alternative PHP Cache (APC) and Zend OpCache, which cached the opcode for high performance, and these tools had a good effect on the performance.

To get rid of the performance issues of PHP, Facebook built their own open source tool called HHVM (HipHop Virtual Machine). According to their official website, HHVM uses the Just In Time (JIT) compilation to achieve superior performance while maintaining the development flexibility that PHP provides. HHVM had great performance compared to PHP, and it is widely used in production for heavy applications such as Magento.

PHP went to war with HHVM using PHP Next Generation (PHPNG). The whole purpose of PHPNG is to increase performance and focus on the rewriting and optimization of the Zend engine memory allocation and PHP data types. People around the world started benchmarking PHPNG and HHVM, and according to them, PHPNG was outperforming HHVM.

At last, PHPNG was merged with the master branch of PHP, and after a tremendous amount of optimization and complete rewriting, PHP 7 was released with huge performance improvements. PHP 7 is still not JIT, but its performance is great and similar to HHVM. This is a huge performance increase from the older versions of PHP.

What this book covers

Chapter 1, Setting Up the Environment, covers how to set up different development environments, including the installation of NGINX, PHP 7, and Percona Server on Windows, different Linux distros, and setting up the Vagrant virtual machine for development purposes.

Chapter 2, New Features in PHP 7, covers the major new features introduced in PHP 7, including Type Hints, Group use Declarations, Anonymous classes, and new operators, such as Spaceship operator, Null Coalesce operators, and the Uniform variable syntax.

Chapter 3, Improving PHP 7 Application Performance, covers different techniques to increase and scale a PHP 7 application's performance. In this chapter, we cover optimization of NGINX and Apache, CDN, and CSS/JavaScript, such as merging and minifying them, full-page caching, and installing and configuring Varnish. At last, we discuss an ideal infrastructure setup for application development.

Chapter 4, Improving Database Performance, covers techniques to optimize MySQL and Percona Server configuration for high performance. Also, it covers different tools to monitor the performance of a database. It also covers Memcached and Redis for caching objects.

Chapter 5, Debugging and Profiling, covers debugging and profiling techniques including the use of Xdebug for debugging and profiling, debugging with Sublime Text 3 and Eclipse, and the PHP DebugBar.

Chapter 6, Stress/Load Testing PHP Applications, covers different tools to stress and load test the application. It covers Apache JMeter, ApacheBench, and Siege for load testing. It also covers how to load test different open source systems such as Magento, Drupal, and WordPress on PHP 7 and PHP 5.6, and compares their performance on PHP 7 and PHP 5.6.

Chapter 7, Best Practices in PHP Programming, covers a few best practices for producing quality standard code. It covers coding styles, design patterns, service-oriented architecture, test-driven development, Git, and deployments.

Appendix A, Tools to Make Life Easy, discusses three of these tools in much more detail. The tools we will discuss are Composer, Git, and Grunt watch.

Appendix B, MVC and Frameworks, covers MVC design patterns and the most popular frameworks that are used in PHP development, which include Laravel, Lumen, and Apigility.

What you need for this book

Any hardware specification that is compliant to run the latest versions of the following software should be enough to get through this book:

  • Operating systems: Debian or Ubuntu

  • Software: NGINX, PHP 7, MySQL, PerconaDB, Redis, Memcached, Xdebug, Apache JMeter, ApacheBench, Siege, and Git

Who this book is for

This book is for those who have basic experience in PHP programming. If you are developing performance-critical applications, then this book is for you.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

location ~ \.php$ {
  fastcgi_pass    127.0.0.1:9000;
  fastcgi_param    SCRIPT_FILENAME complete_path_webroot_folder$fastcgi_script_name;
  include    fastcgi_params;
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

server {
  …
  …
  root html;
  index index.php index.html index.htm;

Any command-line input or output is written as follows:

php-cgi –b 127.0.0.1:9000

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/LearningPHP7HighPerformance_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.