Optional arguments in PHP function

  Pi Ke        2011-03-28 12:35:20       4,732        0    

In PHP, we can define our own functions which can accept optional number of arguments. In this kind of functions, the optional argument should be set with default value.
Fox example, the next code example will illustrate this:
<?php
function func($name,$type=NULL)
{
echo "$name $type";
}
func("Hello");
echo "<br />";
func("Hello","world");
echo "<br />";
?>

Here $type is an optional argument. It must be set with a default value either NULL or some other value. 
Quite simple.Right! In other programming languages, we can achieve similar effects. This is very convenient if we sometimes want to call the same function with different number of arguments.
Also, in PHP API, there are some supplemental functions such as func_num_args(),func_get_arg() and func_num_args() which are to process the arguments passed by the user. This may also help you.

PHP  OPTIONAL  ARGUMENTS  USER DEFINED FUNC 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Crazy Atom Plugin