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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AI Is Making PHP Cool Again
  • 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

Trending

  • Prompt Caching for LLMs, Part 2: Semantic Caching in Production
  • The "Just Read the Code" Myth Is Killing Your AI Workflow
  • Ingesting Public Data That Fails Silently: Patterns From a TypeScript and Cloudflare Workers Pipeline
  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Sep. 04, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
101.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • AI Is Making PHP Cool Again
  • 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

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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