PHP 5 vs. PHP 7
Let’s discuss the main differences between PHP 5 and PHP 7 in this blog.
1. Performance
Zend Engine (ZE) is the open source scripting engine that interprets and compiles PHP. In PHP 7, ZE has been refactored and improved, and it is called PHP Next Generation (PHPNG). PHPNG aims to provide better performance i.e. better throughput and memory utilisation:
2. Security
The final branch of PHP 5, PHP 5.6, has reached its end-of-life since 1st January 2019 (see list of supported PHP versions here). This means that PHP 5 no longer has active and security support by the PHP team. So, if a vulnerability is found, it will not get fixed, leaving your website vulnerable as well (source).
An example is file encryption/cryptography tools. PHP 5 is still using mcrypt while PHP 7 is using openssl. openssl is proven to have better security, performance, maintainability and portability than mcrypt (source). This is why it is important to keep your PHP version up-to-date.
3. Compatibility
Some modern PHP frameworks or extensions are only compatible with PHP 7. The latest version of Laravel (6.x), the most popular framework for PHP, requires PHP 7 and will not work with PHP 5. Laravel 5.4 is the latest version of Laravel that supports PHP 5 . So, if you want to use the latest version of Laravel, PHP 7 is preferable. On the other hand, there are some PHP extensions that might not be ready with PHP 7 yet.
Furthermore, some functions are going to strike a backward compatibility issue as they are deprecated in PHP 7 such as:
- each() → can be replaced with foreach()
- ereg() → can be replaced with preg_match()
- mysql_*() → can be replaced with mysqli_*()
- etc. (see PHP 5.6 to PHP 7.0, PHP 7.0 to PHP 7.1, PHP 7.1 to PHP 7.2 and PHP 7.2 to PHP 7.3 backward incompatible changes)
4. Parameter and Return Type
In PHP 7, you are now able to declare parameter and return type for a function:
public function formatPrice(float $price) : string
{
return '$' . number_format($price);
}
5. Spaceship Operator (<=>)
In PHP 5:
public function sort ($a, $b)
{
if ($a == $b) {
return 0;
} elseif ($a > $b) {
return 1;
} else {
return -1;
}
}
In PHP 7:
function sort ($a, $b)
{
return $a <=> $b;
}
The spaceship operator (<=>) automatically does a combined comparison:
- Return 0 if values on both sides are equal
- Return 1 if value on the left is greater
- Return -1 if value on the right is greater
More examples:
// Comparing integers
echo 1 <=> 1; // 0
echo 2 <=> 1; // 1
echo 1 <=> 2; // -1
// Comparing strings
echo 'a' <=> 'a'; // 0
echo 'b' <=> 'a'; // 1
echo 'a' <=> 'b'; // -1
6. Null Coalesce Operator (??)
In PHP 5:
if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = null;
}
Or, using shorthand ternary operator:
$id = isset($_GET['id']) ? $_GET['id'] : null;
In PHP 7:
$id = $_GET['id'] ?? null;
7. Anonymous Class
Anonymous classes are useful when simple and one-off (not reusable) objects need to be created.
In PHP 5:
class Math
{
public function square($number)
{
return $number * $number;
}
}$testHelper = $testHelper->setMath(new Math());
In PHP 7, we can convert the above Math class to an anonymous class:
$testHelper = $testHelper->setMath(new class {
public function square($number)
{
return $number * $number;
}
});
Anonymous classes can also have constructors, pass input arguments to their constructors, extend other classes, implement an interface and use traits just like normal classes:
interface TestInterface {}class TestClass {}trait TestTrait {}var_dump(new class('test') extends TestClass implements TestInterface {
use TestTrait; private $str; public function __construct($str)
{
$this->str = $str;
}
});
The above dump will output:
object(class@anonymous)#1 (1) {
["Command line code0x123456789":"class@anonymous":private]=>
string("test")
}
8. Escaping Unicode Characters
In PHP 5, if you try to print a unicode character:
echo "\u1000";
This will output:
\u1000
So, you have to do this instead:
echo json_decode('"\u1000"');// ORecho mb_convert_encoding('က', 'UTF-8', 'HTML-ENTITIES');
In PHP 7, we can easily escape unicode characters:
echo "\u{1000}";
This will output:
က
9. Group Import Declarations (use)
In PHP 5:
use TestLibrary\Test\Module\ClassA;
use TestLibrary\Test\Module\ClassB;
use TestLibrary\Test\Module\ClassC as Foo;
In PHP 7:
use TestLibrary\Test\Module\{ ClassA, ClassB, ClassC as Foo };