What are the features introduced in php?

Hurray! PHP 7 is finally out there and to celebrate the release, I created a video that highlights a couple of interesting PHP 7 features.

Contents

  • 1 New PHP 7 features: a quick overview
  • 2 Scalar type hints
  • 3 Return type declarations
  • 4 Anonymous classes
  • 5 The Closure::call() method
  • 6 Generator delegation
  • 7 Generator return expressions
  • 8 The null coalesce operator
  • 9 The space ship operator
  • 10 Throwables
  • 11 Level support for the dirname() function
  • 12 The Integer division function
  • 13 Uniform variable syntax
  • 14 Performance
  • 15 We’re PHP 7 ready

New PHP 7 features: a quick overview

Have a look at the video, it highlights a couple of PHP 7 features I like. Here’s the list of stuff I cover:

  • Scalar type hints
  • Return type declarations
  • Anonymous classes
  • The Closure::call() method
  • Generator delegation
  • Generator return expressions
  • The null coalesce operator
  • The space ship operator
  • Throwables
  • Level support for the dirname() function
  • The Integer division function
  • Uniform variable syntax

Scalar type hints

Type hints have been available in PHP for while now. Unfortunately they were restricted to classes, arrays and callables.

As of PHP 7, the scalar types (integers, floating point numbers, booleans and strings) can also be used as type hints.

I’m happy with this PHP 7 feature, because it allows developers to ensure a better input consistency of a function/method interface. By default “coercive mode” is enabled. This restricts PHP from throwing a type error when the types don’t exactly match, but when a conversion is still possible.

If you enable “strict mode” (by uncommenting line 6), a type error is thrown when the signatures don’t match.

Return type declarations

Whereas type hints ensure input consistency, return type declarations ensure output consistency.

We use a colon before the opening curly brace of a function to hint the return type.

The same strictness rules apply as with the type hints: if “strict mode” is disabled, return values that can be converted to the preferred type are allowed. If you enable “strict mode” this code will throw a type error.

Anonymous classes

Anonymous classes are useful for simple one-off objects. With anonymous classes you can define a class and instantiate an object inline.

The Closure::call() method

Closures are anonymous functions that are declared inline and assigned to a variable. It can be used as a callback for later execution. In PHP 5 it was already possible to bind an object to the scope of the closure as if it was a method.

The “call” method is one of the PHP 7 features that was introduced to simplify the process.

Generator delegation

Generators are cool, but sometimes hard to explain. Ironically using them is surprisingly simple. It’s all about the “yield” keyword. By using “yield”, a generator is returned with the value that is yielded. A generator implements an iterator which makes it easy to use in while or for loops.

In PHP 7 generator delegation was introduced. This means a generator from another function can be addressed.

Generator return expressions

As mentioned: by using the yield keyword, values can be yielded and iterator over. But return values are ignored by generators.

In PHP 7 the “getReturn” method was added to the Generator class to allow return values in generators.

The null coalesce operator

The null coalesce operator is a shorthand for checking if a value is set and not null within inline comparisons. Instead of doing the same old “isset” check over and over again, just use “??” to return the value if it is set (and not null) or an alternative value instead.

The space ship operator

The so-called “space ship operator” makes it easier to compare values. Instead of returning a typical true/false value, the space ship operator returns one of the follow values based on the result of the evaluation:

  • 0 when both values are equal
  • -1 when the left value is less than the right value
  • 1 if the left value is greater than the right value

Throwables

A big change in PHP 7 is the fact that errors are no longer raised the way they used to be raised. Errors now behave in a similar way as exceptions. They both inherit from the Throwable interface.

This means that errors can now be caught in a try/catch block. You can catch both exceptions and errors as Throwables, but you can also catch errors as Error objects.

There are event different kinds of errors we can catch:

  • ArithmeticError
  • AssertionError
  • DivisionByZeroError
  • ParseError
  • TypeError

This is also a backwards compatibility break because custom error handlers might not be triggered. If you have a parse error in an eval function, it will throw a ParseError instead of just returning false. Watch out!

Level support for the dirname() function

The dirname function is used more often than you would think. It is the ideal function to refer to directories in a relative way.

But when you want to go a couple of levels up, you end up nesting dirname calls and that will eventually lead to confusion.

As of PHP 7 the dirname function has a second argument that indicates how many levels your going up the directory tree. If you don’t enter a value, 1 is the default.

The Integer division function

Maybe not one the most important PHP 7 features, but still worth mentioning: the intdiv function returns the integer value of a division whereas regular divisions can result in a float being returned.

Uniform variable syntax

In PHP 7 the “uniform variable syntax” was introduced. This standard changes the way indirect access to array keys, properties and methods is evaluated. The PHP 7 interpretation enforces a strict left-to-right evaluation.

What are the features introduced in php?

This is also considered a backwards compatibility break. It is advised to change your code and use curly braces to enforce the evaluation order if you want your code to work on PHP 7.

Performance

Let’s not forget to mention that the performance of PHP 7 is supposed to be impressive. Some say that it’s twice as fast as PHP 5(.6) others use even more dazzling numbers. I’m confident that it perform great and I’m curious to see the average numbers once people start using it.

Some sources say that it even outperforms HHVM. But don’t forget the phrase lies, damn lies and statistics.

I’m excited, let’s see.

We’re PHP 7 ready

At Combell, the hosting company I work for, we have been testing PHP 7 for a while. We’re a big fan of the PHP 7 features. We already made PHP 7.0.0 RC8 available in our control panel and now we offer PHP 7.0.x on shared hosting.

If you have a Cloud server at Combell and you’re anxious to upgrade to PHP 7, please contact support.

But as mentioned: watch out, there are BC breaks.

What are the new features introduced in php7?

PHP 7.0 added support for scalar type declarations for types string (strings), int (integers), float (floating-point numbers), and bool (booleans). To demonstrate type declarations with an example, create a script (typedeclr.

What are the following features are included in php7?

PHP 7 features and improvements.
1 New PHP 7 features: a quick overview..
2 Scalar type hints..
3 Return type declarations..
4 Anonymous classes..
5 The Closure::call() method..
6 Generator delegation..
7 Generator return expressions..
8 The null coalesce operator..

Which of the following is new operator added in PHP 7?

PHP 7.0 adds a new comparison operator (<=>) to compare expressions. PHP 7.0 adds support for Unicode codepoint escape syntax, to convert an hexadecimal form to the corresponding UTF-8 encoded form. The use statement may group classes, functions, and constants even when imported from the same namespace.

What is the new in PHP 8?

PHP 8.0 is a major update of the PHP language. It contains many new features and optimizations including named arguments, union types, attributes, constructor property promotion, match expression, nullsafe operator, JIT, and improvements in the type system, error handling, and consistency. Upgrade to PHP 8 now!