What's new in PHP 7?

6050b4913a2f5cfb09a1cdb31ea489ed?s=47 Mattias Geniar
December 03, 2015

What's new in PHP 7?

PHP 7 has been released! During the PHP 7 Release Party in Antwerp I gave a presentation on "What's new in PHP 7?" to highlight the new features, the removed features and what to expect in terms of performance.

6050b4913a2f5cfb09a1cdb31ea489ed?s=128

Mattias Geniar

December 03, 2015
Tweet

Transcript

  1. WHAT'S NEW IN PHP 7? PHP LOST COUNT - WHAT

    HAPPENED TO PHP 6? WHO CARES? PHP 7! 7!! @mattiasgeniar PHP 7 Release Party @ Nucleus
  2. WHAT'S THIS TALK ABOUT? History: PHP 6 New features in

    PHP 7 Removed features Q & A Some Nucleus 'announcements' Raf e?
  3. WHO AM I? Mattias Geniar System Engineer / Support Lead

    @ Former dev, mostly Ops now Strong advocate of #DevOps Blogger at and Nucleus.be ma.ttias.be www.nucleus.be
  4. CRON.WEEKLY Weekly newsletter with linux & open source news, tools,

    tips & projects www.cronweekly.com
  5. SPECIAL SHOUT-OUT Big thanks to Tom Schuermans ( ) for

    his help in the presentation & organisation! @tschuermans
  6. HISTORY: LOSING TRACK, WHAT ABOUT PHP 6? Development started in

    2015 ... and died in 2010 "The Great Unicode Release" Besides unicode, all features went to 5.x release 5.3 was pretty much PHP 6 without unicode
  7. PHP 7: WHAT'S NEW? Scalar type declarations Return type declarations

    null coalesce operator Spaceship operator Arrays in constant de ne()'s Anonymous classes
  8. PHP 7: WHAT'S NEW? (CONT'D) Unicode codepoint escape syntax &

    IntlChar Filtered (whitelisted) unserialize() Grouped namespacing in use Session improvements
  9. SCALAR TYPE DECLARATIONS // Coercive mode function sumOfInts(int ...$ints) {

    return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1)); // Outputs int(9); New in PHP 7: string, integer, oat, boolean
  10. strict mode // Strict mode declare(strict_types=1); function sumOfInts(int ...$ints) {

    return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1)); // Outputs PHP Fatal error: Uncaught TypeError: Argument 2 passed to sumOf must be of the type integer, string given, called in test2.php o line 10 and defined in test2.php:5 Stack trace: #0 test2.php(10): sumOfInts(2, '3', 4.1) #1 {main}
  11. RETURN TYPE DECLARATIONS function sum($a, $b): float { return $a

    + $b; } // Result is a float, even with integer input var_dump(sum(1, 2)); float(3)
  12. strict mode declare(strict_types=1); function sum($a, $b): int { return $a

    + $b; } // Result is a fatal error, float vs integer mismatch var_dump(sum(1, 2.5)); PHP Fatal error: Uncaught TypeError: Return value of sum() must be of the type integer, float returned in test3.php: Stack trace: #0 test3.php(11): sum(1, 2.5) #1 {main}
  13. 'STRICT MODE' GOTCHAS Enabled/disabled per le, not a php.ini setting

    applies to function calls, not to the functions declarations (so: if de ned in test.php, all function calls made from test.php have strict typing) Integers are allowed for functions requiring oat
  14. NULL COALESCE OPERATOR <?php // PHP < 7 $username =

    isset($_GET['user']) ? $_GET['user'] : 'nobody' // PHP >= 7 $username = $_GET['user'] ?? 'nobody'; // Chained: $_GET, then $_POST, then 'nobody' $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody'
  15. SPACESHIP OPERATOR

  16. SPACESHIP OPERATOR It returns -1, 0 or 1 when $a

    is respectively less than, equal to, or greater than $b echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // Example: usort callbacks // Note: also works on characters usort($data, function ($left, $right) { return $left[1] <=> $right[1]; });
  17. ARRAYS IN CONSTANTS <?php define('ANIMALS', ['dog', 'cat', 'bird']); echo ANIMALS[1];

    // outputs "cat" ?>
  18. ANONYMOUS CLASSES <?php $app = new Application; // 'new class'

    creates anonymous class $app->setLogger(new class implements Logger { public function log(string $msg) { echo $msg; } });
  19. ANONYMOUS FUNCTIONS (PHP >= 5.3) <?php $example = function ($text)

    { echo $text; }; $example('I want that PHP 7 elephpant!');
  20. UNICODE <?php echo "\u{aa}"; // Output in PHP < 7

    \u{aa} // Output in PHP >= 7 ª
  21. YOU KNOW WHAT THAT MEANS ... <?php echo "\u{1F61D}"; Emoji!

  22. FILTERED / WHITELISTED UNSERIALIZE() <?php // converts all objects into

    __PHP_Incomplete_Class object $data = unserialize($foo, [ "allowed_classes" => false ]); // converts all objects into __PHP_Incomplete_Class object // except those of MyClass and MyClass2 $data = unserialize($foo, [ "allowed_classes" => ["MyClass", "MyClass2"] ]);
  23. GROUPED USE DECLARATIONS <?php use const some\namespace\ConstA; use const some\namespace\ConstB;

    use const some\namespace\ConstC; // PHP 7+ code use const some\namespace\{ConstA, ConstB, ConstC};
  24. SESSION IMPROVEMENTS session_start() accepts array of options (undocumented) option "read_and_close":

    stops session blocking! // PHP 7: session_start([ 'read_and_close' => true, ]); // PHP <= 7 session_write_close();
  25. REMOVED FEATURES & EXTENSIONS IN PHP 7 plain 'mysql' extension

    removed (mysqli / pdo only from now on) php.ini: removed "#" comments, only ";" from now on Removed asp_tags: <% ... %> Finally removed ereg() (deprecated since 5.3) preg_replace() no longer supports "\e"
  26. REMOVED FEATURES & EXTENSIONS IN PHP 7 (PART DEUX) Finally

    removed set_magic_quotes_runtime() (deprecated since 5.3) Removed always_populate_raw_post_data in php.ini
  27. UNIFORM VARIABLE SYNTAX <?php // PHP < 7 $object->$array['item']; -->

    $object->{$array['item']}; // PHP >= 7 $object->$array['item']; --> ${object->$array}['item'];
  28. DATE.TIMEZONE WARNINGS REMOVED default still UTC <?php // PHP <

    7 Strict Standards: date(): It is not safe to rely on the system timezone settings. .... // PHP >= 7 (nothing)
  29. SAPI TRIVIA How many ways are there to run PHP?

    PHP 5.6: 20+ PHP 7.0: 6 PHP 7.0 removed: aolserver, apache, apache_hooks, apache2 lter, caudium, continuity isapi, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, webjames PHP 7.0 supports: embedded, apache2 module, fpm, cgi, cli, litespeed
  30. PERFORMANCE 7 VS 5.6 Test sites http://phpseven.lin5.nucleus.be/wordpress/ http://php ve.lin5.nucleus.be/wordpress/ Basic

    Wordpress, no tweaks or con g changes $ ab -c 1 -n 100 http://site/wordpress PHP 7.0: 12.9req/s (75ms per request) PHP 5.6: 8.9 req/s (120ms per request) PHP 5.5: 2.5 req/s (450ms per request)
  31. PERFORMANCE OF PHP 7 IN ONE SLIDE

  32. PERFORMANCE OF PHP 7 IN ONE SLIDE 6x faster than

    PHP 5.5 1.6 faster than PHP 5.6 Your mileage may vary
  33. Q & A (hold on, we've got more coming)

  34. SOME NUCLEUS ANNOUNCEMENTS 100gr of PHP 7 for everyone! (conveniently

    packed in a 16GB USB disk with the PHP 7 source code on it)
  35. SOME NUCLEUS ANNOUNCEMENTS As of today, PHP 7 support on

    our shared hosting! Let's merge this feature live!
  36. SOME NUCLEUS ANNOUNCEMENTS Free PHP 7 shared hosting for everyone

    here! (give me your e-mail address, I'll hook you up)
  37. PHP 7 ELEPHPANT RAFFLE Time to bribe Mike!

  38. THANKS! Contact via @mattiasgeniar or via m@ttias.be