Jump to content

Simple:passing null value to function


johnsmith153

Recommended Posts

function display_details($name, $department="sales", $sex="male") {
   return "You are {$name} from {$department} and you are {$sex}";
}

 

I need to use the default value for $department, but none of the below work. How do I do it?

 

echo $display_details("Sally", NULL, "female"); // sets $department as empty/blank

 

echo $display_details("Sally", "", "female"); // sets $department as empty/blank

 

echo $display_details("Sally", , "female"); // PHP error

 

 

Link to comment
https://forums.phpfreaks.com/topic/255817-simplepassing-null-value-to-function/
Share on other sites

you could reverse gender and department, since you don't seem to be omitting gender anywhere there..

 

However, if you want a variable length argument list you could look into:

 

func_num_argsfunc_get_args, and func_get_args

 

func_num_args will tell you how many arguments were specified..

 

func_get_args will get an argument by index

 

func_get_args will get all of the arguments

perhaps...

 

function display_details($name, $department, $sex) {
  if(strlen(trim($department))<1){$department = "Sales";}
  if(strlen(trim($sex))<1){$sex = "Male";}
  return "You are {$name} from {$department} and you are {$sex}";
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.