Thursday, September 12, 2019

PHP 7 New Features Part – 1


PHP 7 New Features


Here we are going to learn PHP 7 New Features Part – 1.

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, PHP 7 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.

New Feature Of PHP 7

1. 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.

For Example:
function sum(int $x, int $y){
return $x+$y;
}
echo sum(5, ‘6’);

Result: 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).


For Example:
function sum(int …$p){
return array_sum($p);
}
echo sum(2,3,’6′);

Result: 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.

For 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.

2. Return Type Declaration:
Previously, there was no option to define return type for function or methods in PHP.
declare(strict_types=1);

For Example:
ini_set(‘display_errors’, 1);
function age(){
      return 26;
}

echo age();

Result: 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 PHP 7, we can define return type for any function of method.

For Example:
ini_set(‘display_errors’, 1);
function age() : int
{
         return ‘Welcome In PHP World’.
}

echo age();

Result: 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.

3. 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.

For Example:
$result = isset($_GET[’email’]) ? $_GET[’email’] : ‘default@default.com’;

Result: default@default.com;

To reduce above code in PHP 7,
For Example:
$result = $_GET[’email’] ??’default@default.com‘;

Result will be same “default@default.com“;

4. Constant Array:
In PHP, we use define & const used to define constant variable and assign some value to it.

For Example:

define(‘STUDENT’, ‘Ajay’);
echo STUDENT;

Result: Ajay

In PHP 7, we can define array value as constant.

For Example:

define(‘STUDENT’, [‘Amit’, ‘Sumit’, ‘Kamla’]);
echo STUDENT[0];

Result: Amit

No comments:

Post a Comment