← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 13 of 24

PHP 8 New Features

PHP 8 नए tools लाया जो code को छोटा और safer बनाते हैं, किसी favorite game के नए edition में handy extra moves जैसे। यह common jobs के लिए neat helpers add करता है जिन्हें पहले extra काम चाहिए होता था।
Syntax
php
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
<?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"));
?>

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
<?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"));
?>

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
// PHP 8.0+ syntax
class Address { public $city = "Austin"; }
class User { public $address = null; }
$user = new User();
echo $user->address?->city ?? "No address set";
?>

Constructor Property Promotion

Constructor property promotion एक property declare करने, इसे एक constructor parameter की तरह add करने, और body में इसे assign करने के पुराने pattern को प्रति property एक line में collapse कर देता है, काफी boilerplate काटते हुए।

उदाहरण: Constructor Property Promotion

php
<?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";
?>

match Expression

match expression कई cases में switch की जगह लेता है: यह strict comparison इस्तेमाल करता है, इसे break statements की ज़रूरत नहीं, और सीधे एक value return करता है, common lookup-style logic को कहीं ज़्यादा compact और कम error-prone बनाते हुए।

उदाहरण: The match Expression

php
<?php
// Declare `$status`, set to `2`
$status = 2;
// Print `match($status) {` to the output
echo match($status) {
    1 => 'Pending',
    2 => 'Active',
    default => 'Unknown',
};
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. match या str_contains जैसे PHP 8 features किसी server पर इस्तेमाल करना जो पुराना PHP version चलाता है।
  2. nullsafe operator ?-> इस्तेमाल करना और उम्मीद करना कि यह undefined variables से protect करेगा, जबकि यह सिर्फ null objects को guard करता है।
  3. बिना default arm के match इस्तेमाल करना जब कोई value match न करे, जो UnhandledMatchError throw करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.