PHP 7.1 New Features

PHP 7.1 New Features


PHP 7.1, the new minor adaptation of PHP is at long last here, with various new highlights, changes, and bug fixes. In this article, how about we take a gander at a portion of the great highlights in PHP 7.1. Like,


  • Nullable Types
  • Void Function
  • Symmetric Array Destructuring
  • Class Constant Visibility
  • Iterable Pseudo-Type
  • Multiple Exceptions Handling
  • Support For Key's In List
  • Negative String Offset
  • Support For AEAD
  • Convert Callables To Closures
  • Asynchronous Signal Handling
  • Support for Server HTTP/2
  • Stream Context Option

Nullable Types

Type affirmations for parameters and return esteem would now be able to be set apart as nullable by prefixing the sort name with a question mark. This connotes and additionally the predefined type, NULL can be passed as a parameter or returned as a value, separately.

<?phpfunction test(): ?string{
    return 
'PHP test';
}
var_dump(testReturn());

function 
testReturn(): ?string{
    return 
null;
}
var_dump(testReturn());

function 
test(?string $name)
{
    
var_dump($name);
}
test('PHP test');test(null);test();
?>

Output:
string(10) "PHP test"
NULL
string(10) "PHP test"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...



Void Function

A void return type has been presented. It is non-return type it means it will return nothing. Capacities announced with void as their arrival type should either overlook their arrival articulation through and through or utilize an unfilled return statement. Invalid is anything but a substantial return an incentive for a void capacity.

<?phpfunction swap(&$x, &$y): void{
    if (
$x=== $y) {
        return;
    }

    
$tmp $x;
    
$x$y;
    
$y$tmp;
}
$a 1;$b 2;var_dump(swap($a$b), $a$b);
?>

Output:
null
int(2)
int(1)


Symmetric Array Destructuring

The shorthand exhibit grammar ([]) may now be utilized to destructure clusters for assignments (counting inside foreach), as an option against to the existing list() language structure, which is as yet upheld.

<?php
$value
= [
    [
1'SK'],
    [
2'Jha'],
];
// list() stylelist($id1$name1) = $value[0];// [] style[$id1$name1] = $value[0];// list() styleforeach ($value as list($id$name)) {
    
// logic here with $id and $name}// [] styleforeach ($value as [$id$name]) {
    
// logic here with $id and $name}
?>


Class Constant Visibility

Support for indicating the perceivability of class constants has been included.

<?phpclass ConstDemo{
    const 
PUBLIC_CONST_A 1;
    public const 
PUBLIC_CONST_B 2;
    protected const 
PROTECTED_CONST 3;
    private const 
PRIVATE_CONST 4;
}
?>



Iterable Pseudo-Type

Another pseudo-type (like callable) called iterable has been presented. It might be utilized in parameter and return types, where it acknowledges either clusters or protests that actualize the Traversable interface. As for subtyping, parameter sorts of tyke classes may widen a parent's assertion of cluster or Traversable to iterable. With return types, youngster classes may limit a parent's arrival kind of iterable to cluster or a question that executes Traversable.

<?phpfunction iterator(iterable $iter)
{
    foreach (
$iter as $val) {
        
//
    
}
}
?>



Multiple Exceptions Handling

Different exceptions per getting square may now be determined to utilize the pipe character (|). This is valuable for when diverse special cases from various class chains of command are dealt with the equivalent.

<?phptry {
    
// some code} catch (FirstException SecondException $e) {
    
// handle first and second exceptions}
?>



Support For Key's In List

You would now be able to enters keys in list(), or its new shorthand [] linguistic structure. This empowers destructuring of array with non-whole number or non-successive keys.

<?php
$value 
= [
    [
"id" => 1"name" => 'SK'],
    [
"id" => 2"name" => 'Jha'],
];
// list() stylelist("id" => $id1"name" => $name1) = $value[0];// [] style["id" => $id1"name" => $name1] = $value[0];// list() styleforeach ($value as list("id" => $id"name" => $name)) {
    
// logic here with $id and $name}// [] styleforeach ($value as ["id" => $id"name" => $name]) {
    
// logic here with $id and $name}
?>



Negative String Offset

Support for negative string balances has been added to the string control capacities tolerating balances, and to string ordering with [] or {}. In such cases, a negative balance is deciphered just like a balance from the finish of the string.

<?php
var_dump
("abcdef"[-2]);var_dump(strpos("aabbcc""b", -3));
?>


Output:
string (1) "e"
int(3)

Comments

Popular posts from this blog

Auto generate invoice number in php with financial year format?

Future of PHP