How can I concatenate a string to a variable and use it like another variable?
I have a variable $language
, which can be: "en", "nl", or "fr"
. I have 3 other variables $menu_en, $menu_fr and $menu_fr
.
I have a php page where I want the menu to be displayed in any of three languages.
I've tried this:
echo '$menu_' . $language;
I want the result to be a value $menu_en
, not a string $menu_nl
.
How can i do this? Can eval () help?
source to share
How about using it array
?
$menu = array("en" => "Hi!", "it" => "Ciao!", "es" => "Hola!");
$language = "en";
echo $menu[$language]; // this will print Hi!
echo $menu["it"]; // will print Ciao!
Some references to arrays: http://php.net/manual/it/language.types.array.php
source to share
You can use variable variables - for example:
$menuVar = 'menu_'.$language;
echo $$menuVar;
Pay attention to the double dollar signs.
Link: http://php.net/manual/en/language.variables.variable.php
source to share