kalevra Posted July 14, 2008 Share Posted July 14, 2008 Is there a way to make something like this work? $count = 3; $string = " for ($i=1; $i<$count; $i++){ echo \"This is line number $i\"\n\; } "; echo $string; I'd like it to echo: This is line number 1 This is line number 2 This is line number 3 But it echoes the "un-processed" php code instead... Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/ Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 If you're getting unprocessed PHP output, your webserver may not support PHP at all. The code you gave is full of syntax errors. The following code will work: <?php $count = 3; for ($i=1; $i <= $count; $i++) echo "This is line number $i<br>\n"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/#findComment-589256 Share on other sites More sharing options...
Bendude14 Posted July 14, 2008 Share Posted July 14, 2008 <?php $count = 4; $string = ""; for($i=1; $i < $count; $i++){ echo "This is line number $i"."<br />"; } ?> Try this Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/#findComment-589257 Share on other sites More sharing options...
Lodius2000 Posted July 14, 2008 Share Posted July 14, 2008 im going with ken but i think he forgot some curly braces <?php $count = 3; for ($i=1; $i <= $count; $i++){ echo "This is line number $i<br>\n"; } ?> correct me if im wrong ken, but i think that will do it Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/#findComment-589258 Share on other sites More sharing options...
kalevra Posted July 14, 2008 Author Share Posted July 14, 2008 Ken's solution does work... But I need the code to be in a string and echo that string. Not simply doing a loop to echo the lines. Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/#findComment-589261 Share on other sites More sharing options...
Bendude14 Posted July 14, 2008 Share Posted July 14, 2008 Ok well they are both the same apart from mine was only < (smaller than) and Ken's one <= (smaller than or equal to) Ken's code works without the curly brackets but feel free to add them it will still work Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/#findComment-589263 Share on other sites More sharing options...
Bendude14 Posted July 14, 2008 Share Posted July 14, 2008 just change it to this then and see i echo the string outside of the loop <?php $count = 3; for ($i=1; $i <= $count; $i++) { $text .= "This is line number $i<br>\n"; } echo $text; ?> Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/#findComment-589265 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 When there is only one line in an "if" block or any type of loop, you don't need the curly braces. If you want to assign the value to a string and then print it: <?php $count = 3; $string = ''; for ($i=1; $i <= $count; $i++) $string .= "This is line number $i<br>\n"; echo $string; ?> or <?php $count = 3; $tmp = array(); for ($i=1; $i <= $count; $i++) tmp[] = "This is line number $i"; $string = implode("<br>\n",$tmp) . "<br>\n"; echo $string; ?> Ken Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/#findComment-589266 Share on other sites More sharing options...
kalevra Posted July 14, 2008 Author Share Posted July 14, 2008 Thank you works fine! Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/#findComment-589269 Share on other sites More sharing options...
Lodius2000 Posted July 14, 2008 Share Posted July 14, 2008 thanks for the clarifier ken, I didnt know that Link to comment https://forums.phpfreaks.com/topic/114608-solved-php-code-in-a-string-variable/#findComment-589276 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.