variable in function

Discussion in 'PHP' started by adbox, Oct 27, 2009.

  1. #1
    I am using the google translate class, and need to create a dynamic function name. what I have now is throwing an error:

    this is the error it is outputting:

    any recommendations?
     
    adbox, Oct 27, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you need a semi-colon after the "spannish"
    $language = "spannish"; //subject to dynamic change
    Also, it's Spanish (1 n)
     
    JAY6390, Oct 27, 2009 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    
    $language = "spanish"; //subject to dynamic change
    $final_string = call_user_func(array($gt, "english_to_{$language}"), $value);
    
    PHP:
     
    Last edited: Oct 27, 2009
    nico_swd, Oct 27, 2009 IP
  4. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #4
    hey nico swd

    I plugged this in :
    and got this error message when trying to run. Does this have to do with $gt?

     
    Last edited: Oct 28, 2009
    adbox, Oct 28, 2009 IP
  5. tonythetiger

    tonythetiger Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    try doing this....

    $language = "spannish" //subject to dynamic change
    eval('\$final_string = \$gt->enlgish_to_'.$language.'(\$value)');

    that should give you what you want.
     
    tonythetiger, Oct 28, 2009 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Stay away from eval() whenever possible.

    Try this:
    
    $language = "spanish"; //subject to dynamic change
    $function = "english_to_{$language}";
    
    if (method_exists($gt, $function))
    {
        $final_string = call_user_func(array($gt, "english_to_{$language}"), $value);
    }
    else
    {
        exit('Language not supported');
    }
    
    PHP:
     
    nico_swd, Oct 29, 2009 IP
  7. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #7
    Hey guys, still no dice:

    throws this error:

    and

    tells me the language is not supported, and if I use the code:

    $c = $gt->english_to_spannish("$a");

    I get results
     
    adbox, Oct 29, 2009 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    Check the spelling. $language holds "spanish", and your function is called "spannish" (which is incorrect spelling, by the way). My first code should work just fine...
     
    nico_swd, Oct 30, 2009 IP
  9. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #9
    Your code works right, I just had to instantiate the $gt variable, I had not done it yet. final code:

    Thank you nico_swd!
     
    adbox, Nov 3, 2009 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    You're welcome. :)
     
    nico_swd, Nov 4, 2009 IP