What is new in PHP 7

What is New in PHP 7

PHP 7 is introduced as the next version of PHP.

The earlier one is PHP 5.6. The latest version is scalable and more flexible, faster and easy to implement complex logic.

Many new features are introduced with PHP 7.

These features are equally useful for the interview aspirants. Few of them are explained :

PHP 7 Features

  • Scalar type declarations
  • Return type declarations
  • Null coalescing operator
  • Spaceship operator
  • Constant arrays using define()
  • Anonymous classes
  • Unicode codepoint escape syntax
  • Closure::call()
  • Group use declarations

Scalar type declarations

There may be two scalar types of declarations :

  • coercive(default)
  • strict

We can enforce the types for parameters either coercively or strictly are :

  • strings (string)
  • integers (int)
  • floating-point numbers (float)
  • booleans (bool)

For Example in Coercive mode:

<?php
	function addTheNumbers(int ...$ints)
	{
	    return array_sum($ints);
	}
	var_dump(addTheNumbers(3, '7', 7.8));
?>

Declaration of Return Type

PHP 7 started support for return type while declaring a function.
The return type declarations specify the type of value that will be returned after execution.

For example :

<?php
function sumOfArrays(array ...$arrays): array
{
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);
}

print_r(sumOfArrays([10,20,30], [14,15,16], [17,18,19]));
?>

In the above function, the accepted argument are arrays and the return type of the function is also an array.

Null coalescing operator

This is a comparison and validation operator added to PHP 7 and It is used as a ternary in conjunction with isset() function.
If the expression exists and not NULL then it returns the first operator else it returns the second one.

For example :

<?php
	$booktitle = $_GET['title'] ?? 'no_books';
?>

If the title is set in $_GET and the value of the $_GET[‘title’] is NOT NULL then the value of $booktitle will be the $_GET[‘title’]. In a condition of failure the title will be ‘no_books’.

Spaceship operator

This is a pure comparison operator and compares the values of two variables. It returns -1, 0 and 1. It compares less than, equal to and greater than.

Have a look at examples :

<?php 
// Integers
echo 7 <=> 7; // 0
echo 7 <=> 8; // -1
echo 8 <=> 7; // 1

// Floats
echo 7.5 <=> 7.5; // 0
echo 7.5 <=> 8.5; // -1
echo 8.5 <=> 7.5; // 1

// Strings
echo "b" <=> "b"; // 0
echo "b" <=> "c"; // -1
echo "c" <=> "b"; // 1
?>

Constant arrays using define()

In PHP 7 we can define an array as a constant, like strings and numbers. It can be done using the define() function.

<?php
define('SUPER_HEROS', [
    'Iron Man',
    'Thor',
    'Captain America',
    'Spidar Man'
]);

echo SUPER_HEROS[1]; // outputs "Thor"
?>

Anonymous classes

PHP 7 has introduced support for Anonymous classes. Anonymous classes can be used in place of the full class definition for throwaway objects.

<?php
interface Logger {
    public function log(string $msg);
}

class Application {
    private $logger;

    public function getLogger(): Logger {
         return $this->logger;
    }

    public function setLogger(Logger $logger) {
         $this->logger = $logger;
    }
}

$app = new Application;
$app->setLogger(new class implements Logger {
    public function log(string $msg) {
        echo $msg;
    }
});

var_dump($app->getLogger());
?>

Unicode codepoint escape syntax

A valid Unicode codepoint character can be converted to UTF-8 corresponding character. All the leading 0’s will be neglected.

<?php 
	echo "\u{aa}";
	echo "\u{0000aa}";
	echo "\u{9999}";
?>

The above example will output:

ª
ª (same as before but with optional leading 0’s)

Closure::call()

Closure::call() is a way of temporarily binding an object scope to a closure.


<?php class MyClass { private $val = 1; } $getVal = function() {return $this->val;}; 
echo $getVal->call(new MyClass);
?>

Group use declarations

Classes, functions, and constants being imported from the same namespace can now be grouped together in a single-use statement.

<?php
	use some\namespace\{ClassA, ClassB, ClassC as C};
	use function some\namespace\{fn_a, fn_b, fn_c};
	use const some\namespace\{ConstA, ConstB, ConstC};
?>

Here we don’t need to import one by one, we can import all the classes in a single call. Similarly the functions and constants also.

Now we are able to understand the new updates in PHP 7. Hope this will help. Happy Learning 🙂

Yadav Dhananjay

Yadav Dhananjay

I’m Dhananjay — a free-spirit with a passion for technologies and blogging.

View all posts by Yadav Dhananjay →

3 thoughts on “What is New in PHP 7

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.