View Single Post
  #1 (permalink)  
Old 04-11-2008, 08:00 PM
dman_2007
Guest
 
Posts: n/a
iTrader: / %
Default Tip : Functions which take variable number of arguments in php

We can easily create functions which can take variable number of arguments in php by using following three functions : func_num_args, func_get_arg and func_get_args. Following code will demonstrate how to do it :

Code:
<?php
  function test_func()
  {
    echo 'No. of arguments passed ', func_num_args();
    echo '<br />';
    echo 'First argument passed : ', func_get_arg(0), '<br />'; 
    echo 'List of all arguments passed : ';
    print_r(func_get_args());
    echo '<br /><br />';
  }  
  
  test_func(1);
  test_func(1, 2, 3, 4, 5, 6, 7); 
?>
When run the previous code will produce the following output :

Quote:
No. of arguments passed 1
First argument passed : 1
List of all arguments passed : Array ( [0] => 1 )

No. of arguments passed 7
First argument passed : 1
List of all arguments passed : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 )
Reply With Quote