A major turning point in PHP’s development was reached with the release of PHP 7, which enhanced functionality, security, and performance with a number of new features and advancements. PHP 7 was released in December 2015. In this article, we will learn PHP 7 new features, how they improve PHP applications’ capabilities and development experience.
In the website world, website speed always matters. Users take website loading too seriously. Most of the site they abandons because it takes more than 3 seconds to load. To overcome this, PHP7 came in existence.
HHVM (It is an open source virtual machine developed by a Facebook team. It compiles PHP into native machine code by using just-in-time technology (JIT)) was very popular before release of PHP 7.
Previously, PHP 5 uses Zend II but PHP 7 uses PHP-NG or Next Generation which is a brand new model of engine. It improves the performance as much as twice with optimized memory usage.
Scalar Type Declaration
It means to define any function declared with parameters or without parameter and return value in specific data type (means int, float, string or bool known as scalar type). It has two modes:
Coercive mode
In this type, we does not need to define in scalar data type. It is also known as default mode.
Example
function sum(int $x, int $y) { return $x + $y; } echo sum(5, ‘6’);
Answer will be: 11
In above example, we define $x & $y as integer type. When we call “echo sum(5, ‘6’)”, PHP changed implicitly ‘6’ to 6 and add with 5 & result came 11. Here, PHP interpreter ‘6’ automatically convert into 6(integer).
function sum(int …$p) { return array_sum($p); } echo sum(2,3,’6′);
Answer will be: 11
Strict Mode
In PHP7, a new directive came named “strict_types”. It takes value either 0 or 1. By default its value is zero, means it will never check strictly variables values. But if strict_types value is 1 then it will strictly check return type.
So, we have to use “declare(strict_types=1)” statement at top of the file.
Example
declare(strict_types=1); function sum(int $x, int $y) { return $x+$y; } echo sum(5, ‘6’);
In above example, we define $x & $y as integer type. When we call “echo sum(5, ‘6’)”, it will give fatal error “Uncaught TypeError: Argument 2 passed to sum() must be of the type integer”. Because here we have declared “declare(strict_types=1)” this statement.
Return Type Declaration
Previously, there was no option to define return type for function or methods in PHP.
declare(strict_types=1);
Example
ini_set(‘display_errors’, 1); function age() { return 26; } echo age();
Answer will be: 26
Above function will display 26. If i write “age is 26”, so it will return “age is 26”. Means, here we can define any type of data & it will not give any error to me. But When we want to declare a function which will return only integer value, then we have to define return type.
In PHP7, we can define return type for any function of method.
ini_set(‘display_errors’, 1); function age() : int { return ‘Welcome In PHP World’. } echo age();
Answer will be:
Uncaught TypeError: Return value of age() must be an instance of integer, string returned
Above example will give a Fatal error “Uncaught TypeError: Return value of age() must be an instance of integer, string returned”. It means, this function will only return integer value.
Null coalescing Operator
In PHP, we use ternary operator to match any condition, if condition match then we display some content & if not match then we display other content.
Example
$result = isset($_GET[’email’]) ? $_GET[’email’] : ‘default@default.com’;
Answer will be: default@default.com
To reduce above code in PHP7,
$result = $_GET['email'] ?? "default@default.com";
Answer will be same: default@default.com
Constant Array
In PHP, we use define keyword & const used to define constant variable and assign some value to it.
Example
define(‘STUDENT’, ‘Ajay’); echo STUDENT;
Answer will be: Ajay
In PHP7, we can define array value as constant.
define('STUDENT', ['Amit', 'Sumit', 'Kamla']); echo STUDENT[0];
Answer will be: Amit
Also read about Error Handing In PHP
Anonymous Classes
The word Anonymous means unnamed, means which class have no names is called Anonymous Classes.
Normally, when we define any class in PHP, then it contains at least name of class. But in this type of class, no need to define the class name. We used new class Keyword to define Anonymous class in PHP7.
Suppose, we have a class and it will be used only one time during execution. Then in this situation, we need not to define a class as separate file. We can define it as Anonymous class. It saves our time.
Example
$message = new class{ public function display() { echo 'Welcome To Anonymous Class'; } }; $message->display();
Answer will be: Welcome To Anonymous Class
We can also used constructor in Anonymous Classes.
Error Handling
In PHP5, when any fatal error comes, it stops the execution of script & display blank screen in front of user. In this situation, we became confuse that what is the problem in our code.
But PHP7 has introduce a new feature, where we can display an exception to user, rather than stopping the whole script.
We should not think that Fatal errors are fully gone from PHP7. It still exists to display other type of errors like running out of memory. It also shows fatal errors & stop the script, when these type of errors came.
Example
function sum(int $x, int $y) { return $x + $y; } try { echo sum(‘abc’, ‘def’); } catch(TypeError $e) { echo $e->getMessage(); }
Answer will be:
Argument 1 passed to sum() must be of the type integer, string given
Here one thing we should keep in mind that other error types like warning and notice still works same as previous. Only fatal and recoverable errors throw exceptions.
CSPRNG
It stands for Cryptographically Secure PseudoRandom Number Generator.
In previous version of PHP, generally we use rand() to generate random number in integer. In PHP7, introduced two new functions to generate random numbers named random_bytes() & random_int().
Function Name | Description |
random_bytes | In this function, we always gives a single argument length, which is the length of random string. |
random_int | In this function, we always gives two parameter one is min and other is max. |
Example
$bytes = random_bytes(4); var_dump(bin2hex($bytes));
Answer will be: string(8) “b3c110ee”
echo random_int(10000, 99999);
It always returns a random number between min and max parameters.
Numerous innovations and enhancements brought about by PHP 7 increase PHP’s general capabilities, security, and efficiency. PHP 7 gives programmers strong tools to create more dependable and efficient code, from scalar type declarations and the null coalescing operator to enhanced error handling and the addition of the spaceship operator. Because of these improvements, PHP 7 is a strong option for both new projects and updating old programs.