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

PHP Type Declarations

एक type declaration एक box पर एक label जैसा है जो कहती है 'यहाँ सिर्फ apples आएँ'। PHP फिर complain करता है अगर आप कुछ और डालने की कोशिश करें, जो गलतियाँ जल्दी पकड़ लेता है।
Syntax
php
function function_name(type $parameter, ?type $optional): return_type {
    return $value;
}

Scalar Type Declarations

किसी function parameter से पहले एक type add करना (जैसे function greet(string $name)) PHP को गलत argument type वाली calls reject करवाता है, function body के गहरे अंदर के बजाय call site पर ही bugs पकड़ते हुए।

उदाहरण: Scalar Type Declarations

php
<?php
// Define the function `greet` taking `string $name`
function greet(string $name) {
    // Print "Hello, $name!" to the output
    echo "Hello, $name!";
}
// Call `greet("Alice")`
greet("Alice");
?>

Return Type Declarations

parameter list के बाद declared एक return type (function getAge(): int) document और enforce करता है कि एक function वापस क्या देने का promise करता है, जो readers और static analysis tools दोनों को function के contract पर भरोसा करने में मदद करता है।

उदाहरण: Return Type Declarations

php
<?php
// Define the function `getAge` with no parameters
function getAge(): int {
    // Return `30` from this function
    return 30;
}
// Print `getAge()` to the output
echo getAge();
?>

Nullable Types

किसी type के आगे एक question mark लगाना (?string) उस type या null दोनों की अनुमति देता है, जो parameters या return values के लिए ज़रूरी है जिनकी genuinely कोई value न हो सकती है, जैसे एक optional lookup जो कुछ भी न ढूँढ पाए।

उदाहरण: Nullable Types

php
<?php
// Define the function `findUser` taking `?string $name`
function findUser(?string $name): ?string {
    // Return `$name` from this function
    return $name;
}
// Print a detailed dump (with types) of `findUser(null)`
var_dump(findUser(null));
?>

Strict Types

किसी file के top पर declare(strict_types=1) उस file के लिए PHP की usual implicit type coercion disable कर देता है, इसलिए जहाँ एक int expected हो वहाँ एक string pass करना चुपचाप convert होने के बजाय एक TypeError throw करता है।

उदाहरण: Strict Types

php
<?php
declare(strict_types=1);

function double(int $x): int {
    return $x * 2;
}
echo double(5);
// double("5") would now throw a TypeError instead of silently converting
?>

Iterable Type

iterable type एक plain array या Traversable implement करने वाला कोई भी object accept करता है, एक function को 'कोई भी चीज़ जिस पर आप foreach चला सकें' accept करने देते हुए बिना इस बात की परवाह किए कि यह कौन सा concrete form है।

उदाहरण: Iterable Type

php
<?php
// Define the function `printAll` taking `iterable $items`
function printAll(iterable $items) {
    // Loop over `$items`, binding each item to `$item`
    foreach ($items as $item) {
        // Print `$item . "\n"` to the output
        echo $item . "\n";
    }
}
// Call `printAll([1, 2, 3])`
printAll([1, 2, 3]);
?>
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. declare(strict_types=1); भूल जाना और strict type checks की उम्मीद करना, जबकि PHP अन्यथा types को चुपचाप convert कर देता है।
  2. declared return type से match न करने वाली एक value return करना, जो एक TypeError throw करता है।
  3. एक nullable type को int|null लिखना और short form ?int भूल जाना, या function के अंदर null handle करना भूल जाना।

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.