0

Suppose following is my function definition:

public function addPhotoFeed($val)
{
-----------------------------
------------------------------
-----------------------------
}

In above function $val is an array that is passed as an argument to the same.

Now I want to call the above function when I don't pass any argument. So how should I call it?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    Link to the [PHP Docs](http://www.php.net/manual/en/functions.arguments.php#functions.arguments.default).... reading the docs can be very, very useful – Mark Baker Feb 25 '15 at 19:32
  • possible duplicate of [How do you create optional arguments in php?](http://stackoverflow.com/questions/34868/how-do-you-create-optional-arguments-in-php) – halfer Feb 25 '15 at 19:37
  • ... and searching Stack Overflow prior to asking a question can also be very, very useful `:-)`. – halfer Feb 25 '15 at 19:40

2 Answers2

1

Simply assign the parameter to null, so like this.

public function addPhotoFeed($val = null)
{
 //TODO
}

You will be able to call the function with or without parameters

addPhotoFeed();
addPhotoFeed("Something");

If you don't want the function to do anything when its called with a null parameter, then you could add a condition inside the function, something like this.

public function addPhotoFeed($val = null)
{
   if($vall == null)
     // Do nothing
   else
     // Do something
}
André Ferraz
  • 1,511
  • 11
  • 29
0

You can use default value:

public function addPhotoFeed($val = array())
Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65