Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Better way than this? 2

Status
Not open for further replies.

bdichiara

Programmer
Oct 11, 2006
206
US
Is there a better way to do this? For some reason it's not exactly working right.
Code:
$type_name = ($row_atms['type'] == "free") ? "Free ATMs" : "Other";
$type_name = ($row_atms['type'] == "surcharge") ? "ATM's with surcharge fee" : $type_name;
$type_name = ($row_atms['type'] == "fee") ? "ATM's with our fee" : $type_name;
echo $type_name;

_______________
_brian.
 
I'm sorry, this is working fine, however, is there still a better way? I know you can nest multiple if statements in there, I'm just not sure how and what order it proceeds.

_______________
_brian.
 
I'm not sure what you are trying to do, but I think I would use a switch():

Code:
switch ($type_name)
{
	case 'surcharge':
		$type_name = 'ATMs with surcharge fee';
		break;
		
	case 'fee':
		$type_name = 'ATMs with our fee';
		break;
		
	case 'free':
		$type_name = "Free ATMs";
		break;
		
	default:
		$type_name = 'Other';
}

echo $type_name;



Want the best answers? Ask the best questions! TANSTAAFL!
 
yes
Code:
switch ($row['type']):
 case "free":
  $type_name = "Free ATMs";
  break;
 case "surcharge":
  $type_name="ATMs with surcharge fee";
  break;
 case "fee":
  $type_name="ATMs with our fee";
  break;
 default:
  $type_name = "Other";
endswitch;
 
Both of you deserve a star. Worked great. I can't believe I forgot about the good ol' switch function.

_______________
_brian.
 
Or:

Code:
$response = array("surcharge" => "ATMs with surcharge fee",
                  "fee" => "ATMs with our fee",
                  "free" => "Free ATMs");

$type_name = $response[$row_atms["type"]] ? $response[$row_atms["type"]] : "Other";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top