Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder grumpy's Avatar
    Join Date
    Oct 2006
    Location
    Visible light spectrum
    Posts
    121
    Thanks
    5
    Thanked 6 Times in 6 Posts

    Is this the right way to call a function?

    Hey,

    Here's the problem...

    calling the function:
    PHP Code:
    $name_out = ($fname_out $lname_out);
            
    $name_out_array preg_split('//'$name_out, -1PREG_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:
    PHP Code:
    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?
    Last edited by grumpy; 08-07-2008 at 02:52 PM.

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    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.

  • #3
    Regular Coder grumpy's Avatar
    Join Date
    Oct 2006
    Location
    Visible light spectrum
    Posts
    121
    Thanks
    5
    Thanked 6 Times in 6 Posts
    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...

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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
    PHP Code:
    function NumTable ($letter$i)
    {
       switch(
    $letter[$i]){
          case 
    'a':
          case 
    'j':
          case 
    's':$letter[$i] = 1; break;
         .....................
         default:  
    $letter[$i] = 0;
       }

    Last edited by abduraooft; 08-07-2008 at 04:44 PM. Reason: typo
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
            $name_out_array $letter
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •