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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Trending

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Medallion Architecture: Why You Need It and How To Implement It With ClickHouse
  • Top Book Picks for Site Reliability Engineers
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  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
100.9K 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

  • Inheritance in PHP: A Simple Guide With Examples
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Laravel for Beginners: An Overview of the PHP Framework
  • Is PHP Still the Best Language in 2024?

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!