0

4 Most Important PHP 7 New Features, Must Know

Jack Calder September 22, 2015

With every new major version release of PHP many new features are being introduced. And, the upcoming PHP version is no different.  There has been a lot of debate over deciding the name of the next PHP version release – that will succeed the PHP 5.x series – that will be called as PHP 6 or PHP 7. You can find the discussion concerning each of the proposals in the RFC document titled as “Name of Next Release of PHP”. After many arguments, the next major version release of PHP was named as PHP 7.

PHP 7 is expected to release in mid October this year. Even if the release gets delayed, the new version will be released before the end of 2015. Of course, you’ll be interested in knowing about what all new features you can expect from the next major PHP 7 version release.

In this post, I am sharing 4 of the most important PHP 7 new features.

php 7 new features

1. Better Performance, 2X Speed

Slower performance of PHP has been an issue for PHP coders. But, you’ll see a lot of performance improvements in the new PHP 7 version compared to its previous versions.

Put it simply, PHP 7 will be 2X faster and more performant. Better performance of PHP 7 is because of the refactored PHP (Zend) engine that helps in speeding up PHP applications.

In fact, there has been a large performance boost that varies between 25 percent to 70 percent of real-world applications.

2. Support for Return Type Declarations

PHP 7 will give developers the ability to declare the return type that will help in specifying the type of value that a function will return – similar to argument type declarations.

To be more specific, the same types will be available for both type declarations and argument type hints.

Let’s now look at a basic return type declaration:

  1. <?php
  2. function floatsum($s1, $s2): float {
  3.     return $s1 + $s2;
  4. }
  5.  
  6. // a float value will be returned.
  7. var_dump(floatsum(3, 3));
  8. ?>

The output of the above example is as follows:

float(6)

3. Support for Scalar Type Hints

PHP 7 will allow use of scalar type hints. It means that you will be able to use type-hints with scalar type declarations – such as int, float, bool and string. There are two types of scalar type declarations: coercive (default) and strict.

In essence, you can enforce either coercive mode or strict mode on the parameters using the following types:

  • integers (int)
  • strings (string)
  • booleans (bool)
  • floating-point numbers (float)

Here’s a code snippet that will show you how you can declare types for parameters coercively:

  1. <?php
  2. // Coercive mode
  3. function sumOfInteger(int $i)
  4. {
  5.     return array_sum($i);
  6. }
  7. var_dump(sumOfInteger(2, '3', 3.1));

The output of this code will be:

int(8)

However, in order to enable the strict mode, you’ll have to place a single “declare” directive at the top of your file that looks something like:

  1. <?php
  2. declare(encoding='ISO-8859-1');
  3. // enter your code here
  4. ?>

4. Use of Spaceship Operator

Another great feature of PHP 7 is the introduction of a new Combined Comparison Operator “<=>”, also referred to as the Spaceship Operator. This operator basically helps in performing combined comparisons – mostly when it comes sorting, as you can see in the following code snippet:

  1. <?php
  2.  
  3. // Integers
  4. echo 2 <=> 2; // 0
  5. echo 2 <=> 3; // -1
  6. echo 3 <=> 2; // 1
  7.  
  8. // Floats
  9. echo 2.5 <=> 2.5; // 0
  10. echo 2.5 <=> 3.5; // -1
  11. echo 3.5 <=> 2.5; // 1
  12.  
  13. // Strings
  14. echo "b" <=> "b"; // 0
  15. echo "b" <=> "c"; // -1
  16. echo "c" <=> "b"; // 1
  17. ?>

Note: In order to perform the comparisons correctly, make sure to go through PHP’s type comparison rules. 

Final Words

In case you’re fearing backwards compatibility breaks, and don’t want to switch to PHP 7, then you must stop. You don’t need to upgrade to the new PHP version, if you don’t want. You can still use PHP versions 5.x, but it would be better if you’ll choose to have a modern PHP version. And so, you should give PHP 7 a try since it focuses on inconsistency fixes and making performance improvements. Reading this post will give you a better idea of the key features of PHP 7.

Leave a Comment


Login to your account

Can't remember your Password ?

Register for this site!