5 Things you should know about PHP7

Well, if you are not living under a rock you should have probably heard about PHP7, which is the latest iteration of widely used open source scripting language called PHP. PHP7 comes with a pretty cool change log and release notes to read. Including the most important perfomance imporvement. As per Zend performance improvement is huge!. Without changing single piece of code, just upgrading to PHP7 gives you enormous performance. This article will bring you a brief idea about most important five things you should know about PHP7.

#1 : Perfomance improvement

As I indicated in the introduction, perfomance improvement is the most important change we have on PHP7. The re-design of PHPNG (PHP-Next-Gen) is the major reason for this perfomance improvement on 7. The development team of PHPNG refactored the Zend Engine, and remarkably optimized memory usage. Performance benchmarks provided by the Zend Performance Team below will speak the rest.

php7-performance

frameworks-php7-performance

 

#2 : Scalar Type Hints & Return Types:

Many developers including me would like to have ability to declare return type for the function or method. Now onwards PHP7 will allow developers to declare what kind of return type function is expected to return a value. Below is an example for type hinting parameters.

Example :

public function isUser(int $id) : bool 
{
return isset($this->users[$id]) ? true : false;
}

In the above example bool indicates the function will return only boolean value. This allows developers to set expecting strings, int, float or bool to be passed or returned. You can also have look at many examples in RFC here.

#3 : Spaceship Operator

In PHP7 they have introduced a new operator called spaceship operator (<= >) otherwise called combined comparison operator. The Spaceship operator runs under the official name of Combined Comparison Operator. This can be used mostly in sorting and combined comparison. Spaceship Operator works like strcmp() or version_compare(). Check out the simple example below.

Example :

Before PHP7 :

function order($a, $b)
{
return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
}

After PHP7 :

function order($a, $b) 
{
return $a <=> $b;
}

Looks beautiful right ? You can find more example here.

 

#4 : 64-Bit Windows Systems Support

As you already know PHP is a prominent member of the LAMP stack. Which means its native environment is Linux – but it is also possible to run it on a Windows system. Earlier, the 5.x series doesn’t had 64-bit integer or large file support. So until now x64 builds have been considered experimental.

Now in PHP7, it have introduces consistent 64-bit support which means both native 64-bit integers and large files will be supported, allowing you to confidently run the script on 64-bit windows based system in the future.

 

#5 : Additional Features

Apart from the above core features added to PHP7, these are the few major changes :

Abstract Syntax Tree: Big change on the core. It makes parsing php code easier, less error, and way to build better things in future.

Closure::call() : This is the new method added into core. It allows variable binding at call time rather pre-binding.

Null Coalesce Operator : The coalesce operator or ?? added to the core, which returns the first operand is exists or null.

Example :

Before PHP7:

$id = isset($_GET[‘id’]) ? $_GET[‘id’] : null;

in PHP7

$id = $_GET[‘id’] ?? null;  //equivalent of writing old above syntax

Uniform Variable Syntax : The main deference of existing syntax and new syntax is to keep consistence and complete variable syntax. Allows advance expressions.

Example :

$$foo['bar']['baz']   interpreted as   ($$foo)['bar']['baz']
$foo->$bar['baz']()   interpreted as   ($foo->$bar)['baz']()

Unicode Codepoint Escape Syntax : PHP7 introduced syntax to escape unicode codepoint as below.

Example :

echo "\u{202E}Reversed text"; // outputs ‮Reversed text
echo "\u{1F602}"; // outputs 😂

Along with all above features 7 changes developers can stop cecognizing method names as class names to be a constructor as per PHP4. It has been controversial early to remove but later has been removed completely as currently we no longer required to warn PHP developer how PHP4 works. This removal added as simplicity.

mysql_* functions are completely removed as extension removed by default, which will be a pain for currently running applications based on older version of PHP. Multiple default clause support has been removed from the switch case. Support of ASP tags (<%) and script tags (<script language=”php”> </script>) has been removed meaning that you can use those tags to enter php mode.

You can find more, list of deprecated or removed functionalities here, Removed deprecated functionalities in PHP7.

Wrapping up

Recent changes makes PHP as much more matured language. But it is good message to all PHP developers for building awesome enterprise level applications using latest PHP version.

  • Jithesh Kt

    Nice one!