PHP Type Declarations
In this page:
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
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
// Define the function `getAge` with no parameters
function getAge(): int {
// Return `30` from this function
return 30;
}
// Print `getAge()` to the output
echo getAge();
?>
Login to try C/C++/Java/PHP code in the editor
Nullable Types
किसी type के आगे एक question mark लगाना (?string) उस type या null दोनों की अनुमति देता है, जो parameters या return values के लिए ज़रूरी है जिनकी genuinely कोई value न हो सकती है, जैसे एक optional lookup जो कुछ भी न ढूँढ पाए।
उदाहरण: Nullable Types
<?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));
?>
Login to try C/C++/Java/PHP code in the editor
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
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
?>
Login to try C/C++/Java/PHP code in the editor
Iterable Type
iterable type एक plain array या Traversable implement करने वाला कोई भी object accept करता है, एक function को 'कोई भी चीज़ जिस पर आप foreach चला सकें' accept करने देते हुए बिना इस बात की परवाह किए कि यह कौन सा concrete form है।
उदाहरण: Iterable Type
<?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]);
?>
Login to try C/C++/Java/PHP code in the editor
declare(strict_types=1);भूल जाना और strict type checks की उम्मीद करना, जबकि PHP अन्यथा types को चुपचाप convert कर देता है।- declared return type से match न करने वाली एक value return करना, जो एक
TypeErrorthrow करता है। - एक nullable type को
int|nullलिखना और short form?intभूल जाना, या function के अंदर null handle करना भूल जाना।
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