...

View Full Version : Is this the right way to call a function?



grumpy
08-07-2008, 02:30 PM
Hey,

Here's the problem...

calling the function:

$name_out = ($fname_out . $lname_out);
$name_out_array = preg_split('//', $name_out, -1, PREG_SPLIT_NO_EMPTY);
$name_out_array = $letter;
for ($i=0; $i <= sizeof($letter); $i++)
{
NumTable ($letter, $i);
}
$name_num = array_sum(array ($letter));

The $fname and $lname variables are Firstname and Lastname of a person. The code combines them both, then splits both names into individual letters and then calls the function to replace all the letters with corresponding numbers.

Here's the function:

function NumTable ($letter, $i)
{
if (($letter[$i] == "a") || ($letter[$i] == "j") || ($letter[$i] == "s"))
{
$letter[$i] = 1;
}
else if (($letter[$i] == "b") || ($letter[$i] == "k") || ($letter[$i] == "t"))
{
$letter[$i] = 2;
}
else if (($letter[$i] == "c") || ($letter[$i] == "l") || ($letter[$i] == "u"))
{
$letter[$i] = 3;
}
else if (($letter[$i] == "d") || ($letter[$i] == "m") || ($letter[$i] == "v"))
{
$letter[$i] = 4;
}
else if (($letter[$i] == "e") || ($letter[$i] == "n") || ($letter[$i] == "w"))
{
$letter[$i] = 5;
}
else if (($letter[$i] == "f") || ($letter[$i] == "o") || ($letter[$i] == "x"))
{
$letter[$i] = 6;
}
else if (($letter[$i] == "g") || ($letter[$i] == "p") || ($letter[$i] == "y"))
{
$letter[$i] = 7;
}
else if (($letter[$i] == "h") || ($letter[$i] == "q") || ($letter[$i] == "z"))
{
$letter[$i] = 8;
}
else if (($letter[$i] == "i") || ($letter[$i] == "r"))
{
$letter[$i] = 9;
}
else if ($letter[$i] == " ")
{
$letter[$i] = 0;
}
}

Instead of some numbers 1-9 the output of the function is constantly empty (0) which is an error. What would be the reason?

Would it be better to use a foreach then, instead of the for loop and the $i variable, and if so, how would you do it?

rafiki
08-07-2008, 02:34 PM
Either would work, if your trying to figure out which is fastest then please state.
Trial and error would be a good way to figure it out too. Also Google (http://www.google.com/search?q=for+or+foreach+php).

grumpy
08-07-2008, 02:38 PM
Hi, the problem is instead of some numbers 1-9 the output of the function is 0 which is an error. I have been looking in all search engines and forums all day long before I asked :).

Would appreciate some helpful insight...

abduraooft
08-07-2008, 02:41 PM
if (($letter[$i] == "a") || ($letter[$i] == "j") || ($letter[$i] == "s"))
{
$letter[$i] = 1;
}
else if (($letter[$i] == "b") || ($letter[$i] == "k") || ($letter[$i] == "t"))
{
$letter[$i] = 2;
}
else if (($letter[$i] == "c") || ($letter[$i] == "l") || ($letter[$i] == "u"))
{
$letter[$i] = 3;
}
else if (($letter[$i] == "d") || ($letter[$i] == "m") || ($letter[$i] == "v"))
{
$letter[$i] = 4;
}
else if (($letter[$i] == "e") || ($letter[$i] == "n") || ($letter[$i] == "w"))
{
$letter[$i] = 5;
}
else if (($letter[$i] == "f") || ($letter[$i] == "o") || ($letter[$i] == "x"))
{
$letter[$i] = 6;
}
else if (($letter[$i] == "g") || ($letter[$i] == "p") || ($letter[$i] == "y"))
{
$letter[$i] = 7;
}
else if (($letter[$i] == "h") || ($letter[$i] == "q") || ($letter[$i] == "z"))
{
$letter[$i] = 8;
}
else if (($letter[$i] == "i") || ($letter[$i] == "r"))
{
$letter[$i] = 9;
}
else if ($letter[$i] == " ")
{
$letter[$i] = 0;
}
} How about a switch() statement like

function NumTable ($letter, $i)
{
switch($letter[$i]){
case 'a':
case 'j':
case 's':$letter[$i] = 1; break;
.....................
default: $letter[$i] = 0;
}
}

Fou-Lu
08-07-2008, 04:29 PM
A switch is a much better idea.
This doesn't account for $letters being empty however, since the main function declares what $letters is. Perhaps you should be applying the $name_out_array to $letters instead of the other way around?
I'm pretty sure that arrays in PHP are immutable so you should be able to alter the original inside a function without needing to return a value or passing it by references, although applying either can't hurt.
I'm still putting my money into this one:


$name_out_array = $letter;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum