Reputation: 3544
I have a simple mail() script that sends to multiple emails dynamically by seperating the emails with commas.
<?php
$sendto = array("[email protected]", "[email protected]", "[email protected]");
foreach ($sendto as $email)
{
if ($email is the last indice in the array..) // <-- here
$to = $email;
else
$to = $email . ', ';
}
$subject = "test message";
$msg = "this is a test";
if (mail($to, $subject, $msg))
echo "message sent";
else
echo "uh oh";
?>
How do I identify if (this is the last piece of data in my array) ?
Upvotes: 0
Views: 90
Reputation: 8420
Just make your if clause use the end
function:
if ($email == end($sendto))
Upvotes: 1
Reputation: 16688
php includes implode
or join
which will work much better here:
$to = implode(', ', $sendto);
Upvotes: 3