In this post you can find some of the new features coming with PHP 7. If you are upgrading from PHP 5.x then you can read my post “Things you should know when upgrading to PHP 7“.

Scalar type declarations

In the PHP 7 it is possible to specify function parameters as certain types (type hinting). If the given value is of the incorrect type, then TypeError exception is thrown. The following types can be enforced as parameters :

  • string
  • int
  • float
  • bool

To specify type declaration, just write type name before the parameter name something like int $parameterName.

Here are valid types:

Type Description Minimum PHP version
Class/interface name The parameter must be an instanceof the given class or interface name. PHP 5.0.0
self The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods. PHP 5.0.0
array The parameter must be an array. PHP 5.1.0
callable The parameter must be a valid callable. PHP 5.4.0
bool The parameter must be a boolean value. PHP 7.0.0
float The parameter must be a floating point number. PHP 7.0.0
int The parameter must be an integer. PHP 7.0.0
string The parameter must be a string. PHP 7.0.0

Return type declarations

PHP 7 adds support for return type declarations. Similarly to argument type declarations, return type declarations specify the type of the value that will be returned from a function. The same types are available for return type declarations as are available for argument type declarations.

PHP 7 allows you to define return type declarations. Return type declarations are specifying the type of the value that will be returned from a calling function. Types listed in the table above are available for return type.

Null coalescing operator

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

Spaceship operator

This operator is used for comparing two expressions, and it returns the following values -1, 0, 1. when $a is respectively less than, equal to, or greater than $b.

Constant arrays using define()

One fancy feature added in PHP 7 is ability of defining constant array values using define().

Group use declarations

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

Integer division with intdiv()

The new intdiv() function performs an integer division of its operands and returns it.

Anonymous classes

In PHP 7 you can define anonymous classes by using keyword new class. Anonymous class can be used  in place of full class definitions for throwaway objects.

New features in PHP 7 you should be aware ofhttp://code-epicenter.com/wp-content/uploads/2016/01/php72.jpghttp://code-epicenter.com/wp-content/uploads/2016/01/php72-150x150.jpgAmir DuranPHPProgrammingPHP,PHP 7
In this post you can find some of the new features coming with PHP 7. If you are upgrading from PHP 5.x then you can read my post 'Things you should know when upgrading to PHP 7'. Scalar type declarations In the PHP 7 it is possible to specify function parameters...