Skip to content

PHP function that accepts any number of arguments

Share Button
<?php 

    /** Function accepting any number of arguments */
    function manyArgsFunc(){  
        /**  Returns the number of arguments passed to the function */
        $numOfArgs = func_num_args();    
        /** Returns an array with all arguments passed to the function */ 
        $listOfArgs = func_get_args();

        /** Loop through array & display results */
        for ($i = 0; $i < $numOfArgs; $i++) {
            echo $listOfArgs[$i] . "<br />";
                }
    }

/** Passing argument to the function */

// Example #1
manyArgsFunc("www.linuxGet.co.uk","www.selfies.london","www.kwacho.com","www.kwachIT.com",1024);

echo "<hr />";
// Example #2
manyArgsFunc("PHP",1);

echo "<hr />";
// Example #3
manyArgsFunc(NULL);

?>

 

Result:

 

www.linuxGet.co.uk
www.selfies.london
www.kwacho.com
www.kwachIT.com
1024


PHP
1


Share Button