Jump to content

Question: Default Values of Functions


Lamez

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/188614-question-default-values-of-functions/
Share on other sites

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.

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");
?>

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.