slowlygoinginsane Posted December 9, 2013 Share Posted December 9, 2013 Hi Guys, I have a very basic question that I cannot seem to answer ... so would appreciate any help / advise. Code snippet: $Text1="Hi, how are you"; $Text2="Hey there stranger"; $Text3="Its about time!"; $random_number = rand(1,3); $WelcomeText = "$"."Text".$random_number; echo $WelcomeText; The above script generates the output as $Text3 but what I am trying to achieve is this output: Its about time! I know I can use an array and output the value ... but I wanted to know -- why the above code would not work. Any help / explaination would be great! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/284652-help-needed-on-a-simple-question/ Share on other sites More sharing options...
davidannis Posted December 9, 2013 Share Posted December 9, 2013 I think you can do what you want with eval() http://www.php.net/eval. Will try it and post code. Quote Link to comment https://forums.phpfreaks.com/topic/284652-help-needed-on-a-simple-question/#findComment-1461788 Share on other sites More sharing options...
Solution ignace Posted December 9, 2013 Solution Share Posted December 9, 2013 echo ${'Text' . $random_number}; Quote Link to comment https://forums.phpfreaks.com/topic/284652-help-needed-on-a-simple-question/#findComment-1461790 Share on other sites More sharing options...
davidannis Posted December 9, 2013 Share Posted December 9, 2013 <?php $a1='this is a1'; $myvar='$'.'a'.'1'; eval("\$a1 = \"$a1\";"); echo $a1; ?> This works as an example. Quote Link to comment https://forums.phpfreaks.com/topic/284652-help-needed-on-a-simple-question/#findComment-1461792 Share on other sites More sharing options...
slowlygoinginsane Posted December 9, 2013 Author Share Posted December 9, 2013 Thanks Dr and ignace.... problem solved! The eval article actually explained why it was not working. Thanks both. Have a great day! Quote Link to comment https://forums.phpfreaks.com/topic/284652-help-needed-on-a-simple-question/#findComment-1461793 Share on other sites More sharing options...
Barand Posted December 9, 2013 Share Posted December 9, 2013 An array would be the preferred method but "variable variables" (using $$) will also work $Text1="Hi, how are you"; $Text2="Hey there stranger"; $Text3="Its about time!"; $random_number = rand(1,3); $WelcomeText = "Text$random_number"; echo $$WelcomeText; Quote Link to comment https://forums.phpfreaks.com/topic/284652-help-needed-on-a-simple-question/#findComment-1461796 Share on other sites More sharing options...
davidannis Posted December 9, 2013 Share Posted December 9, 2013 Sorry I mangled the example. It works but proves nothing. Try this: <?php $a1='this is in myvar'; $myvar='$'.'a'.'1'; eval("\$myvar = \"$myvar\";"); echo $myvar; ?> though ignace has an easier way. Quote Link to comment https://forums.phpfreaks.com/topic/284652-help-needed-on-a-simple-question/#findComment-1461805 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.