Everything You Need to Know about PHP 7

PHP 7 is the latest version of the popular programming language PHP. Released in December 2015, PHP 7 offers fast performance for websites and online applications. There are some significant differences between PHP 7 and the previous version of the language, PHP 5.6. Let’s take a look at the answers to some of the most common questions about PHP 7.

What About PHP 6?

Don’t worry, you did not miss the last version. In 2005, the PHP community started working on the development of PHP 6, but the project stalled. As the years went by, the reputation of PHP 6 deteriorated to the point that the community no longer wanted to continue using the name. Therefore, the version of the language that eventually replaced PHP 5.6 was given the name PHP 7.

What Drove the Need for PHP 7?

PHP is an extremely popular programming language. Created in 1994 by Rasmus Lerdorf, PHP has grown in popularity to the extent that today, nearly 82 percent of websites use PHP, which means that most of the Web relies on the language to at least some extent. With an increasing number of people coming online, including a rapidly growing number of mobile users who often rely on relatively slow 3G connections, it is necessary for the servers that power the world’s websites to be able to react quickly to user requests. Studies show that 40 percent of people will abandon a web page that takes more than three seconds to load, demonstrating that speed is a crucial factor in website design.

Significant changes to PHP have increased the performance of sites that use the language dramatically. In fact, it is estimated that PHP 7 offers a 100 percent improvement in performance speed over PHP 5.6. This major improvement in speed allows web developers to create sites that provide interesting and engaging interactive features that still respond to user input as quickly as modern web users have come to expect.

Another motivation for the development of PHP 7 is the need to develop scripting languages that run more efficiently. This demand is driven by two factors: the need to reduce costs and the need to reduce power consumption to protect the environment. Compared to PHP 5.6, PHP 7 places substantially reduced demands on servers, which makes it a more cost-effective and environmentally friendly choice, as comparatively less energy is needed to power servers running PHP 7 applications.

Who Spearheaded the Development of PHP 7?

The development of PHP 7 was spearheaded by Dmitry Stogov, Xinchen Hui, and Nikita Popov. These three developers created an experimental branch of PHP that they originally called PHP Next Generation, often abbreviated as PHPNG. The PHP community embraced this new branch of the scripting language, as it offered significant improvements in performance, and continued to develop it into the stable version of the language now known as PHP 7.

PHP 7 has been through several months of beta testing and was finally released in its stable form in December 2015, just one month after the predicted release date.

What Is New in PHP 7?

The headline change associated with PHP 7 is the significant improvement in performance. Roughly twice as fast as PHP 5.6, PHP 7 can compete with modern competitors to pure PHP, such as Facebook’s Hip Hop Virtual Machine (HHVM). In fact, for Drupal users, PHP 7 offers even faster performance than HHVM, with the added benefit of not needing to use a virtual machine to execute the PHP source code. Also, when PHP 7 runs on WordPress 4.1.1, it can execute twice as many requests per second as the same platform running PHP 5.6.

In addition to better performance, the following technical changes appear in PHP 7. Developers should familiarize themselves with these changes if they intend to switch over to using PHP 7 for their web development projects.

Return Types

Return types, a common feature in most other programming languages, have finally been introduced to PHP. This means that programmers can now specify the type of variable that a function should return. In PHP 7, the return type is specified after the closing parenthesis of the argument list:

function foo(): array { 

return []; 

}

This example declares a function “foo” that returns an array. In PHP 5.6, programmers would not specify the return type, which means that it is not clear to anyone reading the code what the function “foo” is supposed to output. Explicitly declaring return types makes code much easier to read, which is useful when debugging your code or working with code that was written by someone else.

Spaceship Operator

One interesting change to the syntax of PHP 7 is the introduction of the spaceship operator, which you can use to quickly and conveniently compare two expressions. Use this operator as follows:

a <=> b

  • If a is less than b, this expression outputs -1
  • If a is equal to b, this expression outputs 0
  • If a is greater than b, this expression outputs 1

Using this operator to compare variables requires much less typing than coding multiple tests with the traditional less than (<), equal to (==) and (>) greater than operators do.

Other Syntax Changes

Many other minor aspects of PHP syntax have also changed in the transition from PHP 5.6 to PHP 7. The following modifications are backward incompatible, which means that developers who have previously worked in PHP 5.6 will need to update their code if they want it to work under PHP 7.

  • Old-fashioned error handling has been replaced with object-oriented exceptions. This change is intended to make it easier for developers to find and fix bugs in their code.
  • Syntax for variable dereferencing has been changed to make it more consistent.
  • Syntax for the foreach statement has changed.
  • The list() operator no longer supports strings.
  • In PHP 5.6, a bug allowed the switch statement to have multiple default clauses, leading to code behaving unpredictably. This bug has now been fixed.
  • PHP 7 features more consistent conversions between integers and floating point numbers. This change eliminates some surprising errors that can occur when you intentionally or accidentally convert a variable that was initially stored as an integer into a floating point number, which is not necessarily a whole number, or a floating point into an integer.
  • The old-fashioned syntax for marking comments in INI files has been removed. Instead of prefixing comments with the # symbol, you now need to use a semi-colon instead.

To see details of other backward-incomptable changes that have been introduced in PHP 7, visit PHP.net to download the documentation for the new language version. This documentation explains the technical advances that appear in PHP 7 in detail and can be a useful reference manual for developers who are programming in PHP 7 for the first time. For a quick look at the new features in PHP 7, take a look at this summary.

How Do I Upgrade to PHP 7?

PHP 7 is the most significant update that has affected the PHP scripting language in more than a decade. Therefore, upgrading existing code to PHP 7 comes with a few challenges. However, the reward — much faster performance and less overall demand on your web servers — is worth the effort of upgrading.

First, you need to make sure that any libraries that your PHP project uses are available for PHP 7. If the libraries do not yet support PHP 7, you may have to hold off on upgrading for a while, or see if it is possible to remove dependence on those libraries.

If your code is written in PHP 5.5 or PHP 5.6, then upgrading will likely be straightforward. However, if you use PHP 4, then there are some syntax changes that you should be aware of. For example, the old style of constructor functions employed in PHP 4 is not supported in PHP 7, even though these constructors worked in PHP 5.

When upgrading to PHP 7, it is best to ensure that your code includes tests, such as unit and integration tests. These tests should catch any issues with your application before they show up as bugs in a live environment.

Who Is Already Using PHP 7?

As of December 2015, less than 0.1 percent of websites are using PHP 7. However, many sites are expected to upgrade soon, as PHP 7’s clear performance advantages mean that it is sure to play a role in the future of web development. WordPress, the popular website-building platform used by millions of small businesses and bloggers, has been preparing for the release of PHP 7 for months, which means that WordPress users can easily upgrade to the new version of the language.

Conclusion

PHP 7 is a major upgrade compared to the last stable version of the language, PHP 5.6. Although upgrading your code from PHP 5.6 to PHP 7 involves carefully checking for incompatibilities, both in your code and in any libraries on which your code depends on, the benefits of upgrading to PHP 7 make the effort very worthwhile. PHP 7 performs much faster than PHP 5.6, which allows sites that use the new version of the language to offer much better service to their users.

Comparing PHP 7 vs. HHVM

PHP is one of the most popular scripting languages used for web development. The latest version of PHP, PHP 7 is a new version of the language that is been optimized for fast performance. However, PHP has a rival in HHVM (Hip Hop Virtual Machine) — a virtual tool that executes PHP code. The competition between these two options is heating up, so let’s take a look at the performance that each can offer.

What is HHVM?

In 2008, Facebook started working on a tool to convert PHP script into C++ so it could be compiled and executed on web servers. The aim was to conserve server resources, an important goal, as Facebook’s user base was growing rapidly. In this sense, the project was a success; it allowed the server to accommodate between five and six times more traffic than it had managed before.

Fast-forward a couple years to 2010. Facebook’s server needs had grown even more, placing it in a position to require another innovation to allow it to operate more efficiently. In response to this demand, Facebook developed the Hip Hop Virtual Machine (HHVM).

HHVM uses Just-In-Time (JIT) compilation to convert PHP code into a type of bytecode. It then converts this bytecode into machine code and optimizes it so that it runs as quickly as possible.

What is PHP 7?

PHP 7 is the PHP community’s response to HHVM. Early announcements of the launch of PHP 7 claimed that it would offer better than 100 percent performance improvements over the previous version of the language, PHP 5.

You might be wondering why PHP skipped version 6. The answer is that development on PHP 6 began in 2005, but it went on so long and ran into so many problems that PHP 6 had developed a bad reputation long before it was ready for release. As a result, the PHP community decided to skip the name PHP 6 and go straight to PHP 7 for the new working version of the language.

The real question is not how PHP 7 compares to PHP 5, as it is pretty clear that PHP 7 offers speedier performance. Instead, the consideration is how PHP 7 compares to HHVM. Many experts have conducted tests on the two ways of handling PHP code, which have revealed some interesting results.

PHP 7 vs. HHVM: Similarities and Differences

Before answering the “which is better” question, let’s take a look at the key differences between PHP 7 and HHVM, as well as the ways in which they are similar.

Code Interpretation

The fundamental difference between PHP 7 and HHVM is the way in which each one interprets PHP code. PHP 7 uses the standard PHP interpreter, free software that is available for anyone to use, to directly interpret and execute PHP code on the server. This generates HTML code, which is then sent to the client. The client then displays the desired content to the web user.

In contrast, the Hip Hop Virtual Machine first converts PHP code into Hip Hop bytecode. This code is then translated into machine code and executed. Some optimization takes place during this translation, ironing out inefficiencies in PHP code with the aim of delivering faster performance.

Writing Code

Both the PHP interpreter and HHVM take PHP code and execute it. Therefore, the process of writing the code is pretty much the same in each case. However, if you want to use HHVM, you need to install it on your server and then call it using the hhvm command on the command line.

Benchmark Testing

HHVM has offered much faster performance than previous versions of PHP. However, recent benchmark tests suggest that PHP 7 is slightly faster than HHVM, at least in some situations. Let’s take a look at the results of some benchmark testing conducted by Kinsta.

  • WordPress: Running on WordPress 4.1.1, PHP 7 allows more than twice as many requests to be executed per second as PHP 5.6. However, it still doesn’t process quite as many as HHVM 3.6.1, which executed 624 requests per second in the test compared to just 604 executed each second by PHP 7.

  • Drupal: PHP 7 offers a distinct advantage over HHVM for Drupal users. PHP 7 can handle 37 percent more server requests per second compared to HHVM on Drupal 8.

Which Companies Use HHVM?

In addition to Facebook, which developed HHVM, many other businesses have adopted this solution to running PHP applications on their own servers. These include Wikimedia and the e-commerce site Etsy.

  • Wikimedia: Wikimedia hosts a huge range of educational content, including the famous Wikipedia online encyclopedia. Attracting nearly half a billion Internet users each month, Wikimedia needs to optimize server performance to cope with its high level of demand. HHVM poses a significant advantage over PHP in that it can load multiple SPU cores simultaneously whereas PHP is a single-threaded language that can’t be parallelized. According to Wikimedia, deploying HHVM shrank CPU load from 50 to just 10 percent, halved the mean time taken to respond to users submitting edits and reduced the average page load time from 1.3 seconds to just 0.9 seconds.

  • Etsy: With 54 million users, Etsy’s servers also face significant demands. Etsy engineers compared HHVM to PHP 5.4 and found that HHVM could cope with up to 280 server requests per second whereas the response time of PHP 5.4 started to dramatically increase once the number of requests grew beyond 190 per second.

What Does the Future Hold for PHP 7 and HHVM?

PHP 7 is due for stable release in November 2015. Therefore, companies are not yet using the new language, but promising benchmark test results of the performance of the beta version of PHP 7 could tempt more companies to adopt the new version of the language.

The future looks bright for PHP 7, but what about HHVM? It is likely that it is far from dead. Many businesses are already using HHVM to increase performance on their sites. The transition between PHP and HHVM is not instantaneous. It took Etsy more than six months to complete the transition. With the speed benefits of PHP 7 compared to HHVM being only very slight, it is unlikely that businesses will rush to switch back to PHP.

Facebook is continuing to develop HHVM. It has recently announced support for Mac OS X, making the technology accessible for developers who prefer to work in the Apple development environment. HHVM developers are convinced that HHVM is still faster than PHP 7 in many situations, including with WordPress.

Why Does the HHVM vs. PHP 7 Competition Matter to PHP Shop Owners?

As an online store owner, you need to make your decision on whether to use PHP 7 or HHVM based on the platform that hosts your shop. For example, if your site is built using WordPress, take a look at benchmark tests for HHVM and PHP 7 to find out how the latest release of each one performs. You want to choose the solution that can offer the biggest reduction in page load times, server response times and CPU usage.

Reasons to Choose HHVM

  • HHVM uses dynamic translation to deliver faster performance in many situations, including on WordPress.

  • HHVM uses less memory to process each request in cases where it faces a very large number of requests.

  • HHVM developers are steadily increasing the number of PHP code bases that the engine can run. It can already run the latest version of WordPress, along with many other common PHP frameworks and applications.

  • HHVM is open source. Even though HHVM has been developed by Facebook, it is open source, which means the source code is available to anyone who wants to use or alter it.

Reasons to Choose PHP 7

  • PHP 7 performs faster than HHVM in some situations, including when running on Drupal 8.

  • Using PHP 7 doesn’t require you to install or setup HHVM.

  • Code written in PHP 5 should work as expected after a transition to PHP 7, although some features of PHP 4 code are no longer supported in the new release. In practice, this means that any code created in the last decade is probably ready for the transition to PHP 7.

  • PHP 7 is developed by the PHP community, a group with a long-standing reputation for creating stable and reliable PHP releases.

HHVM vs. PHP 7: Make Your Choice

Don’t agonize for too long over the decision. Kinsta recommends that online businesses choose quickly between PHP 7 and HHVM. The sooner you make your decision, the sooner you can begin to implement the solution, allowing you to optimize your website performance. A poorly performing website can cause your reputation to suffer, which can be difficult to reverse.

Both HHVM and PHP 7 offer significant benefits compared to older versions of PHP. Make your choice and start the process of switching your site to the new system as soon as possible.