Link to home
Start Free TrialLog in
Avatar of debbieau1
debbieau1Flag for United States of America

asked on

Get correct variable contents from page using $_GET param

I have the following

$var1 = "this is the first one";
$var2 = "this is the second one";
$var3 = "this is the third one";

I want to select the variable text, based on a GET parameter.    so for example if the get variable is set to '2', then I want it to echo "This is the second one"  The _GET will be dynamically set.

I didnt want to use if statements here.
Thank you.
ASKER CERTIFIED SOLUTION
Avatar of sohibe1911
sohibe1911

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Assuming that you use the variable "Text" in the $_GET array, and that variable have just a numeric value (1,2,3);

Just:



<?php

$var1 = "this is the first one";
$var2 = "this is the second one";
$var3 = "this is the third one";

// Here we get it...
$text = $(var.$_GET["Text"]);

// Here we show it...

echo ( ((!empty($text)) ? $text : "Text Not Found...") );


?>

Open in new window

Avatar of debbieau1

ASKER

thanks for your quick response but I get a parse error on line 8, which is the problem I was having
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, I didn't put some quotes. Any way, I think that my method is pretty much simple and efficient. ;)

Remember: Assuming that you use the variable "Text" in the $_GET array, and that variable have just a numeric value (1,2,3);

Here the correct code:
<?php

$var1 = "this is the first one";
$var2 = "this is the second one";
$var3 = "this is the third one";

// Here we get it...
$text = $("var".$_GET["Text"]);

// Here we show it...

echo ( ((!empty($text)) ? $text : "Text Not Found...") );


?>

Open in new window

Yes it is simple but unfortunately I still get a parse error.  
 
It refers to the ( on this line
$text = $("var".$_GET["option"]);  
thank you
The people can't wait a little bit... U_U
<?php

$var1 = "this is the first one";
$var2 = "this is the second one";
$var3 = "this is the third one";

// Here we get it...
$text = ("var".$_GET["Text"]);

// Here we show it...

echo ( ((!empty($$text)) ? $$text : "Text Not Found...") );


?>

Open in new window

Thanks for the points; it's a good question and a good teaching example for the right way to do it.  As far as the "efficiency" of code like that goes, all I can say is that the speed of your server is about 100,000,000 times faster than a motivated typist.  So you can optimize this code all you want, you still cannot make a difference of more than 1/100,000,000 to your client audience.  They pay me to tell them stuff like that ;-)

Best regards, ~Ray
U_U

Oh sad confusion... "Efficiency" is not completely about execution time, it is about Productivity too...
So... Use 17 lines to solve a problem that can be solved just in 2 lines, means that a "motivated typist" can write the "2 lines solution" 8.5 times in the same time that another "motivated typist" use to write the "17 lines" solution once.   Time is money, that is what I would to say. =)