Jump to content

Help needed on a simple question


Go to solution Solved by ignace,

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;
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.