php - 在变量中放入一个for循环

I am trying to echo a for loop inside a html mail message, the loop is

for($i=0; $i<$arrlength; $i++)
{
echo $mailroom[$i] ;
if ($i<($arrlength-1) )
{
echo " &amp; ";
}
}

It is printing the results perfectly, but it not printing any result at the html message, the html message is

$headers = "From: ". "XXXX" . "<" . $frommail . ">
";
$headers .= "Reply-To: " . $frommail . "
";
$headers .= "Return-path: ". $frommail;
$headers .= "MIME-Version: 1.0" . "
";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "
";


$sendmessage = "
<html>
<head>
<title>XXXXXX DETAILS</title>
</head>
<body>
<p>DATA FOR XXXXXXXXXX</p>
<table border=1>
<tr>
<th>Booked on</th>
<th>Name</th> 
<th>Bank</th>
<th>UIN</th>
<th>Phone</th>
<th>From</th>
<th>To</th>
<th>Room No.s</th>
<th>Tariff</th>
<th>Caution Money</th>
<th>Courier</th>
<th>Bank Charges</th>
<th>Total Received</th>
</tr>
<tr>
<td>$mailtoday</td>
<td>$name (ESP)</td>
<td>&nbsp;</td>
<td>$uin</td>
<td>$phone</td>
<td>$mailfrom</td>
<td>$mailto</td>
<td>
**for($i=0; $i<$arrlength; $i++)
{
echo $mailroom[$i] ;
if ($i<($arrlength-1) )
{
echo " &amp; ";
}
}**
    </td>
<td>$room_total</td>
<td>$c_money</td>
<td>$courier</td>
<td>$b_charges</td>
<td>$totalreceived</td>
</tr>
</table>
</body>
</html>
";
`

Can I put the for loop inside a variable so that I can use it withing the html message or otherwise later.

use this code, you missed php opening <?php and closing ?> tags

$sendmessage .= ".........<td>";
for($i=0; $i<$arrlength; $i++)
{
    $sendmessage .= $mailroom[$i] ;
    if ($i<($arrlength-1) )
     {
       $sendmessage .= " &amp; ";
   }
}
$sendmessage .= "</td>.........";

You have to close off your string before attempting to use a non-string value. In this case I'd do like this:

"<td>$name (ESP)</td>
<td>&nbsp;</td>
<td>$uin</td>
<td>$phone</td>
<td>$mailfrom</td>
<td>$mailto</td>
<td>" . implode(' &amp; ', $mailroom) . "</td>
<td>$room_total</td>
<td>$c_money</td>"

You cannot use a for-loop (or any other statement for that matter) in a string.

Instead you need to concatenate your string inside the loop. For example:

$myString = "test ";
for($i = 0; $i < 3; $i++) {
  $myString = $myString . "$i, ";
}
$myString = $myString . " end!";
echo $myString; // shows "test 1, 2, 3, end!"

(I created this small example, as you code snippet is quite long, but the same applies)

No,You can't do this. The double quotation marks is for variable replacement. Not for code running.

Try this

$headers = "From: ". "XXXX" . "<" . $frommail . ">
";
$headers .= "Reply-To: " . $frommail . "
";
$headers .= "Return-path: ". $frommail;
$headers .= "MIME-Version: 1.0" . "
";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "
";


$sendmessage = "
<html>
<head>
<title>XXXXXX DETAILS</title>
</head>
<body>
<p>DATA FOR XXXXXXXXXX</p>
<table border=1>
<tr>
<th>Booked on</th>
<th>Name</th> 
<th>Bank</th>
<th>UIN</th>
<th>Phone</th>
<th>From</th>
<th>To</th>
<th>Room No.s</th>
<th>Tariff</th>
<th>Caution Money</th>
<th>Courier</th>
<th>Bank Charges</th>
<th>Total Received</th>
</tr>
<tr>
<td>$mailtoday</td>
<td>$name (ESP)</td>
<td>&nbsp;</td>
<td>$uin</td>
<td>$phone</td>
<td>$mailfrom</td>
<td>$mailto</td>
<td>";
for($i=0; $i<$arrlength; $i++)
{
$sendmessage.= $mailroom[$i] ;
if ($i<($arrlength-1) )
{
$sendmessage .= "&amp; ";
}
}

$sendmessage.=" </td>
<td>$room_total</td>
<td>$c_money</td>
<td>$courier</td>
<td>$b_charges</td>
<td>$totalreceived</td>
</tr>
</table>
</body>
</html>";