SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot Narfotic's Avatar
    Join Date
    Aug 2003
    Location
    Antwerp, Flanders, BE , EU
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy function in variable

    Prolly an absolute newbie question, but how do I trigger a function as this one (which should take the name of a variable and then add "-en" to it):
    PHP Code:
    function trans_var($var){
       
    $temp explode(".",$var);
         
    $temp[count($temp)-2] .= "_en";
         print(
    implode(".",$temp));
     } 
    in a variable like this one (replacing the 'message' with 'message_en')
    PHP Code:
    <?php echo($news["message"]);?>
    I allready tried some like this:
    PHP Code:
    <?php print($news[trans_var('message')]);?>
    but it just doesnt work

    How can I do this?

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    San Diego, CA (USA)
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    fix your function to this:

    PHP Code:
    function trans_var(&$var){
       
    $temp explode(".",$var);
        
    $temp[count($temp)-2] .= "_en";
        return 
    implode(".",$temp);

    and then you can do this:

    PHP Code:
    print trans_var($news['message']); 

  3. #3
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a little tip i was given a while ago - try and avoid printing from functions, echo'ing is generally ok but either way i would avoid printing inside a function. store the values in a variable and return them as shown above... then print out the result of the function.


Bookmarks

Posting Permissions

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