If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
अभी तक हमने PHP में Parameterized Functions , Function Call By value And Call By Reference के बारे में पढ़ा , इन functions को जब हम define करते थे तो हमें need के according parameter भी define करते थे , और उस function को Call करते समय हमें उसी के accordingly values भी pass करनी होती थी।
मतलब हमें ये पता होता था कि कितने variables pass किये जायेंगे उसके according हम function के अंदर logic लिखते थे , अब अगर हमें ये नहीं मालूम हो कि function call करते समय कितने variables pass हो रहे हैं , इस तरह के functions को handle करने के लिए हमें PHP ने facility provide की है Variable Length Argument Functions . जिसकी help से हम pass किये गए सभी arguments / values को easily handle कर सकते हैं। इसके लिए हमें function define करते समय variable से पहले सिर्फ triple dots ( ... ) prepend करने होते हैं।
function function_name(...$var) { echo 'Numbers of variables : '. count($var); }
File : function_length.php
<?php
function print_vars(...$vars)
{
echo 'Numbers of variables : '. count($vars).'<br>';
foreach($vars as $var)
{
echo $var.' , ';
}
}
print_vars(12, 34, 56);
?>
Explain - जब हम Variable Length Argument Handle करते हैं तो जितने भी variables pass करते हैं function call करते समय हमें उन Variables का Array मिलता है , जिसे count() function के trough total passed variables पता कर सकते हैं , साथ ही Foreach Loop या For Loop का use भी कर सकते हैं।
अगर हम function define करते समय कोई भी parameter define नहीं करना चाहते और pass किये गए सभी arguments handle भी करना चाहते हैं , तो आप predefine function func_get_args() का भी use कर सकते हैं। हालाँकि func_get_args() भी हमें pass किये गए सभी arguments का Array ही return करता है।
See Example :
File : function_length2.php
<?php
function test_fun()
{
$arguments = func_get_args();
echo '<pre>';
print_r($arguments);
}
test_fun(45, 34,56, 67, 78, 34);
?>
Array ( [0] => 45 [1] => 34 [2] => 56 [3] => 67 [4] => 78 [5] => 34 )
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook