lindm Posted August 28, 2007 Share Posted August 28, 2007 Is it possible to execute a return function a certain times based on a variable containing a number. I don't want echo or anything so. This is my code that doesn't work: <?php $file = fopen("formsaved.html", "w") or exit("Unable to open file!"); function board($number) {for ($num=1; $num<=10; $num++ ) { return "test"; } } $stringData = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> </head> <body> <form id="form" name="form" method="post" action="writeform.php"> <table width="610" border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="top">'.board(5).'</td> </tr> </table> </form> </body> </html> ' ; fwrite($file, $stringData); fclose($file); header("Location: formsaved.html"); ?> In short. The above should return, $number of times...but doesn't per definition...any solutions? The reason I use return and not echo is becuase the above code is part of a variable containing html code to be written to a file... Quote Link to comment https://forums.phpfreaks.com/topic/67088-loop-problem/ Share on other sites More sharing options...
lemmin Posted August 28, 2007 Share Posted August 28, 2007 Just call the same function that number of times. function board() { return "test"; } for ($num=1; $num<=10; $num++) echo board(); Quote Link to comment https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336459 Share on other sites More sharing options...
lindm Posted August 28, 2007 Author Share Posted August 28, 2007 Doesn't seem to go for me. Below is modified code: <?php $file = fopen("formsaved.html", "w") or exit("Unable to open file!"); function board() { return "test"; } $stringData = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> </head> <body> <form id="form" name="form" method="post" action="writeform.php"> <table width="610" border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="top">'.for ($num=1; $num<=10; $num++) echo board();}.'</td> </tr> </table> </form> </body> </html> ' ; fwrite($file, $stringData); fclose($file); header("Location: formsaved.html"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336476 Share on other sites More sharing options...
lemmin Posted August 28, 2007 Share Posted August 28, 2007 echo doesn't return a string so you can't concatinate it to one. Same with for. Try: $stringData = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> </head> <body> <form id="form" name="form" method="post" action="writeform.php"> <table width="610" border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="top">'; for ($num=1; $num<=10; $num++) $stringData .= board(); $stringData .= </tr> </table> </form> </body> </html> '; Quote Link to comment https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336491 Share on other sites More sharing options...
lindm Posted August 28, 2007 Author Share Posted August 28, 2007 It worked, just corrected a small ' missing after "$stringData .=". So it is not possible to have a single function for my problem that is called simply by '.function().' ? <?php $file = fopen("formsaved.html", "w") or exit("Unable to open file!"); function board() { return "test"; } $stringData = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> </head> <body> <form id="form" name="form" method="post" action="writeform.php"> <table width="610" border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="top">'; for ($num=1; $num<=10; $num++) $stringData .= board(); $stringData .= ' </tr> </table> </form> </body> </html> '; fwrite($file, $stringData); fclose($file); header("Location: formsaved.html"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336534 Share on other sites More sharing options...
lemmin Posted August 28, 2007 Share Posted August 28, 2007 You can have a single function return that entire string, but not multiple ones. For example: function board($num) { $stringData = ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> </head> <body> <form id="form" name="form" method="post" action="writeform.php"> <table width="610" border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="top">'; for ($i=1; $i<=$num; $i++) $stringData .= "test"; $stringData .= ' </tr> </table> </form> </body> </html> '; } echo board(10); That returns the big string with the "test" string repeated as many times as you pass to the function. Quote Link to comment https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336574 Share on other sites More sharing options...
sasa Posted August 28, 2007 Share Posted August 28, 2007 can you use echo str_repeat('test', 10); Quote Link to comment https://forums.phpfreaks.com/topic/67088-loop-problem/#findComment-336601 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.