Over a million developers have joined DZone.

Everything You Need to Know about PHP 7

PHP 7 is out, and you likely have a few questions. Here's a look at differences from PHP 6, why PHP 7 came about, what's new, and more!

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
php.php 6 ,php 7web dev ,peperformance

Published at DZone with permission of Omed Habib, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}