Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Web =>  PHP

Optional arguments in PHP function

  Pi Ke      2011-03-28 12:35:20      2,314    0    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

  SAVE AS PDF   MARK AS READ   MARK AS IMPORTANT

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious

  RELATED


  0 COMMENT


No comment for this article.


  WRITE ARTICLE

I am looking for an IT support job

By sonic0002
When an IT support engineer goes to a new job interview, how does s/he describes her/his job experience? "Because there are no issues encountered after I deployed the application, so later I was fired and I am looking for a new job."