What’s new in PHP 7.0

After almost two years of work PHP 7.0.0 was released on December 3rd 2015 (my birthday!).

It’s now almost a year old and so I thought I’d write a few notes about my experiences.

The first thing to be aware of is that it supersedes PHP 5. There is no PHP 6. Why? There has been a lot of talk about this but basically PHP 6 was built but never released mainly due to not containing any of the Unicode changes.

So, what’s new in PHP 7.0 over PHP 5.6?

Faster with less memory usage

PHP 7.0 is around 2x faster than PHP 5.6. Given that 25% of the web is running WordPress which uses PHP that’s got to be good! It also uses significantly less memory.

Type declarations – static and return

PHP 5 has had type declarations (aka type hints in PHP 5) for class, self, array and callable however PHP 7 adds declarations for bool, float, int and strings – both in function parameters and, also when you declare a function, in the return type.

e.g. function sum(int $a, int $b): int {

PHP does a check to make sure the arg

Note: if you try and run this code using PHP 5.6, you’ll get something like:

PHP Parse error:  syntax error, unexpected ':', expecting '{' in my_file.php on line 10

Error handling

On the subject of errors, one new feature with PHP 7 is that an exception is now thrown when a fatal and recoverable error occurs. In PHP 5.6 the PHP script would simply halt resulting in a blank screen. Now there’s a Throwable interface.

New Operators

Spaceship Operator ( <=> ) aka Combined Comparison Operator: this combines less than, equal, and greater than. Essentially it runs through these from left to right. One good use case is in sorting.

Null Coalesce Operator

$animal = $value ?? "Some random animal";

So, $animal becomes $value UNLESS $value is null in which case it gets the text on the right.

random numbers

We now have a secure way of generating random data which interfaces with the OS’ random number generator. Along with that comes two new functions: random_int and random_bytes.

A few notes that aren’t perhaps so relevant for developers:

  • PHP 7.0 uses an Abstract Syntax Tree (AST) which means the language (e.g. the if … then structures) are stored in a tree format which are then used to generate a Control Flow Graph (CFG)
  • Also, we now have Unicode support for emoji and international characters

OK, I’m sold – how do I install it?

If you’re installing on bare metal (e.g without a VM) then you’ll run into conflicts. e.g. /usr/bin/php can only link to one version.

One option which I like is to simply use Docker containers with the specific versions. Unfortunately, that’s a separate blog post. However, I’m posting a link below with some basic details.

Or you can use an online REPL such as https://repl.it/languages/php

References: 

PHP Releases: https://secure.php.net/releases/

PHP 7 Release:

http://php.net/releases/7_0_0.php

PHP 6: https://wiki.php.net/rfc/php6#no_good_reasons_not_to_skip_version_6

and

https://news.ycombinator.com/item?id=8786919

PHP 7 Changelog

https://secure.php.net/ChangeLog-7.php#7.0.0

Abstract Syntax Tree and Control Flow Graph

https://en.wikipedia.org/wiki/Abstract_syntax_tree

http://web.cs.iastate.edu/~weile/cs641/2.ProgramRepresentations.pdf

PHP 7 / Docker

https://hub.docker.com/_/php/ and

PHP 7 With Docker And Docker Compose

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *