Jump to content

Help needed on a simple question


slowlygoinginsane

Recommended Posts

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 :)

 

Link to comment
https://forums.phpfreaks.com/topic/284652-help-needed-on-a-simple-question/
Share on other sites

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.