Monday 28 November 2011

Variable Length Argument Function


Ø       func_num_args

  • This function returns the number of argument passed into the current user defined function. The return type is integer.
  • Func_num_args() will generates a warning if called from outside of a user defined function.

Syntax

                        int func_num_args(void)

Example

<?php
          function callme()
          {
                   $value = func_num_args();
                   echo "Number of arguments : ".$value;
          }
          callme(1,2,3,4);
          ?>

Output  

                   Number of arguments : 4

Ø       func_get_arg

  • Returns an item from the argument list.

Syntax

                        mixed func_get_arg(int arg_num)

  • Returns the argument which is at the arg_num’th offset into a user-defined function’s argument list.
  • Function argument are counted starting from zero.
  • Func_get_arg() will generates a warning if called from outside of a function definition. This function cannot be used directly as a function parameter instead, its result may be assigned to a variable, which can then be passed to the function.

Example

<?php
          function callme()
          {
                   $num_arg = func_num_args();
                   echo "Number of arguments : ".$num_arg."<br/>";
                   if($num_arg >= 2)
                   {
                             echo "Seccond arguments is : ".func_get_arg(1);
                   }
          }
          callme(1,2,3,4);
          ?>

Output  

                   Number of arguments : 4
                   Seccond arguments is : 2

Ø       func_get_args

  • Returns the array of arguments passed into the current user defined function.

Syntax

                        array func_get_args(void)

Example

<?php
          function callme()
          {
                   $num_args = func_num_args();
                   if($num_args > 0)
                   {
                             $num = func_get_args();
                             foreach($num as $value)
                             {
                                      echo $value."<br/>";
                             }
                   }
          }
          callme("Hello","World");
          ?>

Output  

                   Hello
                   World

Posted By : Hemangi Zala

1 comment:

  1. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Best Devops Training in pune
    Data science training in Bangalore

    ReplyDelete