The performance and
new features of PHP 7

by Enrico Zimuel
Senior Software Engineer - Zend
Rogue Wave Software Inc.


Turin Web Performance Meetup, 16th May 2017

About me

PHP

  • PHP: Hypertext Preprocessor
  • The most popular server-side language: PHP is used by 82.6% of all the websites (source: w3techs.com)
  • Used by Facebook, Wikipedia, Yahoo, Etsy, Flickr, Digg, Badoo, Tumblr, Dailymotion, etc
  • 22 years of usage, since 1995
  • Full OOP support since PHP 5

PHP ecosystem

PHP 7

Released: 3 December 2015

Last major was PHP 5, 13 July 2004 (11 years!)

Last release is 7.1.5 (11 May 2017)

PHP 6 ?

Unicode support at the core language level

Not released, project abandoned

Supported Versions

Version Initial release Active support Security support
5.6 28 Aug 2014 19 Jan 2017 31 Dec 2018
7.0 3 Dec 2015 3 Dec 2017 3 Dec 2018
7.1 1 Dec 2016 1 Dec 2018 1 Dec 2019

PHP7 in a nutshell

  • Great performance (thanks to PHPNG)
  • Return/Scalar Type Declarations
  • Anonymous Classes
  • Nullable types
  • Void return type
  • New Error hierarchy
  • Multiple catch
  • And more...

Performance

PHPNG, PHP Next Generation

Project by Dmitry Stogov (Zend)

New data structure management in the PHP engine

Great performance improvement!

Benchmark


$a = [];
for ($i = 0; $i < 1000000; $i++) {
    $a[$i] = ["hello"];
}
echo memory_get_usage(true);
PHP 5.6 PHP 7
Memory Usage 428 MB 33 MB
Execution time 0.49 sec 0.06 sec

Other languages

PHP 7 is also faster than Python 3!

Wordpress

Drupal

Magento

Case studies

  • Badoo saved one million dollars switching to PHP 7 (source)
  • Tumblr reduced the latency and CPU load by half moving to PHP 7 (source)
  • Dailymotion handles twice more traffic with same infrastructure switching to PHP 7 (source)

Badoo

Tumblr

New Features

Return Type Declarations


function foo(): array {
    return [];
}

function bar(): DateTime {
    return null; // invalid
}

More information: PHP documentation

Scalar Type Declarations


declare(strict_types=1);

function sendHttpStatus(int $statusCode, string $message) {
    header('HTTP/1.0 ' .$statusCode. ' ' .$message);
}
sendHttpStatus(404, "File Not Found"); // ok
sendHttpStatus("403", "OK"); // fatal error

function add(float $a, float $b): float {
    return $a + $b;
}
add(1, 2); // float(3)

More information: PHP documentation

Anonymous Classes


/* return an anonymous class */
return new class($controller) implements Page {
    public function __construct($controller) {
        /* ... */
    }
    /* ... */
};

class Foo {}
$child = new class extends Foo {};
var_dump($child instanceof Foo); // true

More information: PHP documentation

Nullable types

  • For parameters and return values
  • Prefixing the type name with a ?
  • NULL can be passed as an argument,
    or returned as a value

Example


function hi(?string $name): ?string
{
    if (null === $name) {
        return null;
    }
    return 'Hello ' . $name;
}

echo hi(null); // returns null
echo hi('Enrico'); // returns 'Hello Enrico'
echo hi(); // Fatal error: Too few arguments to function hi(), 0 passed

Void Return Type


function swap(&$left, &$right): void
{
    if ($left === $right) {
        return;
    }
    $tmp   = $left;
    $left  = $right;
    $right = $tmp;
}

$a = 1;
$b = 2;
var_dump(swap($a, $b), $a, $b); // null, int(2), int(1)

New Error hierarchy

Throwable: Error, Exception


function call_method($obj) {
    $obj->method();
}

try {
    call_method(null); // oops!
} catch (Error $e) {
    printf ("Error: %s\n", $e->getMessage());
} catch (Exception $e) {
    printf ("Exception: %s\n", $e->getMessage());
}

More information: PHP documentation

Multiple catch {}


try {
    // Some code...
} catch (ExceptionType1 $e) {
    // Code to handle the exception
} catch (ExceptionType2 $e) {
    // Same code to handle the exception
} catch (Exception $e) {
    // ...
}

try {
    // Some code...
} catch (ExceptionType1 | ExceptionType2 $e) {
    // Code to handle the exception
} catch (\Exception $e) {
    // ...
}

Conclusion

Thanks!

Contact me: enrico.zimuel [at] roguewave.com

Follow me: @ezimuel



Creative Commons License
This work is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
I used reveal.js to make this presentation.