Web Programming TutorialsTop 10 Features Of The Latest PHP Version

Top 10 Features Of The Latest PHP Version

Latest PHP Version

PHP7 has been been rolled out for quite time now and the development community can’t get enough of the new features rolled out with this release. This is by far the most anticipated release of PHP. This is because PHP has come up with major performance boost to the language along with new operators, functions, generators, anonymous classes, function return types and much more. Below we cover the most loved features of the PHP7 that are doing rounds in the Development Community.

Major Performance Boost to the PHP Engine
PHP now comes with a new Engine for PHP7. This is faster than the traditional Zend Engine for PHP5.6. Many benchmarks have proved a major performance boost for PHP scripts along with the much used CMSes like WordPress, Drupal, etc. The new PHP7 engine is said to be twice as fast as the traditional Zend Engine. In some cases, it is even more than 3 times faster.

Return Types for PHP Functions
This has been one of the major features of PHP7. As PHP is now concentrating more on its OOP usage, Return Types for functions is a must feature to have. Now you can specify Return Types for your functions, methods, generators as well as anonymous functions. This is an optional feature, it is up to the Developer’s discretion whether to specify the Return Type or not, keeping in mind backward compatibility of the code. A very simple example of the syntax is shown below:

function return_the_sum_as_string($num_one, $num_two): string{ 
	return $num_one + $num_two; 
var_dump(return_the_sum_as_string(1, 3)); # returns: string(1) "4" 

Scalar Type Declaration for PHP7 Functions
Another move by the PHP to make programs much more Type Safe is the feature called Scalar Type Declaration. You can now specify the types of the arguments being accepted by the PHP functions. Your supported types could be: integer, float, boolean, string, interfaces, array, callable. This is an important feature that helps in making PHP programs more type safe than ever. An example of the same is demonstrated below:

function return_the_sum(int $num_one, int $num_two){ 
	return $num_one + $num_two; 
var_dump(return_the_sum(1, 3.2)); # returns: int(4) 

Null Coalescing Operator
This new operator is basically an improvement over the traditional ternary operator. This operator cuts shorts the syntax when working with NULL values and the non exiting variables. The working of this operator is demonstrated in the example below:

echo "Greetings, " . ($name_not_found ?? "Sir") . "!"; # outputs: Greetings, Sir! 

The Spaceship Operator
This new PHP7 operator performs three checks at a time. It performs comparisons for equality, less than and greater than on the operands. When using this operator for comparisons, your code would be less cluttered, thus more clear. The usage of this operator is demonstrated below:

echo (1 <=> 1) . PHP_EOL; # outputs: 0 
echo (1 <=> 2) . PHP_EOL; # outputs: -1 
echo (2 <=> 1) . PHP_EOL; # outputs: 1 

Array Constants Declaration using define()
Support for array constants in PHP have been implemented since PHP5.6. Array constants were declared using the const keyword. Now in PHP7, you’ll be able to use the define() function to define your array constants.

define('superheroes', [ 
	'DC_heroes' => [ 
		'h1' => 'BATMAN', 
		'h2' => 'The Flash' 
	'Marvel' => [ 
		'h1' => 'Captain America', 
		'h2' => 'Black Widow' 
echo 'Greatest Superhero is ', superheroes['Marvel']['h2'], "\n"; # outputs: My Favorite is Black Widow 

Unlimited arguments to PHP7 functions
PHP7 comes with a cleaner syntax to pass unlimited parameters to a PHP function. Though there was support for this in the previous PHP versions as well. PHP7 however comes up with the clear and easy to understand syntax for the same. The usage of this new syntax is demonstrated in the example below:

function get_the_sum(int ...$numbers){ 
	return array_sum($numbers); 
echo get_the_sum(1,2,3,4,5,6,4,5,5,2,5,1); # outputs: 43 

Directory Levels in the dirname() function
dirname() function is probably one of the most used php functions of all time. This function returns the parent’s directory path by accepting the path of the current file or directory as a string. A new addition to this function is the ability to return paths of different levels. We can now request an upper level of the parent directory to be returned by passing the desired level number as a second argument to this function. This functionality is demonstrated in the example below:

echo dirname('/home/Desktop/php/php7/project-file.php', 2), PHP_EOL; # outputs: /home/Desktop/php 

PHP7 Integer Division function
Integer Division has been a problem with PHP 5 and was done by the hackish approach using type casting. Not any more. With PHP7, a new function is introduced especially for this purpose, the function name is intdiv(). The function simply accepts two parameters i.e. dividend and divisor and returns a quotient which is an integer. The working is demonstrated below:

echo intdiv(8, 3), PHP_EOL; # outputs: 2 

Anonymous Classes in PHP7
PHP7 introduces a way to create a classes without naming them. Objects can be straight away derived using these anonymous classes. These objects can be unset anytime. This is an important feature. The following example shows a way of creating a simple object out of an anonymous class:

$myself = new class("John Doe"){ 
	public function __construct($name){ 
		echo 'Greetings, ' . $name . '!' . PHP_EOL; 

This is of course not the complete list of the rolled out PHP7 features. With every minor release, PHP is releasing new features on regular basis.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -