Are you a PHP Developer? Get ready for the PHP 7 revolution

4 min reading
Are you a PHP Developer? Get ready  for the PHP 7 revolution
Are you a PHP Developer? Get ready  for the PHP 7 revolution

BBVA API Market

The software development industry has welcomed the arrival of some interesting developments in the form of programming languages, not because they have been recently launched, but because of their growing importance in the projects: Python and Ruby are two such examples. But even though that is the case, Java is still Java and PHP is still PHP. And 2015 will be the year in which PHP came to a decisive turning point in its history with the arrival of PHP 7

Logically PHP 7 brings all kinds of new features and the conclusion is clear: the new version is good for developers in this syntax and for developing digital projects in general. The improvements are interesting. These are changes not only in performance, but also in the use of proper syntax when programming. Here is a summary of the most important ones:

1. PHP 7 greatly improves server performance

Several studies have been conducted on the performance improvement offered by PHP version 7. Perhaps one of the most interesting is this presentation by Rasmus Lerdorf (@rasmus), creator of the first version of the PHP programming language, in which there are several graphs showing that PHP 7 doubles the speed of PHP 5.6 in almost all applications and also uses less memory. Here are two practical examples of the performance of the new PHP 7 syntax with CMS Drupal 8 and WordPress 4.1: 

What can be seen in Lerdorf ‘s two graphs is that PHP 7 manages more than twice as many requests as the PHP server in two common CMS for creating and editing websites such as Drupal and WordPress. This means that the same number of requests takes half the time to serve them or that it serves twice as many pages as PHP 5.6 in the same response time. In Drupal, PHP 7 responds to 185 requests per second while PHP 5.6. responds to 101. In WordPress there are 545 requests in PHP 7 compared to 262 in PHP 5.6.

They are not the only platforms where PHP 7 shows higher performance than previous versions of this syntax, namely version 5.6.:

●      In phpBB, the PHP-based open source forum system, the latest version of the programming language offers a performance of 912 requests per second. In 5.6. this is 483 requests per second. The performance is almost 100% higher.

●      In the Wardrobe editing system, performance is similar: 912 requests per second in PHP 7 and 532 in PHP 5.6. All platforms and applications studied are in Lerdorf’s presentation.

2. PHP 7 vs Facebook HHVM

With the release of PHP 7, HHVM (Hip Hop Virtual Machine), the virtual machine that compiles and runs PHP developed by Facebook, has a rival.

In 2008, the social network run by Mark Zuckerberg had to consider a change in concept due to the enormous growth in user numbers: it created a virtual tool that converts PHP script to C++ for it to be compiled and run in the servers. This made it possible to host between five and six times more traffic on the same server than before. In 2010, Facebook again figured out how to improve server performance with the creation of HHVM.

HHVM operates with a JIT engine (Just-in-Time) to transform PHP language into byte code and then into machine code, which allows it to be compiled and run in much quicker times and with much lower requirements for the server. This greatly multiplies the speed. PHP 7 was created to compete with HHVM and the tests are unbeatable. 

3. New operators in PHP 7: Spaceship ⇔ and null

The operator in PHP is always used to compare two expressions. Depending on the values, the comparison returns a value of 0 if the two elements are equal, a value of 1 if the first is greater and -1 if the second is greater. Such comparisons (whether flexible == or strict ===) in PHP are always based on comparison tables of types in PHP.

Here are some examples of comparisons of integers, decimal numbers and strings with the new PHP 7 spacecraft operator ⇔ or also called a combined comparison operator:

<?php

// Whole numbers

echo 1 <=> 1; // 0

echo 1 <=> 2; // -1

echo 2 <=> 1; // 1

 

// Decimal numbers

echo 1.5 <=> 1.5; // 0

echo 1.5 <=> 2.5; // -1

echo 2.5 <=> 1.5; // 1

 

// Strings

echo “a” <=> “a”; // 0

echo “a” <=> “b”; // -1

echo “b” <=> “a”; // 1

?>

 

The fusion operator of null is a conditional allocation that aids the programmer to check a value prior to using it. A practical example of using this operator (only available from PHP 7):

$a = $b ?? “default”;

4. New JSON parser

With PHP 7 the json extension is replaced by jsond. The current json parser does not have an open source json extension. This is the reason why some Linux distributions do not have json support and are excluded. The solution was to change and that would facilitate further improvements without giving problems.

5. New statements in PHP 7

In PHP 7 new types of variables have been added to define functions and methods: int, float, bool and string. That’s what this new version of this syntax is called scalar statements. These new types of statements behave exactly like the existing methods in PHP for the usual building of functions. With this change, programming in PHP is expected to be more accurate and self-documented.

The other statements that are new to PHP 7 are return statements. These statements allow the programmer to type the return of a function with the values that should be returned, that value will stand after the arguments and before the opening of keys, always preceded by a colon. A practical coding example:

<?php
function a() : bool
{
   return 1;
}
var_dump(a());

6. Anonymous classes reach PHP 7

PHP 7 comes with capacity to implement anonymous classes. They can be used instead of definitions of whole classes for disposable objects. The encoding of an anonymous class in the latest version of PHP would look like this:

object(class@anonymous)#2 (0) {
}

Follow us on @BBVAAPIMarket

It may interest you