This is a guest contribution by Lucy Berrette
The new version of PHP launched on the market in 2014. The latest version of PHP 5.6 offers some of the nice features while other features have been removed from it.
Obviously, being a PHP developer, it is necessary to know everything about new versions. Through this post, I will introduce you the exciting features of PHP 5.6 version.
1. Constant Scalar Expressions
The latest version of PHP 5.6 offers a scalar expression that contains both numeric and string literals. However, in the former versions of PHP, it was expected to be a constant value of static function arguments and property declaration.
const ONE = 1; // Scalar Expression in constant const TWO = ONE * 2; class helloWorld { // Scalar Expression in Property const THREE = TWO + 1; // Scalar Expression in Methods public hello f($a = ONE + self::THREE) { return $a; } } echo (new helloWorld)->hello()."\n";
2. Variadic Functions
It allows you to seize a variable number of arguments to a function, merged with “normal arguments passed in. The following example will help you understand what I am saying:
function concatenate($transform, ...$strings) { $string = ''; foreach($strings as $piece) { $string .= $piece; } return($transform($string)); } echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");
A function declaration has the … operator in it and it means” … and other will go into $strings”. In other words, you can pass more than one arguments into this function and the second and next ones will be added to the $strings array.
3. Argument Unpacking
You can use the same operator (…) to remove any argument that is either an array or as a set of traversable objects. It gives a unique way of using functions that already exists, so it becomes necessary as soon as you upgrade to PHP 5.6.
If you want to use the argument unpacking, then just warn PHP that it requires to unpacking the array into variables by using … operator.
$email[] = "Hi there"; $email[] = "Thanks for registering, hope you like it"; mail("someone @ example .com" , ...$email);
You can pass all of your arguments in an array. A PHP will take your array and pass each of the aspects in as the next parameter in turn
4. It can upload Large File
With the introduction of the latest version of PHP 5.6, it becomes possible to upload a file larger than 2 GB.
5. You can use php://input multiple times
You can use php://input many times whenever you want to read data. It provides a great reduction in memory as compared to read Post Data.
Conclusion
In this post, we have discussed some of the primary features that are offered by the new version of PHP 5.6. This version has trucked into the market with the scope of improvements and feature addition. However, PHP always introduces compatible versions with every release. But, still this version offers the relevant functions that have grabbed the interest of many PHP developers.
About Lucy Berrette – Lucy Barret works for WPGeeks Ltd. as a HTML to WordPress Developer and manages all major projects with her team of developers. She enjoys writing tutorials and loves to eat Thai food.