Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

What's New in PHP 7

DZone's Guide to

What's New in PHP 7

In this article, we go over some of the new features introduced in PHP 7, and how to use these new additions in your code.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

The following are some of the new features PHP 7:

  • Scalar type declarations

  • Return type declarations

  • Null coalescing operator

  • Spaceship operator

  • Constant arrays using define()

  • Anonymous classes

  • Unicode codepoint escape syntax

  • Closure::call()

  • Filtered unserialize()

  • IntlChar

  • Expectations

  • Group use declarations

  • Generator Return Expressions

  • Generator delegation

  • Integer division with intdiv()

  • Session options

  • preg_replace_callback_array()

  • CSPRNG Functions

Scalar Type Declarations

Q: What are Scalar type declarations in PHP?

A: Type declarations are what allows the function to require certain types of parameters at call time. There are two types Scalar type declarations: one is coercive or you can say it default and other is strict.

Type parameters: strings (string), integers (int), floating-point numbers (float), and booleans (bool).

Default or Coercive Type

function returnsum(float $x, float $y) 
{
return $x + $y;
}

returnsum(6, "8 days");
//output float(14)

returnsum(4.1, "4.2");
//output float(8.3)

Strict Type

declare(strict_types=1);
function returnsum(float $a, float $b)
 {
return $a + $b;
}

returnsum(3.1, 2.1);
// output float(5.2)

returnsum(3, "2 days");
// Fatal error

Return Type Declarations

Q: Return type declarations?

A: Just like the Parameter Type Declaration, the Return Type Declaration is the type of value that you want to return from a function.

Basic or Default Type of Return Type Declarations

function add($a, $b): float {
return $a + $b;
}

var_dump(add(1, 2));
// output will be float(3)

Strict Type of Return Type Declarations

declare(strict_types=1);
function add($a, $b): int {
return $a + $b;
}

var_dump(add(1, 2));
var_dump(add(1, 2.5));
// output will be
// int(3)
// Fatal error

Null Coalescing Operator

Q: What is a Null coalescing operator and what are  the uses of it?

A: The null coalescing operator is represent edlike this ??. It's used to check if the value is set or null, or in other words, if the value is exists and not null, then it returns the first operand, otherwise it returns the second operand.

Example:

// Fetches the value of $_GET['username'] and returns 'not define'
// if it does not exist.
$username = $_GET['username'] ?? 'not define';
// This is same as to:
$username = isset($_GET['username']) ? $_GET['username'] : 'not define';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['username'], $_POST['username'], and
// 'not define'.
$username = $_GET['username'] ?? $_POST['username'] ?? 'not define';

Spaceship Operator

Q: What is the spaceship operator?

A: The Spaceship operator is represented like this <=>. It is used to compare two expressions and return -1, 0, 1 when one variable is less than, equal to, or greater than, as compared to the other variable.

Example:

// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

Constant Arrays Using define()

Q: What are constant arrays that use define()?

A: In PHP 7, you can also define the array as a constant by using define.

Example: 

define('NAME', array('Chandar','Ram','Gugu'));

echo NAME[1]; // outputs "Chandar"

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
php ,php 7 ,web dev ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}