raul_7 Posted July 27, 2007 Share Posted July 27, 2007 Hello everyone, thank you very much for all the help you guys provide i have a question lets say i have bunch of variables like this $test1 $test2 $test3 etc ... now i want to print each variable but i dont want to type $test1 $test2 every time (there are over 200 variable) and each time user selects different number of variables, so i have a for loop which makes a $i variable and increase it every time it runs, i was hoping to see if there is anyway to print bunch of them maybe like this ? echo $test$i; -->of course this doesn't work but i am just trying to show what i need. any help is very much appreciated. Thank you. Raul Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 27, 2007 Share Posted July 27, 2007 do your loop but echo $test . $i; Quote Link to comment Share on other sites More sharing options...
tibberous Posted July 27, 2007 Share Posted July 27, 2007 Yes you can, one sec, I'll write it for you... Quote Link to comment Share on other sites More sharing options...
Nakor Posted July 27, 2007 Share Posted July 27, 2007 The above example will not do what you are after. Try the following: eval('echo $test'. $i .';'); Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 27, 2007 Share Posted July 27, 2007 um why wouldnt it work??? Quote Link to comment Share on other sites More sharing options...
Nakor Posted July 27, 2007 Share Posted July 27, 2007 Because you are just concatenating two variables. Your code: $test = 'default'; $test1 = 'a'; $test2 = 'b'; $test3 = 'c'; for($i = 1; $i <= 3; $i++) { echo $test . $i; } /* Output: default1 default2 default3 */ My code: $test = 'default'; $test1 = 'a'; $test2 = 'b'; $test3 = 'c'; for($i = 1; $i <= 3; $i++) { eval('echo $test'. $i .';'); } /* Output: a b c */ Quote Link to comment Share on other sites More sharing options...
tibberous Posted July 27, 2007 Share Posted July 27, 2007 <?php $test1 = "=)"; $test2 = ""; $test3 = ""; $test4 = ":|"; $test5 = ":/"; if( ($face = intval($face)) >= 1 && ($face = intval($face)) <= 5){ $myvar = "test$face"; $myvalue = $$myvar; echo "<div><b>$myvalue</b></div>"; } ?><form action="<?php echo $PHP_SELF; ?>" method="get"> <div>Enter 1-5 <input type="text" name="face"></div> <div><input type="submit" value="Get my Face!"></div> </form> Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted July 27, 2007 Share Posted July 27, 2007 Nakor your code doesnt work... : Error: Parse error: syntax error, unexpected $end, expecting ',' or ';' in /home/dreamsh/public_html/test.php(9) : eval()'d code on line 1 Parse error: syntax error, unexpected $end, expecting ',' or ';' in /home/dreamsh/public_html/test.php(9) : eval()'d code on line 1 Parse error: syntax error, unexpected $end, expecting ',' or ';' in /home/dreamsh/public_html/test.php(9) : eval()'d code on line 1 Code: <?php $test = 'default'; $test1 = 'a'; $test2 = 'b'; $test3 = 'c'; for($i = 1; $i <= 3; $i++) { eval('echo $test'. $i); } ?> Quote Link to comment Share on other sites More sharing options...
Nakor Posted July 27, 2007 Share Posted July 27, 2007 Left out the closing tag, my bad eval('echo $test'. $i .';'); Tested and works =) Quote Link to comment Share on other sites More sharing options...
tibberous Posted July 27, 2007 Share Posted July 27, 2007 Here it is written using eval. <?php $test1 = "=)"; $test2 = ""; $test3 = ""; $test4 = ":|"; $test5 = ":/"; if( ($face = intval($face)) >= 1 && ($face = intval($face)) <= 5){ eval("\$x = \$test$face;"); $myvalue = $x; echo "<div><b>$myvalue</b></div>"; } ?><form action="<?php echo $PHP_SELF; ?>" method="get"> <div>Enter 1-5 <input type="text" name="face"></div> <div><input type="submit" value="Get my Face!"></div> </form> Quote Link to comment Share on other sites More sharing options...
raul_7 Posted July 27, 2007 Author Share Posted July 27, 2007 Thank very much Nakor and tibberous , perfect solution. Quote Link to comment 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.