johnsmith153 Posted January 26, 2012 Share Posted January 26, 2012 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 More sharing options...
RussellReal Posted January 26, 2012 Share Posted January 26, 2012 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_args, func_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 Link to comment https://forums.phpfreaks.com/topic/255817-simplepassing-null-value-to-function/#findComment-1311380 Share on other sites More sharing options...
litebearer Posted January 26, 2012 Share Posted January 26, 2012 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}"; } Link to comment https://forums.phpfreaks.com/topic/255817-simplepassing-null-value-to-function/#findComment-1311381 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.