Jump to content

Converting an Int to a string?


martinpompey

Recommended Posts

Hi All,

 

I have a php form which is posting a country variable which is currently a number value, but before I post this to a flat text file I want to convert the number into a string (E.G. in the DB 1 = France 3=UK 6-Germany)... I was thinking something along the lines of:

 

$Country = $_REQUEST['Country'];

 

if ($Country =1)

{$CountryW = 'France';}

 

else if ($Country =3)

{$Country = 'UK';}

 

else if ($Country =9)

{$Country = 'Test';}

 

else {$Country = 'Germany';}

 

But this doesn't seem to be working and there would be around 180 Countries so I can see its not efficient. Any help would be much appreciated. If you need more code let me know.

 

Thanks so much for any help

Martin

Link to comment
https://forums.phpfreaks.com/topic/210642-converting-an-int-to-a-string/
Share on other sites

You would use a lookup array -

 

$lookup = array();
$lookup[1] = 'France';
$lookup[3] = 'UK';
$lookup[9] = 'Test';
// add other entries here ...

$Country = $_REQUEST['Country'];

if(isset($lookup[$Country])){
    $Country = $lookup[$Country];
} else {
    $Country = 'Germany';
}

 

Once you have an array like that you would also use it to build your form so that you have consistency and shorter code.

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.