PHP Iterables
In this page:
function function_name(iterable $items): iterable {
foreach ($items as $item) {
yield $item;
}
}
is_iterable($value);
क्या Iterable Count होता है
दो तरह की values iterable type satisfy करती हैं: कोई भी plain PHP array, और कोई भी object जो built-in Traversable interface implement करता हो (जिसमें Iterator interface और Generator functions दोनों शामिल हैं) -- दोनों एक foreach loop का target हो सकते हैं।
उदाहरण: What Counts as Iterable
<?php
// Define the function `gen` with no parameters
function gen() {
yield 1;
yield 2;
}
// Print a detailed dump (with types) of `is_iterable([1, 2, 3])`
var_dump(is_iterable([1, 2, 3]));
// Print a detailed dump (with types) of `is_iterable(gen())`
var_dump(is_iterable(gen()));
?>
Login to try C/C++/Java/PHP code in the editor
Parameter Type की तरह iterable इस्तेमाल करना
किसी function parameter को array के बजाय iterable type-hint करना signal करता है कि function input पर सिर्फ foreach से loop चलाएगा, और इसे specifically एक real array होने की माँग नहीं है -- function को inputs की एक wider range के साथ usable बनाते हुए, बड़े generators सहित।
उदाहरण: Using iterable as a Parameter 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
Return Type की तरह iterable इस्तेमाल करना
एक function अपना return type भी iterable declare कर सकता है, यह signal करते हुए कि यह कुछ loopable return करेगा -- या तो एक real array जो यह बनाता है, या yield इस्तेमाल करने वाला एक generator -- बिना यह commit किए कि कौन सा specific one, function की implementation को बाद में बदलने की flexibility देते हुए।
उदाहरण: Using iterable as a Return Type
<?php
// Define the function `numbers` with no parameters
function numbers(): iterable {
yield 1;
yield 2;
yield 3;
}
// Loop over `numbers()`, binding each item to `$n`
foreach (numbers() as $n) {
// Print `$n . "\n"` to the output
echo $n . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
Generators का One-Time-Use Nature
किसी array के उलट, जिस पर आप जितनी बार चाहें loop चला सकते हैं, एक Generator सिर्फ एक बार iterate किया जा सकता है -- एक बार किसी generator पर एक foreach loop खत्म हो जाए, उसी generator object पर फिर loop चलाना कुछ भी produce नहीं करता, क्योंकि इसकी internal position पहले से अंत तक advance हो चुकी है।
उदाहरण: The One-Time-Use Nature of Generators
<?php
function gen() {
yield 1;
yield 2;
}
$g = gen();
foreach ($g as $v) { echo $v . "\n"; }
foreach ($g as $v) { echo "second pass: $v\n"; } // never runs
echo "done";
?>
Login to try C/C++/Java/PHP code in the editor
iterable बनाम array: सही Type Hint चुनना
array specifically तब इस्तेमाल करें जब किसी function को genuinely count(), sorting, या key से random access जैसी array-only capabilities चाहिए हों।
iterable तब इस्तेमाल करें जब किसी function को सिर्फ foreach से एक बार values में चलना हो -- बिना किसी real cost के inputs की एक wider range accept करते हुए।
उदाहरण: iterable vs array: Choosing the Right Type Hint
<?php
function countItems(array $items) {
return count($items); // needs real array capabilities
}
function printItems(iterable $items) {
foreach ($items as $item) {
echo $item . "\n"; // only ever loops
}
}
echo countItems([1, 2, 3]);
?>
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि iterable specifically "array" का मतलब है, और फिर iterable से type-hinted एक parameter पर array-only functions (जैसे array_push()) call करना, जो fail हो जाता है अगर इसके बजाय एक Traversable object pass किया जाए।
- यह भूल जाना कि एक iterable parameter एक Generator हो सकता है, जिस पर सिर्फ एक बार loop चलाया जा सकता है -- इसे दूसरी बार loop चलाना कुछ नहीं देता।
- किसी parameter को array type-hint करना जब iterable ज़्यादा flexible होगा और उतना ही safe उस function के लिए जो सिर्फ अपने input पर foreach से loop चलाता है।
- iterable एक pseudo-type है (PHP 7.1 से available) जो किसी भी ऐसी चीज़ को represent करता है जिस पर foreach से loop चलाया जा सके: एक array, या Traversable implement करने वाला कोई भी object।
- iterable की तरह hinted एक function parameter या return type plain arrays और iterator/generator objects दोनों को interchangeably accept करता है।
- क्योंकि एक Generator सिर्फ एक बार iterate किया जा सकता है, एक iterable receive करने वाले code को बिना check किए यह नहीं मानना चाहिए कि इस पर कई बार loop चलाया जा सकता है।
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