DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Migrating from Monolith to Microservices Using PHP: A Step-by-Step Guide
  • Inheritance in PHP: A Simple Guide With Examples

Trending

  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  • Building a Zero-Cost Approval Workflow With AWS Lambda Durable Functions
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • 11 Agentic Testing Tools to Know in 2026
  1. DZone
  2. Coding
  3. Languages
  4. What's New in PHP 7

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.

By 
Chandar Bhushan user avatar
Chandar Bhushan
·
Sep. 04, 18 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
101.2K Views

Join the DZone community and get the full member experience.

Join For Free

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"
PHP

Published at DZone with permission of Chandar Bhushan. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Migrating from Monolith to Microservices Using PHP: A Step-by-Step Guide
  • Inheritance in PHP: A Simple Guide With Examples

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook