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 Quote Link to comment 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 Quote Link to comment 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}"; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.