-1

Im trying to make a function, that takes an input, determines its value, and outputs a word from an already existing array that has been included in the script. The problem is the output is blank, i believe the function ignores variables already in the script, is there a way to change this so existing variables arent ignored by the function?

Here is the function:

The words need to be from an array because of multi-lingual requirements.

function get_genre($id)
{
    if($id == "1"){
        $genre = $lang['277'];
    }
    if($id == "2"){
        $genre = $lang['278'];
    }
    if($id == "3"){
        $genre = $lang['279'];
    }
    if($id == "4"){
        $genre = $lang['280'];
    }
    if($id == "5"){
        $genre = $lang['281'];
    }
    if($id == "6"){
        $genre = $lang['282'];
    }
    if($id == "7"){
        $genre = $lang['283'];
    }
    if($id == "8"){
        $genre = $lang['284'];
    }
    if($id == "9"){
        $genre = $lang['285'];
    }
    if($id == "10"){
        $genre = $lang['286'];
    }
    if($id == "11"){
        $genre = $lang['287'];
    }
    if($id == "12"){
        $genre = $lang['288'];
    }
    if($id == "13"){
        $genre = $lang['289'];
    }
    if($id == "14"){
        $genre = $lang['290'];
    } 
    if($id == "15"){
        $genre = $lang['374'];
    }
    return $genre;
}    
hakre
  • 178,314
  • 47
  • 389
  • 754
JimmyBanks
  • 3,201
  • 6
  • 38
  • 66

3 Answers3

0
function get_genre($id)
{
    global $lang;
    ....
}

or

function get_genre($id, $lang) //Must pass $lang array to function here
{

}
Connor Deckers
  • 2,024
  • 4
  • 24
  • 42
  • i don't understand, in your example `$genre = $lang['277'];`, $lang already has a value because it has already been defined previously. If you set it as global within the function, it doesn't allow you to use its values set previously, from outside. Besides, using global is a very bad habit. See this link: http://stackoverflow.com/q/1557787/1291428 – Sebas Jun 23 '12 at 02:37
  • @Sebas While I agree that `global` isn't the best system in existence, if the variable is contained within the same file as the function, and always will be, there's no problem with it, in this case. Also, as far as my knowledge and experience leads, `global` reads the variable in it's current state, and after the function call, it is read as it is. It's not passed as a copy, but rather a reference, so it should work fine. – Connor Deckers Jun 23 '12 at 02:47
  • It sounds like a trick to me, an ugly one. If you really want to use global, at least use it on the top of the stack... – Sebas Jun 23 '12 at 02:52
  • @Sebas: Hey, if it works, it works. No one said it had to be pretty. – Connor Deckers Jun 23 '12 at 02:52
  • 1
    @Sebas so after trolling everyone on this page about being wrong, you delete your answer after i pointed out it didnt work and there was multiple syntax errors? Classy. – JimmyBanks Jun 24 '12 at 01:41
  • @JimmyBanks, if this helped you out, could you upvote it? Every point helps :) – Connor Deckers Jun 24 '12 at 01:43
  • Yay :) Someone must have been annoyed and downvoted me then.. odd. Oh well :) – Connor Deckers Jun 24 '12 at 01:45
  • The currently given answers are perfectly inestetic and against what i consider healthy programming, that's why I downvoted them. Regarded my answer, I just tried it on my computer and it works. The way you answered made me realize that afterall, to me it was not worth loosing any additional time, that's why a aborted my attempt to help. Good luck – Sebas Jun 24 '12 at 08:33
  • I have to add that I didn't troll, just tried to make the readers understand that the provided answers were not of my taste. Please bring any moderation if you think it went out of bounds. I apologize if it did. – Sebas Jun 24 '12 at 08:35
0
function get_genre($id)
{
 global $lang;

if($id == "1"){
    $genre = $lang['277'];
}
if($id == "2"){
    $genre = $lang['278'];
}
if($id == "3"){
    $genre = $lang['279'];
}
if($id == "4"){
    $genre = $lang['280'];
}
if($id == "5"){
    $genre = $lang['281'];
}
if($id == "6"){
    $genre = $lang['282'];
}
if($id == "7"){
    $genre = $lang['283'];
}
if($id == "8"){
    $genre = $lang['284'];
}
if($id == "9"){
    $genre = $lang['285'];
}
if($id == "10"){
    $genre = $lang['286'];
}
if($id == "11"){
    $genre = $lang['287'];
}
if($id == "12"){
    $genre = $lang['288'];
}
if($id == "13"){
    $genre = $lang['289'];
}
if($id == "14"){
    $genre = $lang['290'];
} 
if($id == "15"){
    $genre = $lang['374'];
}
return $genre;

}

Joshua
  • 126
  • 6
-1

While this isn't ideal, have you considered using an associative array?

var $lookupArray = array();
$lookupArray["1"] = $lang['274'];
....

Then you can call it like this:

function get_genre($id)
{            

   return(array_key_exists($id,$lookupArray)) ? $lookupArray[$id] : null;                      

} 
Rachael
  • 424
  • 2
  • 6