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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  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.

Chandar Bhushan user avatar by
Chandar Bhushan
·
Sep. 04, 18 · Tutorial
Like (8)
Save
Tweet
Share
99.76K 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.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Spring Boot, Quarkus, or Micronaut?
  • Real-Time Analytics for IoT
  • 5 Software Developer Competencies: How To Recognize a Good Programmer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: