PHP में Variable Length Argument Functions ऐसे फंक्शन होते हैं, जो किसी निश्चित संख्या तक सीमित न रहकर dynamic रूप से कई arguments (पैरामीटर) को स्वीकार कर सकते हैं। इसका मतलब है कि आप एक ही फंक्शन को एक या एक से अधिक arguments के साथ कॉल कर सकते हैं।
Why Use Variable Length Argument Functions In Php?
जब हमें एक Function में arguments की संख्या पहले से नहीं पता हो या हमें dynamic input प्रोसेस करना हो, तो हम variable-length arguments का उपयोग करते हैं।
PHP में ... (Splat Operator):
- PHP में variable-length arguments को handle करने के लिए
...(splat operator) का उपयोग किया जाता है। - यह सभी passed arguments को array के रूप में फंक्शन में प्राप्त करता है।
Syntax: Variable Length Argument Function:
<?php
function functionName(...$parameters) {
// Your Code Goes Here
}
?>PHPPHP Variable Length Argument Functions का Example:
Without Fixed Parameter:
<?php
function printValues(...$values) {
foreach ($values as $value) {
echo $value . "\n";
}
}
printValues(10, 20, 30);
printValues("PHP", "MySQL", "JavaScript");
?>PHP
Some Fixed & Some Variable Parameter:
<?php
function studentDetails($name, ...$marks) {
echo "Student Name: $name \n";
echo "Marks: " . implode(", ", $marks) . "\n";
}
studentDetails("Ravi", 85, 90, 78);
studentDetails("Suman", 92, 88);
?>PHP
Calculation With Arguments:
<?php
function sum(...$numbers) {
return array_sum($numbers); // सभी arguments का जोड़ return करें
}
echo "Sum: " . sum(10, 20, 30, 40); // Output: Sum: 100
echo "\nSum: " . sum(5, 15);
?>PHP
Using Multidimensional Data:
<?php
function displayData(...$data) {
foreach ($data as $item) {
if (is_array($item)) {
echo implode(", ", $item) . "\n";
} else {
echo $item . "\n";
}
}
}
displayData("Ravi", [85, 90, 78], "Suman", [92, 88]);
?>PHP
PHP func_get_args() function:
PHP में func_get_args() एक इनबिल्ट फंक्शन है, जिसका उपयोग किसी फंक्शन में पास किए गए सभी arguments को एक array के रूप में प्राप्त करने के लिए किया जाता है। यह उन फंक्शन्स में उपयोगी है, जिनमें variable number of arguments पास किए जाते हैं।
<?php
function displayArguments() {
$arguments = func_get_args(); // सभी आर्गुमेंट्स को प्राप्त करें
foreach ($arguments as $arg) {
echo $arg . "\n";
}
}
displayArguments("PHP", "is", "awesome!");
?>PHP
Advantages of Variable Length Argument Functions:
- Dynamic Input Handling: यह unknown number of arguments को आसानी से handle कर सकता है।
- Flexibility: Developers को flexibility देता है, जिससे वे किसी भी संख्या में arguments पास कर सकते हैं।
- Code Reusability: एक ही फंक्शन को अलग-अलग प्रकार और संख्या के arguments के लिए उपयोग किया जा सकता है।
Important Points to Remember:
...के उपयोग से सभी arguments एक array के रूप में प्राप्त होते हैं।- आप वैरिएबल arguments के साथ फिक्स्ड arguments भी उपयोग कर सकते हैं।
- आप
array_sum(),count(), औरimplode()जैसी PHP array functions का उपयोग variable-length arguments को प्रोसेस करने के लिए कर सकते हैं।
Conclusion:
PHP में Variable Length Argument Functions dynamic और flexible programming के लिए बेहद उपयोगी हैं। यह तब उपयोगी होता है जब फंक्शन को एक या अधिक arguments के साथ कॉल किया जाना हो और arguments की संख्या निश्चित न हो।... (splat operator) के उपयोग से हम आसानी से इन arguments को प्रोसेस कर सकते हैं।

Welcome to HindiTutorials, your go-to platform for learning and mastering concepts in Hindi! Our mission is to make education accessible and simple for everyone by providing high-quality tutorials on programming, technology, and various other topics.