PHP 8 New Features
In this page:
The str_contains() function
str_contains() finally gave PHP a native, readable way to check for a substring -- before PHP 8, developers had to awkwardly check whether strpos() returned false, which was easy to get subtly wrong.
Example: The str_contains() function
<?php
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) { return strpos($haystack, $needle) !== false; }
}
var_dump(str_contains("Hello World", "World"));
?>
Login to try C/C++/Java/PHP code in the editor
The str_starts_with() and str_ends_with() functions
str_starts_with() and str_ends_with() cover the two other common substring checks natively, replacing brittle substr()-and-compare patterns that had shown up in PHP code for years.
Example: The str_starts_with() and str_ends_with() functions
<?php
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) { return substr($haystack, 0, strlen($needle)) === $needle; }
}
if (!function_exists('str_ends_with')) {
function str_ends_with($haystack, $needle) { return substr($haystack, -strlen($needle)) === $needle; }
}
var_dump(str_starts_with("Hello World", "Hello"));
var_dump(str_ends_with("Hello World", "World"));
?>
Login to try C/C++/Java/PHP code in the editor
The Nullsafe Operator (?->)
The nullsafe operator (?->) short-circuits a chained call the moment any link in the chain is null, returning null immediately instead of throwing a fatal error -- a big readability win over nested isset() checks.
Example: The Nullsafe Operator (?->)
<?php
// PHP 8.0+ syntax
class Address { public $city = "Austin"; }
class User { public $address = null; }
$user = new User();
echo $user->address?->city ?? "No address set";
?>
Login to try C/C++/Java/PHP code in the editor
Constructor Property Promotion
Constructor property promotion collapses the old pattern of declaring a property, adding it as a constructor parameter, and assigning it in the body into a single line per property, cutting a lot of boilerplate.
Example: Constructor Property Promotion
<?php
// PHP 8.0+ syntax
class Point {
public function __construct(
public float $x,
public float $y
) {}
}
$p = new Point(1.5, 2.5);
echo "$p->x, $p->y";
?>
Login to try C/C++/Java/PHP code in the editor
The match Expression
The match expression replaces switch in many cases: it uses strict comparison, requires no break statements, and directly returns a value, making common lookup-style logic far more compact and less error-prone.
Example: The match Expression
<?php
$status = 2;
echo match($status) {
1 => 'Pending',
2 => 'Active',
default => 'Unknown',
};
?>
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser