Jump to content

how to make a long if..elseif string shorter


stammeshaus

Recommended Posts

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......

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
}

?>

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.