Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print a variable who's name is stored in another var 1

Status
Not open for further replies.

esromneb

Programmer
Mar 30, 2002
76
US
Is there any other way to do this with out using the ugly eval function?
Code:
<?
$text1 = &quot;this is text1&quot;;
$text2 = &quot;welcome to text two&quot;;
$index = 2;

$code = &quot;print(\&quot;\$text$index\&quot;);&quot;;
print(&quot;code = $code<br>\n&quot;);
eval($code);
?>

Thanks,
Ben
 
One way is to use arrays:

$text = array ('this is text1', 'welcome to text two');
$index = 1;
print $text[$index];


Another is to use variable variables (
<?php
$text1 = &quot;this is text1&quot;;
$text2 = &quot;welcome to text two&quot;;
$index = 2;

print ${'text'.$index};
?>


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top