PHP 8 New Features
In this page:
str_contains($haystack, $needle);
str_starts_with($haystack, $prefix);
str_ends_with($haystack, $suffix);
$value = $object?->property?->method(); // nullsafe operator
str_contains() Function
str_contains() ने आख़िरकार PHP को एक substring check करने का एक native, readable तरीका दिया -- PHP 8 से पहले, developers को awkwardly यह check करना पड़ता था कि strpos() ने false return किया या नहीं, जो subtly गलत होना आसान था।
उदाहरण: The str_contains() function
<?php
// Check whether `!function_exists('str_contains')`
if (!function_exists('str_contains')) {
// Define the function `str_contains` taking `$haystack`, `$needle`
function str_contains($haystack, $needle) { return strpos($haystack, $needle) !== false; }
}
// Print a detailed dump (with types) of `str_contains("Hello World", "World")`
var_dump(str_contains("Hello World", "World"));
?>
Login to try C/C++/Java/PHP code in the editor
str_starts_with() और str_ends_with() Functions
str_starts_with() और str_ends_with() natively दो और common substring checks cover करते हैं, उन brittle substr()-and-compare patterns की जगह लेते हुए जो PHP code में सालों से दिख रहे थे।
उदाहरण: The str_starts_with() and str_ends_with() functions
<?php
// Check whether `!function_exists('str_starts_with')`
if (!function_exists('str_starts_with')) {
// Define the function `str_starts_with` taking `$haystack`, `$needle`
function str_starts_with($haystack, $needle) { return substr($haystack, 0, strlen($needle)) === $needle; }
}
// Check whether `!function_exists('str_ends_with')`
if (!function_exists('str_ends_with')) {
// Define the function `str_ends_with` taking `$haystack`, `$needle`
function str_ends_with($haystack, $needle) { return substr($haystack, -strlen($needle)) === $needle; }
}
// Print a detailed dump (with types) of `str_starts_with("Hello World", "Hello")`
var_dump(str_starts_with("Hello World", "Hello"));
// Print a detailed dump (with types) of `str_ends_with("Hello World", "World")`
var_dump(str_ends_with("Hello World", "World"));
?>
Login to try C/C++/Java/PHP code in the editor
Nullsafe Operator (?->)
nullsafe operator (?->) chain में किसी भी link के null होते ही एक chained call को short-circuit कर देता है, एक fatal error throw करने के बजाय तुरंत null return करते हुए -- nested isset() checks पर एक बड़ी readability win।
उदाहरण: 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 एक property declare करने, इसे एक constructor parameter की तरह add करने, और body में इसे assign करने के पुराने pattern को प्रति property एक line में collapse कर देता है, काफी boilerplate काटते हुए।
उदाहरण: 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
match Expression
match expression कई cases में switch की जगह लेता है: यह strict comparison इस्तेमाल करता है, इसे break statements की ज़रूरत नहीं, और सीधे एक value return करता है, common lookup-style logic को कहीं ज़्यादा compact और कम error-prone बनाते हुए।
उदाहरण: The match Expression
<?php
// Declare `$status`, set to `2`
$status = 2;
// Print `match($status) {` to the output
echo match($status) {
1 => 'Pending',
2 => 'Active',
default => 'Unknown',
};
?>
Login to try C/C++/Java/PHP code in the editor
matchयाstr_containsजैसे PHP 8 features किसी server पर इस्तेमाल करना जो पुराना PHP version चलाता है।- nullsafe operator
?->इस्तेमाल करना और उम्मीद करना कि यह undefined variables से protect करेगा, जबकि यह सिर्फ null objects को guard करता है। - बिना default arm के
matchइस्तेमाल करना जब कोई value match न करे, जोUnhandledMatchErrorthrow करता है।
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