[thelist] building a dynamic variable in php

Carl J Meyer cjmeyer at npcc.net
Mon Jan 13 12:32:00 CST 2003


Hi Tom,

On Mon, 2003-01-13 at 10:14, Tom Dell'Aringa wrote:
> -------------------------------------------------
> for($i = 1; $i<=10; $i++)
> {
> 	$var = "mail" . $i;
> 	${$var} = $_POST[$i];
>
> 	$body = "body of email";
>
> 	mail(${$var}, "From $personA/$personB", $body, "From:
> $email\r\nReply-To: $email\r\n");
> }
> --------------------------------------------------

Yes, this code is a bit silly :-)  Why go to all the fuss of the dynamic
variable when you're doing the mail() right there in the loop?  You
could cut this down to:

----------------
for($i=0; $i<=10; $i++) {
  $body = "body of email";
  mail($_POST[$i], "From $personA/$personB", $body, "From:
$email\r\nReply-To: $email\r\n");
}
---------------

And you haven't lost anything but your confusion :-)

In this case the only reason I can see why you'd want to transfer the
address from $_POST to another variable is so you can do some validation
/ checking on it (you could also do that straight to _POST but I think
it's better to avoid modifying posted values directly).  Even if you do
that, within the loop you might as well use the same variable (say
$mail) every time (who cares if it's overwritten next time through?) and
not bother with the dynamic variables.

Carl




More information about the thelist mailing list