In this lecture I shall tell you about the upcoming “PHP 7 features“. As you all knows about PHP programming language. This language is mostly used in the development of websites and it acts like a bridge. Let’s discuss PHP 7 features.

Scalar Type Hints & Return Types

I am going to start with the most controversial change that was added in PHP 7 is Scalar Type Hints. This means is that you can now type-hint with scalar types. Specifically int, float, string, and bool. By default type-hints are non-strict, which means they will coerce the original type to the type specified by the type-hint. It means if you pass
“int(1)” into a function that requires a float, it will become “float(1.0)”. By passing “float(1.5)” into a function that requires an “int”, it will become “int(1)”.

Take a closer look on the below code:

The above function will return the below output.

As you can see the sum is shown as “9” while it would be 9.5. Due to the required integer type it displayed like this.

Furthermore, PHP 7 also supports Return Type Hints which support all the same types as arguments.

Combined Comparison Operator

The another addition to PHP 7 is the addition of the Combined Comparison Operator “<=>” which is known as the spaceship operator. I might be biased this operator is still a nice addition to the language, complementing the greater-than and less-than operators.
It effectively works like “strcmp()” or “version_compare()” returning “-1” if the left operand is smaller than the right. It will return “0” if both are equal and “1” if the left is greater than the right. The major difference being that it can be used on any “two operands” not just strings, but also on “integers” , “floats” ,  “arrays” , etc.

Take a closer look on the below code:

The spaceship operator is used for comparing two expressions. It returns an integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP’s usual type comparison rules.

Unicode Codepoint Escape Syntax

The addition of a new escape character  “\u” allows us to specify Unicode character code points (in hexidecimal) unambiguously inside PHP strings. The syntax used is \u{CODE}.

The below code will explain you that how it works.

This takes a Unicode codepoint in hexadecimal form, and outputs that codepoint in UTF-8 to a double-quoted string or a heredoc. Any valid codepoint is accepted, with leading 0’s being optional.

Null Coalesce Operator

The null coalesce 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.

Take a look on the below code:



PHP 7 Bind Closure on Call

PHP 7 now adding an easy way to do this at call time. It bind both “$this” and calling “scope” to the same object with the addition of “Closure->call()”. This method always takes the “object” as it’s first argument, followed by any arguments to pass into the closure.

Closure::call() is a more performant, shorthand way of temporarily binding an object scope to a closure and invoking it. Take a closer look on the below code:

The above example’s output is shown below:

This is how it works.

PHP 7 Group Use Declarations

If you’ve ever had to import “many classes” from the same “namespace” then you’ve probably been very happy
if your IDE will auto-complete them for you. For others, and for brevity, PHP 7 now has “Group Use Declarations”. This allows you to quickly specify many similar imports with better clarity.

I shall make it simple for you, take a closer look on below code:

This gives the ability to group multiple use declarations according to the parent namespace. This seeks to
remove code verbosity when importing multiple classes, functions, or constants that come under the same namespace.

Generators Improvements

There are more two new features added to generators in PHP 7.

a) Generator Return Expressions

This feature of PHP 7 builds upon the basis of generator functionality introduced into PHP 5.5. It enables for a return  statement to be used within a generator, it enable for a final expression to be returned (return by reference
is not allowed). This value can be fetched by using the “new Generator::getReturn()” method. It may only be used
once the generator has finishing yielding values.

Let’s see the below code to check that how it works:

The above example will give the below output:


Being able to explicitly return a final value from a generator is a handy ability to have. This is because it enables for a “final” value to be returned by a generator that can be specifically “handled” by the client code executing the generator. This is far more simpler than forcing the client code. Firstly check whether the final value has been yielded and then if it is then it will handle that value specifically.
b) Generator Delegation
Generator delegation builds upon the “ability” of being able to return expressions from generators in PHP 7. It does this by using an new “syntax” of yield from “<expr>” where “<expr>” can be any Traversable object or array. This <expr> will be no longer valid and then execution will continue in the calling generator. This feature enables yield statements to be broken down into smaller operations, thereby promoting cleaner code that has greater re-usability.

PHP 7 Engine Exceptions

Expectations are a backwards compatible enhancement to the older “assert()” function of PHP 7. They enable for zero-cost assertions in production code and provide the ability to throw custom exceptions on error. As with the “old API”, if the first argument is a “string”, then it will be evaluated. The second argument can either be a “plain string” or a custom exception “object” containing an error message.

Take a closer look on the below code:

This example will show the below message:

Filtered unserialize()

This feature of PHP 7 provides better security when unserializing objects on untrusted data. It prevents possible code “injections” by enabling the developer to “whitelist” classes that can be unserialized.

Take a closer look on below code:

Integer Division with intdiv()

The “intdiv()” function of PHP 7 has been introduced to handle “division” where an integer is to be returned. Take a closer look on below code:

This above code will return the below output:

session_start() Options

This feature of PHP 7 gives the ability to pass in an “array” of options to the “session_start()” function. This is used to set “session-based” php.ini options:

The above code sets the session.cache_limiter option to private. This feature of PHP 7 also introduces a new php.ini setting (session.lazy_write) that is, by default, set to true and means that session data is only rewritten if it changes.