Lamez Posted January 15, 2010 Share Posted January 15, 2010 I am working on a function that makes a list out of an existing array, this website was pre-made, and I cannot locate the list for the life of me. How can I make a default value in function based off of a variable? <?php function makeSportList($name = "", $sportList = $sport_list){ $list = '<select name="sport" id="sport">'; $list .= '<option value="-1">All Sports</option>'; for ($i=0;$i<=count($sportList);$i++){ if(md5(strtolower($name)) == md5(strtolower($sportList[$i]))){ $sel = "SELECTED"; }else{ $sel = ""; } $list .= '<option value="'.$sportList[$i].'" '.$sel.'>'.$sportList[$i].'</option>'; } return $list.'</select>'; } ?> The manual says nothing of this. :/ -thanks! Quote Link to comment https://forums.phpfreaks.com/topic/188614-question-default-values-of-functions/ Share on other sites More sharing options...
taquitosensei Posted January 15, 2010 Share Posted January 15, 2010 the default value is in the function definition function makeSportList($name="Default Value Goes Here", $sportlist=$sport_list) { } Quote Link to comment https://forums.phpfreaks.com/topic/188614-question-default-values-of-functions/#findComment-995757 Share on other sites More sharing options...
Lamez Posted January 15, 2010 Author Share Posted January 15, 2010 Like I mention, I think I mentioned, that returns a error: Parse error: syntax error, unexpected T_VARIABLE in ... Its the $foo = $bar. Wait, I think I figured out how do this. Quote Link to comment https://forums.phpfreaks.com/topic/188614-question-default-values-of-functions/#findComment-995780 Share on other sites More sharing options...
trq Posted January 15, 2010 Share Posted January 15, 2010 How can I make a default value in function based off of a variable? You can't. Functions are meant to be stand alone reusable bits of code, your logic would break that. Quote Link to comment https://forums.phpfreaks.com/topic/188614-question-default-values-of-functions/#findComment-995782 Share on other sites More sharing options...
trq Posted January 15, 2010 Share Posted January 15, 2010 Sorry, you could but it is a very poor design choice. <?php function makeSportList($name = "", $sportList = null) { if (is_numm($sportList)) { global $sport_list; $sportList = $sport_list; } $list = '<select name="sport" id="sport">'; $list .= '<option value="-1">All Sports</option>'; for ($i=0;$i<=count($sportList);$i++){ if(md5(strtolower($name)) == md5(strtolower($sportList[$i]))){ $sel = "SELECTED"; }else{ $sel = ""; } $list .= '<option value="'.$sportList[$i].'" '.$sel.'>'.$sportList[$i].'</option>'; } return $list.'</select>'; } ?> Terrible idea. Quote Link to comment https://forums.phpfreaks.com/topic/188614-question-default-values-of-functions/#findComment-995785 Share on other sites More sharing options...
Lamez Posted January 15, 2010 Author Share Posted January 15, 2010 Thanks for the (not needed) help, I guess I had a brainfart (again), here is how I fixed it: <?php function makeSportList($sportList, $name = ""){ $list = '<select name="sport" id="sport">'; $list .= '<option value="-1">All Sports</option>'; for ($i=0;$i<=count($sportList);$i++){ if(md5(strtolower($name)) === md5(strtolower($sportList[$i]))){ $sel = "SELECTED"; }else{ $sel = ""; } $list .= '<option value="'.$sportList[$i].'" '.$sel.'>'.$sportList[$i].'</option>'; } return $list.'</select>'; } echo makeSportList($sport_list); echo "<br />"; echo makeSportList($sport_list, "football"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/188614-question-default-values-of-functions/#findComment-995789 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.