PHP is everywhere, with estimates saying that, of the server-side language that is known, 82.6% of websites use PHP and about 95.3% of those are using PHP 5 according to W3Techs. It’s been 23 years since PHP was born, and we’re now at version 7.1.3. Tweaks and changes along the way are working to enhance performance (i.e., speed) and make coding more precise and efficient. If you’re part of the group that is still in earlier versions, it may be time to move up to PHP 7. And no, you didn’t miss 6. One of PHP’s core developers Andrei Zmievski wanted to bring native Unicode support to the language by embedding the ICU library and internally representing strings as UTF-16. Because this project would lead to major internal and user-affecting changes, it was planned to be the next version, PHP 6. However, using UTF-16 as default encoding takes a lot of time and memory, and many developers turned to the 5.2/5.3 branches or didn’t use it at all. This caused a delay in the project and since much was already written about 6, it was decided by vote in 2014 to move on to version 7.
Now that we’re a few versions into 7, let’s take a look at some of the features.
Now you can specifically declare nullable types which allows you to make the intent clear of what you are planning to do with variables or functions which makes the code easy to understand. It also enables error handling in a better way.
In earlier versions, it was expected that every function is returning something of some value, but void function allows you to write functions that are logical implementations of something and not return any value. The advantage of a void function is that it’s clear that you aren’t expecting anything from it which makes error handling better. Most programming languages already have void function and now PHP is up to speed in that regard.
SYMMETRIC ARRAY DESTRUCTURING
You can now use square bracket syntax to access elements that you would previously do with a list function. The advantage is you can directly loop to any variables without having to use list function. You can also use indexes in the square bracket syntax to map key value array easily. This makes the code more precise and concise.
In addition to many data types, the Iterable pseudo-type allows you to specify that the elements can be iterate inside of a function. This allows for better error handling because you know exactly what type of data that you are getting.
MULTI-CATCH EXCEPTION HANDLING
With this new feature, you can add multiple catchers which allows you to catch multiple errors, syntax, or exceptions. This enables you to be more efficient and it makes debugging easier.
SUPPORT FOR NEGATIVE STRING OFFSETS
In earlier versions of PHP, it didn’t allow negative offsets, you could use only positive integers. Now you can specify negative offsets so that you can grab what you want with more precision.
CONVERT CALLABLES TO CLOSURES
The benefit of callables to Closures is that you can grab errors where they are happening instead of where they are being called. This allows for completing the transaction without leaving anything in the global scope.
ASYNCHRONOUS SIGNAL HANDLING
Most people don’t usually get to this level, but if you do just know that this is a trigger that doesn’t interrupt the flow of the script. This was done previously with ticks, but it was resource heavy.
HTTP/2 SERVER PUSH SUPPORT IN EXT/CURL
This enhancement will decrease page load times to First Byte since with HTTP/2 the server can detect assets and requests and push them all at once.
Ready to make the move to version 7? If you’re still in version 5, the jump may be a big one, but will be well worth it. Support for 5.5 has ended so now is the time. PHP is gearing up to compete with the other languages, and you’ll want to experience all of the new features that make development enjoyable.
Please contact us if you are interested in exploring how to form an effective digital strategy and execute that strategy with an experienced web development firm.