stammeshaus Posted December 15, 2010 Share Posted December 15, 2010 Hello, I have this long if...elseif string and I want to know if I can do this a lot shorter. e.g. with a var statement. I am a beginner in coding, so excuse me if this is a simple questeion... if ($landdef == "NL"){ $landnaamdef = "Nederland"; } elseif ($landdef == "AFG"){ $landnaamdef = "Afghanistan"; } elseif ($landdef == "AL"){ $landnaamdef = "Albanië"; } elseif ($landdef == "GBA"){ $landnaamdef = "Alderney"; } elseif ($landdef == "DZ"){ $landnaamdef = "Algerije"; } elseif ($landdef == "AND"){ $landnaamdef = "Andorra"; } elseif ($landdef == "AN"){ $landnaamdef = "Angola"; } elseif ($landdef == "RA"){ $landnaamdef = "Argentinië"; } elseif ($landdef == "AUS"){ $landnaamdef = "Australië"; } etc...... Link to comment https://forums.phpfreaks.com/topic/221757-how-to-make-a-long-ifelseif-string-shorter/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 15, 2010 Share Posted December 15, 2010 You would use an array to hold the key/value pairs - <?php $lookup = array(); $lookup['NL'] = "Nederland"; $lookup['AFG'] = "Afghanistan"; // add other entries here... if(isset($lookup[$landdef])){ $landnaamdef = $lookup[$landdef]; } else { // entry does not exist } ?> Link to comment https://forums.phpfreaks.com/topic/221757-how-to-make-a-long-ifelseif-string-shorter/#findComment-1147644 Share on other sites More sharing options...
stammeshaus Posted December 15, 2010 Author Share Posted December 15, 2010 That is shorter indeed and easier to maintain as well. Thanx a lot! Link to comment https://forums.phpfreaks.com/topic/221757-how-to-make-a-long-ifelseif-string-shorter/#findComment-1147659 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.