...

View Full Version : forloop trouble



Crash1hd
10-23-2005, 08:12 AM
I was wondering if anyone out there would have a better cleaner code for the following



if($str2ascii == 0)
{
$str2ascii = ")";
}
if($str2ascii == 1)
{
$str2ascii = "!";
}
if($str2ascii == 2)
{
$str2ascii = "@";
}
if($str2ascii == 3)
{
$str2ascii = "#";
}
if($str2ascii == 4)
{
$str2ascii = "$";
}
if($str2ascii == 5)
{
$str2ascii = "%";
}
if($str2ascii == 6)
{
$str2ascii = "^";
}
if($str2ascii == 7)
{
$str2ascii = "&";
}
if($str2ascii == 8)
{
$str2ascii = "*";
}
if($str2ascii == 9)
{
$str2ascii = "(";
}


I was trying to do a for loop but I couldnt figure out how to get the !@#... in it maybe there is a built in function or something?

Thanks

Crash1hd

vinyl-junkie
10-23-2005, 09:22 AM
Have a look at switch (http://us3.php.net/manual/en/control-structures.switch.php). It's a really nice way of formatting this type of code. :)

Crash1hd
10-23-2005, 09:37 AM
Ahh yes but is there no way to do a forloop that I just cant figure out or is the switch the only answer?

marek_mar
10-23-2005, 11:31 AM
You could use an array.

vinyl-junkie
10-23-2005, 04:46 PM
Even if you used an array, look at what you're doing with each occurrence. It's something different for each one. Array processing would more or less expect you to do the same thing with each occurrence in your array. I think switch is your best bet.

marek_mar
10-23-2005, 05:02 PM
<?php
$chars = array(')', '!', '@', '#', '$', '%', '^', '&', '*', '(');
print $chars[$str2ascii];
?>

Crash1hd
10-24-2005, 04:37 AM
excellent thankyou :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum