SlideShare a Scribd company logo
What To Expect From PHP7
PHP 7: What To Expect
Lorna Mitchell, CodeMotion
Rome 2016
 
 
Slides available online, help yourself:
http://www.lornajane.net/resources
Versions of PHP
Version Support until Security fixes until
PHP 5.5 (expired) 10th July 2016
PHP 5.6 31st December 2016 31st December 2018
PHP 7.0 3rd December 2017 3rd December 2018
 
see also: http://php.net/supported-versions.php
PHP 7 Is Fast
PHP 7 Is Fast
Why PHP 7 Is Fast
• Grew from the phpng project
• Influenced by HHVM/Hacklang
• Major refactoring of the Zend Engine
• More compact data structures throughout
• As a result all extensions need updates
• http://gophp7.org/gophp7-ext/
 
Rasmus' stats: http://talks.php.net/fluent15#/6
Abstract Syntax Trees
PHP 7 uses an additional AST step during compilation
 
 
This gives a performance boost and much nicer
architecture
New Features
Combined Comparison Operator
The <=> "spaceship" operator is for quick greater/less
than comparison.
 
1 echo 2 <=> 1; // 1
2 echo 2 <=> 3; // -1
3 echo 2 <=> 2; // 0
Ternary Shorthand
Refresher on this PHP 5 feature:
1 echo $count ? $count : 10; // 10
2 echo $count ?: 10; // 10
Null Coalesce Operator
Operator ?? is ternary shorthand (?:) but with isset().
 
1 $b = 16;
2
3 echo $a ?? 2; // 2
4 echo $a ?? $b ?? 7; // 16
Type Hints
PHP 5 has type hinting, allowing you to say what kind of
parameter is acceptable in a method call.
 
1 function sample(array $list, $length) {
2 return array_slice($list, 0, $length);
3 }
Type Hints
If we use the wrong parameter types, it errors
1 print_r(sample(3, 3));
 
PHP 5 error:
Catchable fatal error: Argument 1 passed to sample() must be of the type
array, integer given
 
PHP 7 error:
Fatal error: Uncaught TypeError: Argument 1 passed to sample() must be of
the type array, integer given
Scalar Type Hints
PHP 7 lets us hint more datatypes:
• string
• int
• float
• bool
Scalar Type Hints
We can amend our code accordingly:
1 function sample(array $list, int $length) {
2 return array_slice($list, 0, $length);
3 }
 
And then call the method:
1 $moves = ['hop', 'skip', 'jump', 'tumble'];
2 print_r(sample($moves, "2")); // ['hop', 'skip']
Scalar Type Hints
To enable strict type check, add this line in the calling
context:
declare(strict_types=1);
Return Type Hints
We can also type hint for return values.
1 function sample(array $list, int $length): array {
2 if($length > 0) {
3 return array_slice($list, 0, $length);
4 }
5 return false;
6 }
Return Type Hints
This works:
1 $moves = ['hop', 'skip', 'jump', 'tumble'];
2 print_r(sample($moves, "2")); // ['hop', 'skip']
 
This because false fails the return typehint
1 $moves = ['hop', 'skip', 'jump', 'tumble'];
2 print_r(sample($moves, 0));
Fatal error: Uncaught TypeError: Return value of sample() must be of the
type array, boolean returned
Exceptions and Errors
PHP 5 exceptions are alive, well, and excellent
Exceptions in PHP 7
They now implement the Throwable interface
Errors in PHP 7
Some errors are now catchable via the Error class
Catching Exceptions and Errors
1 function sample(array $list, int $length) {
2 throw new Exception("You fail");
3 }
4
5 try {
6 $a = sample(1,1);
7 } catch (Exception $e) {
8 echo "you hit the exception line";
9 } catch (TypeError $e) {
10 echo "you passed the wrong arguments"; }
Catch Method Calls on Non-Objects
Does this error look familiar?
1 $a = 6;
2 $a->grow();
 
PHP 5:
Fatal error: Call to a member function grow() on integer
 
PHP 7:
Fatal error: Uncaught Error: Call to a member function grow() on integer
Catch Method Calls on Non-Objects
PHP 7 allows us to catch Errors as well as Exceptions
1 try {
2 $a = 6;
3 $a->grow();
4 } catch (Error $e) {
5 echo "(oops! " . $e->getMessage() . ")n";
6 // now take other evasive action
7 }
Newer bits of PHP will use this new Error mechanism
Random* Functions
PHP 7 introduces a couple of neat randomness functions:
• random_bytes() — Generates cryptographically
secure pseudo-random bytes
• random_int() - Generates cryptographically secure
pseudo-random integers
 
For PHP <7 use
https://github.com/paragonie/random_compat
Upgrading to PHP 7
Uniform Variable Syntax
This is a feature as well as a gotcha.
• Good news: more consistent and complete variable
syntax with fast parsing
• Bad news: some quite subtle changes from old syntax
when dereferencing or using $$
• If in doubt, add more { and }
 
RFC: https://wiki.php.net/rfc/uniform_variable_syntax
Phan
Static analyser: https://github.com/etsy/phan
• reads code and PHPDoc comments
• warns about BC breaks including uniform variable
syntax issues
• warns you about undeclared things
• checks parameter types
 
Includes a great guide to codebase wrangling:
http://lrnja.net/1W2Gjmb
Foreach
Check that you're not relying on any foreach()
weirdnesses
• The array pointer will no longer move, look out for
use of current() and next() inside a foreach()
loop
• Don't assign to the thing you're looping over, the
behaviour has changed
 
RFC: https://wiki.php.net/rfc/php7_foreach
Deprecated Features
You should expect things that trigger E_DEPRECATED in
older versions of PHP to be removed.
 
Caveats:
• The RFC to remove things was agreed but it hasn't
been implemented yet
• The mysql_* functions really are removed
• PHP 4 constructors are less removed than you'd
expect them to be
Upgrading to PHP 7
Step 1: Upgrade to PHP 5.5 or 5.6.
Upgrading to PHP 7
Step 1: Upgrade to PHP 5.5 or 5.6.
 
Most PHP 5 code will just work with a few pitfalls to look
out for.
 
You probably want to run composer update while
you're at it
Upgrading to PHP 7
There are fabulous comprehensive instructions
http://php.net/manual/en/migration70.php
 
Making the business case for PHP 7
• calculate hardware cost saving
• calculate developer time required
 
Done :)
Acquiring PHP 7
Windows users: get a new binary
 
Linux users:
• wait for your distro to update
• use an alternative source (e.g.
http://lrnja.net/1PIPw2M)
• compile it yourself
The Future
The Future
• PHP 5.6 support has been extended
• Support until 31st December 2016
• Security fixes until 31st December 2018
• PHP 7.0 is safe to run
• PHP 7.1 looks even better
 
(see also http://php.net/supported-versions.php)
Questions?
Slides are on http://lornajane.net
(related blog posts are there too)
 
Contact me
• lorna@lornajane.net
• @lornajane
 
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0
Unported License.

More Related Content

What's hot (20)

PDF
React Native One Day
Troy Miles
 
PDF
Découvrir dtrace en ligne de commande.
CocoaHeads France
 
PPTX
Elixir
Fuat Buğra AYDIN
 
PDF
LINQ Inside
jeffz
 
PDF
Intro to React
Troy Miles
 
PPTX
5 Tips for Better JavaScript
Todd Anglin
 
PDF
How much performance can you get out of Javascript? - Massimiliano Mantione -...
Codemotion
 
PDF
Object Oriented Exploitation: New techniques in Windows mitigation bypass
Sam Thomas
 
PPTX
Angular2
Oswald Campesato
 
PDF
Connect.Tech- Swift Memory Management
stable|kernel
 
KEY
DjangoCon US 2011 - Monkeying around at New Relic
Graham Dumpleton
 
PDF
Introduction to Asynchronous scala
Stratio
 
PDF
Recipes to build Code Generators for Non-Xtext Models with Xtend
Karsten Thoms
 
PPTX
Introduction to es6
NexThoughts Technologies
 
PDF
Object Oriented PHP - PART-2
Jalpesh Vasa
 
PDF
PHP unserialization vulnerabilities: What are we missing?
Sam Thomas
 
PPTX
Async fun
💡 Tomasz Kogut
 
PDF
The Joy Of Ruby
Clinton Dreisbach
 
PDF
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Susan Potter
 
PDF
Hear no evil, see no evil, patch no evil: Or, how to monkey-patch safely.
Graham Dumpleton
 
React Native One Day
Troy Miles
 
Découvrir dtrace en ligne de commande.
CocoaHeads France
 
LINQ Inside
jeffz
 
Intro to React
Troy Miles
 
5 Tips for Better JavaScript
Todd Anglin
 
How much performance can you get out of Javascript? - Massimiliano Mantione -...
Codemotion
 
Object Oriented Exploitation: New techniques in Windows mitigation bypass
Sam Thomas
 
Connect.Tech- Swift Memory Management
stable|kernel
 
DjangoCon US 2011 - Monkeying around at New Relic
Graham Dumpleton
 
Introduction to Asynchronous scala
Stratio
 
Recipes to build Code Generators for Non-Xtext Models with Xtend
Karsten Thoms
 
Introduction to es6
NexThoughts Technologies
 
Object Oriented PHP - PART-2
Jalpesh Vasa
 
PHP unserialization vulnerabilities: What are we missing?
Sam Thomas
 
The Joy Of Ruby
Clinton Dreisbach
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Susan Potter
 
Hear no evil, see no evil, patch no evil: Or, how to monkey-patch safely.
Graham Dumpleton
 

Similar to What To Expect From PHP7 (20)

PDF
The why and how of moving to php 8
Wim Godden
 
PDF
The why and how of moving to php 7
Wim Godden
 
PPTX
Php7 HHVM and co
weltling
 
ODP
The why and how of moving to php 5.4
Wim Godden
 
PPTX
Php7 hhvm and co
Pierre Joye
 
PPTX
Migrating to PHP 7
John Coggeshall
 
PDF
The new features of PHP 7
Zend by Rogue Wave Software
 
PDF
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
Codemotion
 
PDF
Php7 傳說中的第七隻大象
bobo52310
 
PDF
Last train to php 7
Damien Seguy
 
PPTX
PHP from soup to nuts Course Deck
rICh morrow
 
PDF
Introduction to PHP - Basics of PHP
wahidullah mudaser
 
PPT
Php manish
Manish Jain
 
PDF
Winter%200405%20-%20Beginning%20PHP
tutorialsruby
 
PDF
Winter%200405%20-%20Beginning%20PHP
tutorialsruby
 
PDF
Php 7 compliance workshop singapore
Damien Seguy
 
PDF
Giới thiệu PHP 7
ZendVN
 
PDF
Php 7.2 compliance workshop php benelux
Damien Seguy
 
PPTX
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
The why and how of moving to php 8
Wim Godden
 
The why and how of moving to php 7
Wim Godden
 
Php7 HHVM and co
weltling
 
The why and how of moving to php 5.4
Wim Godden
 
Php7 hhvm and co
Pierre Joye
 
Migrating to PHP 7
John Coggeshall
 
The new features of PHP 7
Zend by Rogue Wave Software
 
The new features of PHP 7 - Enrico Zimuel - Codemotion Milan 2016
Codemotion
 
Php7 傳說中的第七隻大象
bobo52310
 
Last train to php 7
Damien Seguy
 
PHP from soup to nuts Course Deck
rICh morrow
 
Introduction to PHP - Basics of PHP
wahidullah mudaser
 
Php manish
Manish Jain
 
Winter%200405%20-%20Beginning%20PHP
tutorialsruby
 
Winter%200405%20-%20Beginning%20PHP
tutorialsruby
 
Php 7 compliance workshop singapore
Damien Seguy
 
Giới thiệu PHP 7
ZendVN
 
Php 7.2 compliance workshop php benelux
Damien Seguy
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 
Ad

More from Codemotion (20)

PDF
Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
Codemotion
 
PDF
Pompili - From hero to_zero: The FatalNoise neverending story
Codemotion
 
PPTX
Pastore - Commodore 65 - La storia
Codemotion
 
PPTX
Pennisi - Essere Richard Altwasser
Codemotion
 
PPTX
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
Codemotion
 
PPTX
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
Codemotion
 
PPTX
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Codemotion
 
PPTX
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Codemotion
 
PDF
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Codemotion
 
PDF
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Codemotion
 
PDF
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Codemotion
 
PDF
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Codemotion
 
PDF
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Codemotion
 
PDF
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Codemotion
 
PPTX
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Codemotion
 
PPTX
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
Codemotion
 
PDF
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Codemotion
 
PDF
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Codemotion
 
PDF
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Codemotion
 
PDF
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Codemotion
 
Fuzz-testing: A hacker's approach to making your code more secure | Pascal Ze...
Codemotion
 
Pompili - From hero to_zero: The FatalNoise neverending story
Codemotion
 
Pastore - Commodore 65 - La storia
Codemotion
 
Pennisi - Essere Richard Altwasser
Codemotion
 
Michel Schudel - Let's build a blockchain... in 40 minutes! - Codemotion Amst...
Codemotion
 
Richard Süselbeck - Building your own ride share app - Codemotion Amsterdam 2019
Codemotion
 
Eward Driehuis - What we learned from 20.000 attacks - Codemotion Amsterdam 2019
Codemotion
 
Francesco Baldassarri - Deliver Data at Scale - Codemotion Amsterdam 2019 -
Codemotion
 
Martin Förtsch, Thomas Endres - Stereoscopic Style Transfer AI - Codemotion A...
Codemotion
 
Melanie Rieback, Klaus Kursawe - Blockchain Security: Melting the "Silver Bul...
Codemotion
 
Angelo van der Sijpt - How well do you know your network stack? - Codemotion ...
Codemotion
 
Lars Wolff - Performance Testing for DevOps in the Cloud - Codemotion Amsterd...
Codemotion
 
Sascha Wolter - Conversational AI Demystified - Codemotion Amsterdam 2019
Codemotion
 
Michele Tonutti - Scaling is caring - Codemotion Amsterdam 2019
Codemotion
 
Pat Hermens - From 100 to 1,000+ deployments a day - Codemotion Amsterdam 2019
Codemotion
 
James Birnie - Using Many Worlds of Compute Power with Quantum - Codemotion A...
Codemotion
 
Don Goodman-Wilson - Chinese food, motor scooters, and open source developmen...
Codemotion
 
Pieter Omvlee - The story behind Sketch - Codemotion Amsterdam 2019
Codemotion
 
Dave Farley - Taking Back “Software Engineering” - Codemotion Amsterdam 2019
Codemotion
 
Joshua Hoffman - Should the CTO be Coding? - Codemotion Amsterdam 2019
Codemotion
 
Ad

Recently uploaded (20)

PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PPTX
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
PPTX
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
PDF
Web Hosting for Shopify WooCommerce etc.
Harry_Phoneix Harry_Phoneix
 
PPTX
英国假毕业证诺森比亚大学成绩单GPA修改UNN学生卡网上可查学历成绩单
Taqyea
 
PDF
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
PPT
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
PPTX
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
PPTX
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
PPTX
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PPTX
本科硕士学历佛罗里达大学毕业证(UF毕业证书)24小时在线办理
Taqyea
 
PDF
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
PPTX
internet básico presentacion es una red global
70965857
 
PPTX
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
PPTX
INTEGRATION OF ICT IN LEARNING AND INCORPORATIING TECHNOLOGY
kvshardwork1235
 
PPTX
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
PPT
introduction to networking with basics coverage
RamananMuthukrishnan
 
PPTX
Orchestrating things in Angular application
Peter Abraham
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
Web Hosting for Shopify WooCommerce etc.
Harry_Phoneix Harry_Phoneix
 
英国假毕业证诺森比亚大学成绩单GPA修改UNN学生卡网上可查学历成绩单
Taqyea
 
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
Computer Securityyyyyyyy - Chapter 1.ppt
SolomonSB
 
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
本科硕士学历佛罗里达大学毕业证(UF毕业证书)24小时在线办理
Taqyea
 
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
internet básico presentacion es una red global
70965857
 
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
INTEGRATION OF ICT IN LEARNING AND INCORPORATIING TECHNOLOGY
kvshardwork1235
 
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
introduction to networking with basics coverage
RamananMuthukrishnan
 
Orchestrating things in Angular application
Peter Abraham
 

What To Expect From PHP7

  • 2. PHP 7: What To Expect Lorna Mitchell, CodeMotion Rome 2016     Slides available online, help yourself: http://www.lornajane.net/resources
  • 3. Versions of PHP Version Support until Security fixes until PHP 5.5 (expired) 10th July 2016 PHP 5.6 31st December 2016 31st December 2018 PHP 7.0 3rd December 2017 3rd December 2018   see also: http://php.net/supported-versions.php
  • 4. PHP 7 Is Fast
  • 5. PHP 7 Is Fast
  • 6. Why PHP 7 Is Fast • Grew from the phpng project • Influenced by HHVM/Hacklang • Major refactoring of the Zend Engine • More compact data structures throughout • As a result all extensions need updates • http://gophp7.org/gophp7-ext/   Rasmus' stats: http://talks.php.net/fluent15#/6
  • 7. Abstract Syntax Trees PHP 7 uses an additional AST step during compilation     This gives a performance boost and much nicer architecture
  • 9. Combined Comparison Operator The <=> "spaceship" operator is for quick greater/less than comparison.   1 echo 2 <=> 1; // 1 2 echo 2 <=> 3; // -1 3 echo 2 <=> 2; // 0
  • 10. Ternary Shorthand Refresher on this PHP 5 feature: 1 echo $count ? $count : 10; // 10 2 echo $count ?: 10; // 10
  • 11. Null Coalesce Operator Operator ?? is ternary shorthand (?:) but with isset().   1 $b = 16; 2 3 echo $a ?? 2; // 2 4 echo $a ?? $b ?? 7; // 16
  • 12. Type Hints PHP 5 has type hinting, allowing you to say what kind of parameter is acceptable in a method call.   1 function sample(array $list, $length) { 2 return array_slice($list, 0, $length); 3 }
  • 13. Type Hints If we use the wrong parameter types, it errors 1 print_r(sample(3, 3));   PHP 5 error: Catchable fatal error: Argument 1 passed to sample() must be of the type array, integer given   PHP 7 error: Fatal error: Uncaught TypeError: Argument 1 passed to sample() must be of the type array, integer given
  • 14. Scalar Type Hints PHP 7 lets us hint more datatypes: • string • int • float • bool
  • 15. Scalar Type Hints We can amend our code accordingly: 1 function sample(array $list, int $length) { 2 return array_slice($list, 0, $length); 3 }   And then call the method: 1 $moves = ['hop', 'skip', 'jump', 'tumble']; 2 print_r(sample($moves, "2")); // ['hop', 'skip']
  • 16. Scalar Type Hints To enable strict type check, add this line in the calling context: declare(strict_types=1);
  • 17. Return Type Hints We can also type hint for return values. 1 function sample(array $list, int $length): array { 2 if($length > 0) { 3 return array_slice($list, 0, $length); 4 } 5 return false; 6 }
  • 18. Return Type Hints This works: 1 $moves = ['hop', 'skip', 'jump', 'tumble']; 2 print_r(sample($moves, "2")); // ['hop', 'skip']   This because false fails the return typehint 1 $moves = ['hop', 'skip', 'jump', 'tumble']; 2 print_r(sample($moves, 0)); Fatal error: Uncaught TypeError: Return value of sample() must be of the type array, boolean returned
  • 19. Exceptions and Errors PHP 5 exceptions are alive, well, and excellent
  • 20. Exceptions in PHP 7 They now implement the Throwable interface
  • 21. Errors in PHP 7 Some errors are now catchable via the Error class
  • 22. Catching Exceptions and Errors 1 function sample(array $list, int $length) { 2 throw new Exception("You fail"); 3 } 4 5 try { 6 $a = sample(1,1); 7 } catch (Exception $e) { 8 echo "you hit the exception line"; 9 } catch (TypeError $e) { 10 echo "you passed the wrong arguments"; }
  • 23. Catch Method Calls on Non-Objects Does this error look familiar? 1 $a = 6; 2 $a->grow();   PHP 5: Fatal error: Call to a member function grow() on integer   PHP 7: Fatal error: Uncaught Error: Call to a member function grow() on integer
  • 24. Catch Method Calls on Non-Objects PHP 7 allows us to catch Errors as well as Exceptions 1 try { 2 $a = 6; 3 $a->grow(); 4 } catch (Error $e) { 5 echo "(oops! " . $e->getMessage() . ")n"; 6 // now take other evasive action 7 } Newer bits of PHP will use this new Error mechanism
  • 25. Random* Functions PHP 7 introduces a couple of neat randomness functions: • random_bytes() — Generates cryptographically secure pseudo-random bytes • random_int() - Generates cryptographically secure pseudo-random integers   For PHP <7 use https://github.com/paragonie/random_compat
  • 27. Uniform Variable Syntax This is a feature as well as a gotcha. • Good news: more consistent and complete variable syntax with fast parsing • Bad news: some quite subtle changes from old syntax when dereferencing or using $$ • If in doubt, add more { and }   RFC: https://wiki.php.net/rfc/uniform_variable_syntax
  • 28. Phan Static analyser: https://github.com/etsy/phan • reads code and PHPDoc comments • warns about BC breaks including uniform variable syntax issues • warns you about undeclared things • checks parameter types   Includes a great guide to codebase wrangling: http://lrnja.net/1W2Gjmb
  • 29. Foreach Check that you're not relying on any foreach() weirdnesses • The array pointer will no longer move, look out for use of current() and next() inside a foreach() loop • Don't assign to the thing you're looping over, the behaviour has changed   RFC: https://wiki.php.net/rfc/php7_foreach
  • 30. Deprecated Features You should expect things that trigger E_DEPRECATED in older versions of PHP to be removed.   Caveats: • The RFC to remove things was agreed but it hasn't been implemented yet • The mysql_* functions really are removed • PHP 4 constructors are less removed than you'd expect them to be
  • 31. Upgrading to PHP 7 Step 1: Upgrade to PHP 5.5 or 5.6.
  • 32. Upgrading to PHP 7 Step 1: Upgrade to PHP 5.5 or 5.6.   Most PHP 5 code will just work with a few pitfalls to look out for.   You probably want to run composer update while you're at it
  • 33. Upgrading to PHP 7 There are fabulous comprehensive instructions http://php.net/manual/en/migration70.php   Making the business case for PHP 7 • calculate hardware cost saving • calculate developer time required   Done :)
  • 34. Acquiring PHP 7 Windows users: get a new binary   Linux users: • wait for your distro to update • use an alternative source (e.g. http://lrnja.net/1PIPw2M) • compile it yourself
  • 36. The Future • PHP 5.6 support has been extended • Support until 31st December 2016 • Security fixes until 31st December 2018 • PHP 7.0 is safe to run • PHP 7.1 looks even better   (see also http://php.net/supported-versions.php)
  • 37. Questions? Slides are on http://lornajane.net (related blog posts are there too)   Contact me • [email protected] • @lornajane   This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.