These are my PHP highlights from each release. PHP do such a great job moving the language forward, and documenting all these changes but I always forget all the cool new things I can use so I've documented the most interesting ones for me here.
Shortcuts: 7.2 - 7.1 - 7.0 - 5.6 - 5.5 - 5.4 - 5.3
PHP 7.2
New Features
- New
object
type -function tweet(object $message): object
[changelog] - Sodium added as a core extension, allowing better and easier encryption/decryption [changelog]
- Argon2 added as a new hash in the password hashing API [changelog]
Extras
- The
$additional_headers
parameter ofmail()
andmb_sendmail()
now also accepts anarray
instead of astring
[changelog]
Backwards Incompatible Changes
object
is now a hard-reserved word and cannot be used as a class, trait or interface name [changelog]
Deprecations
__autoload
has been deprecated in favour ofspl_autoload_register
[changelog]
PHP 7.1
New Features
- Nullable types -
function tweet(?string $messageOverride): ?TweetResult
[changelog] - Short-hand array destructuring -
[$latitude, $longitude] = $locations[42];
[changelog] -
- This can also be used in
foreach
:foreach ($locations as [$latitude, $longitude]) {
- This can also be used in
iterable
pseudo-type -function tweetMessages(iterable $messages)
[changelog]- Asynchronous signal handling [changelog]
pcntl_async_signals(true); // turn on async signals
pcntl_signal(SIGTERM, function($sig) {
die("You can't terminate me! Do you know who I am?");
});
- Catch multiple exceptions [changelog]
try {
$tweeter->tweet($messageOverride);
} catch (BadMessage | AccountBlocked | TooLong $e) {
Log::error('Oops, I did it again');
}
Backwards Incompatible Changes
void
anditerable
are now reserved keywords and cannot be used to name classes, traits or interfaces. [changelog]DateTime
andDateTimeImmutable
now properly incorporate microseconds -new DateTime() == new DateTime();
is likely to equalfalse
[changelog]
PHP 7.0
Migration Guide • Backwards Incompatible Changes
PHP 7.0 has the largest number of backwards incompatible changes which may require large changes to your codebase. That said, PHP 7.0 was a huge leap in functionality, speed and progress from PHP 5.6.
New Features
- Scalar type declarations -
function tweet(string $message)
-string
,int
,float
,bool
[changelog] (this is my favourite new feature) - Return type declarations -
function tweet(string $message): bool
[changelog] - Null coalescing operator -
$_GET['message'] ?? "Come up with your own message bro";
[changelog] - Unicode codepoint escape syntax -
echo "\u{32DB}";
will echo㋛
[changelog]
Backwards Incompatible Changes
mysql_
functions removed [changelog]- Many errors have been converted to exceptions and 'Error' exceptions inherit from the
Error
class [changelog] mssql_
functions removed [changelog]ereg_
functions removed [changelog]- Magic quotes removed -
set_magic_quotes_runtime()
andmagic_quotes_runtime()
[changelog] bool
,int
,float
,string
,NULL
,TRUE
,FALSE
cannot be used to name classes, interfaces or traits [changelog]preg_replace
no longer supports\e
PREG_REPLACE_EVAL [changelog]
Deprecations
- PHP 4 style constructors -
class Tweet { function tweet() { echo "Constructor"; }
[changelog]
PHP 5.6
New Features
- Variadic functions and splat operator -
function tweet (...$messages)
-$messages = ['I try to say goodbye', 'And I choke']; tweet(...$messages);
[changelog] php://input
is now reusable! Woo! [changelog]__debugInfo
magic method to determine what's shown when usingvar_dump
[changelog]
PHP 5.5
New Features
- Generators added [changelog]
- New password hashing API [changelog] - This is incredibly important for bringing added security to applications
- Array dereferencing -
echo ['Macy', 'Gray'][1];
==Gray
[changelog] - Addition of
::class
to get a class name [changelog] array_column
added - very useful function [documentation]json_last_error_msg()
added [documentation]
Deprecations
mysql_*
deprecated (Removed in PHP 7.0) [changelog]preg_replace
/e modifier (Removed in PHP 7.0) [changelog]
PHP 5.4
New Features
- Short array syntax -
$tweets = ['Short', 'Array', 'Syntax', 'Is', 'The', 'Best'];
[changelog] - This is one of my favourite features ever traits
were added allow 'copy and paste' of methods for classes [documentation]<?=
is always available for a quicker way of printing [changelog]
Backwards Incompatible Changes
- PHP 5.4
htmlspecialchars
andhtmlentities
assume the charset is UTF-8 by default and will return blank if the input is not valid UTF-8 [source] - Magic quotes has been removed requiring code changes [changelog]
- Cannot use super globals in function declarations -
function tweet($_GET, $_POST)
[changelog] - New reserved keywords
trait
,callable
,insteadof
[changelog] session_is_registered
,session_register
,session_unregister
have been removed [changelog]
I'll continue to update this as each new version is released (last updated 2018-08-08).