We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Speed Up with PHP 7

December 28, 2015
| Articles, VPS Hosting, Web Hosting

Success does not necessarily come from breakthrough innovation but from flawless execution. A great strategy alone will not win a game or a battle. As you may have your strategy, we are giving you the necessary artillery to win all of your fights.


We want our clients to have the best possible hosting experience, and there is nothing better than the latest scripting language support for your website! As of today PHP 7 is fully implemented and supported for all of our services at Host1Plus!


We waited 11 years for PHP 7 release after its predecessor PHP 5 came out – what took so long?


On December 3rd 2015 a major version of PHP 7 was introduced to IT enthusiasts. While some may wonder, what took so long after PHP 5 release, others may be surprised by the jump from PHP 5 to PHP 7.


PHP 6 Gone Missing?


Long story short is that the Unicode problems that were supposed to be solved in version 6 miserably failed to fulfill the expectations and had a huge amount of complications. Therefore PHP Group developers made a decision to skip PHP 6 and go straight for PHP 7. In case you are interested about PHP 6 and Unicode aspects, you can read more about it in Wikipedia. Other than that, let’s look at PHP 7 features we have waited for so long…


Twice as Fast PHP 7 Performance


No doubt this is one of the major improvements. PHP 7 is based on the PHP Next-Gen project or PHPNG for short.  From the very begining PHP uses Zend Engine, and PHP 7 is powered by Zend Engine 3. The official benchmarks are impressive – most applications running on PHP 5.6 run at least twice as fast on PHP 7 with lower memory consumption as a bonus. All that just from upgrading PHP, without having to change a single line of code!


Take a look at the results based on benchmark made by Zend Performance Team.




Three-Way Comparison


PHP 7 has introduced a new operator <=> called “Spaceship” that is used to compare two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.


What is the benefit from this feature?


To put it simply, it is more dynamic (and shorter) than trying to use boolean operators. This now gives you an extra element of control and, if you only have few outcomes, you can tidy up those disturbing if, elseif  and else statements.


An example is given below:

// Integers
echo 2 <=> 2; // 0
echo 2 <=> 3; // -1
echo 3 <=> 2; // 1
// Floats
echo 2.5 <=> 2.5; // 0
echo 2.5 <=> 3.5; // -1
echo 3.5 <=> 2.5; // 1
// Strings
echo "b" <=> "b"; // 0
echo "b" <=> "c"; // -1
echo "c" <=> "b"; // 1


Comparisons are performed according to PHP’s usual type comparison rules that you may read here.


Support for Return Type Declarations


PHP 7 has implemented Return Type Declarations, which allows developers to declare the return type value that a function will output – similar to argument Type Hints.


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:


function arraysSum(array ...$arrays): array
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);

print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));


The output of the above example is as follows:


    [0] => 6
    [1] => 15
    [2] => 24


You can find more in-depth information about return type declarations in the official Return Type Declarations documentation.


Support for Scalar Type Hints


PHP 7 has introduced the 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)


By default the mode is coercive, else known as weak. Switching between modes is possible with the help of declare statement:


declare(strict_types=0); //Weak
declare(strict_types=1); //Strict

The declare statement must be the first line of the file and it applies only to that file. If we declare the type as strict this will affect all the internal functions of PHP to any extensions installed.


Let us look into an example:


<?php class Item
    // more code
    public function getPrice()
        return $this->price;
    public function setPrice(float $price)
        $this->price = $price;
    public function setTitle(string $title)
        $this->title = $title;
    public function setPublished(bool $published)
        $this->published = $published;
    public function setId(int $id)
        $this->id = $id;


Weak Type Mode


$item = new Item();


Passing a string value ‘1.2’ will not throw any error/warning. It will automatically typecast to float.


$item = new Item();


Now if we pass a ‘string’ value we get a Fatal error: Uncaught TypeError: Argument 1 passed to Item::setPrice() must be of the type float, string given.


So in a weak type mode if the values can be converted to appropriate types no errors or warning are thrown, else we will get fatal errors.


Strict Type Mode


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


// enter your code here


Note: Full documentation and examples of scalar type declarations can be found in this reference.




Lots of improved features are waiting for you, why not start playing with PHP 7 and lay your hands on to upgrade all of your legacy systems? Upgrade to PHP 7 together with Host1Plus to enjoy increased usability and efficiency while developing your website, fast performance, easy integration with various applications and compatibility with all OS such as Windows, UNIX and so forth!


Express your thoughts on what you do and do not like about the new PHP 7 release in the comments below! If you have found this article valuable, share it with your friends and fellow developers or other people that are interested in programming!


Thanks for taking your time to read this article!

By Dovainis Kalėda
Categories: Articles, VPS Hosting, Web Hosting
No Comments Leave a Comment
Leave a Comment