Nexus10 Posted July 16, 2010 Share Posted July 16, 2010 <?php $learn_textiles_lvl = 5; // original variable $a = "$"."learn_".$industry."_lvl"; // attempt at reconstructing original variable when only $industry is known (it is not always 'textiles') $current_level = $a; // test echo("<br />Current level: $current_level"); ?> How do I construct a variable and get its contents (existing) from a string function (or other way)? I.e. I have the varible $industry and need to get the contents of an existing variable called $learn_(INDUSTRY)_lvl where INDUSTRY is defined in the variable $industry. Quote Link to comment https://forums.phpfreaks.com/topic/207955-making-a-string-into-a-variable/ Share on other sites More sharing options...
kenrbnsn Posted July 16, 2010 Share Posted July 16, 2010 You want to use variable variables Ken Quote Link to comment https://forums.phpfreaks.com/topic/207955-making-a-string-into-a-variable/#findComment-1087120 Share on other sites More sharing options...
Nexus10 Posted July 16, 2010 Author Share Posted July 16, 2010 I'm still confused by that. Quote Link to comment https://forums.phpfreaks.com/topic/207955-making-a-string-into-a-variable/#findComment-1087127 Share on other sites More sharing options...
Nexus10 Posted July 16, 2010 Author Share Posted July 16, 2010 $industry = "Textiles"; $a = ${$industry}; echo("<br />a: $a"); $a is then blank. ? Quote Link to comment https://forums.phpfreaks.com/topic/207955-making-a-string-into-a-variable/#findComment-1087129 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2010 Share Posted July 16, 2010 That is looking for a variable named $Textiles. Is there one? Ken Quote Link to comment https://forums.phpfreaks.com/topic/207955-making-a-string-into-a-variable/#findComment-1087131 Share on other sites More sharing options...
Nexus10 Posted July 16, 2010 Author Share Posted July 16, 2010 No, I have a variable $industry which holds the value 'textiles' (but it also holds 20 other values as well at different times). Quote Link to comment https://forums.phpfreaks.com/topic/207955-making-a-string-into-a-variable/#findComment-1087134 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2010 Share Posted July 16, 2010 Try this: <?php $industry = 'textiles'; $learn_textiles_lvl = 5; // original variable $a = ${'learn_' . $industry . '_lvl'}; // attempt at reconstructing original variable when only $industry is known (it is not always 'textiles') $current_level = $a; // test echo("<br />Current level: $current_level"); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/207955-making-a-string-into-a-variable/#findComment-1087138 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.