-1

My function is like this:

<?php
function facebook_friend_list($username){
............................................
............................................
echo "Number of friends:". $N ;
}
?>

My problem: I have to call This function like this :

facebook_friend_list($a);
facebook_friend_list();
facebook_friend_list($t);
............................
............................

inside a loop. So, sometimes this function is getting parameter and sometimes it is not getting. When it does not get parameter, it shows "Missing argument 1 for facebook_friend_list(), called in........................................................."

How to solve this problem?

Asif Iqbal
  • 1,132
  • 1
  • 10
  • 23
  • Assign a default value to your parameter like `function facebook_friend_list($username = '')` or whatever default value you need – Mr. Alien Jan 11 '15 at 06:32
  • 1
    Can your function handle an empty string or `NULL` as parameter? If so, explicitly pass that, or make it the default param value in the function declaration. See http://php.net/manual/en/functions.arguments.php#~Default+argument+values – mario Jan 11 '15 at 06:32
  • 1
    Why do you have to call the function without arguments? What is the meaning of "asking for the friend list of " ? – Erwin Bolwidt Jan 11 '15 at 06:33

4 Answers4

1
<?php

  function facebook_friend_list($username = null){

    if($username == null){
         // argument is not given stuff 
    }

    if($username != null){
         // argument is gived to access use :-  echo $username;
    }

    ............................................
    ............................................
   echo "Number of friends:". $N ;
 }

?>

Thanks :)

Tiger
  • 404
  • 1
  • 4
  • 13
0

You really ought to tell us more about what you're trying to accomplish, but you can make parameters optional by specifying defaults:

function facebook_friend_list($username = null) {
    ...
}
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
0

Change the line

function facebook_friend_list($username){

to

function facebook_friend_list($username = '') {

Then, within the function facebook_friend_list(), make sure to add code to handle the situation when the string is empty.

You can also use a value other than a default string, for example if you wanted it to use a certain username by default, you could type this instead:

function facebook_friend_list($username = "exampleusername") {
Nerixel
  • 427
  • 3
  • 9
0

try this ,

inside the loop
  if($var != null){
  facebook_friend_list($var);
  }else{
  assign 0 ;
  }