Include the result of a Foreach loop in a variable in PHP? (for the mail () function)

I am French, so I do not speak English very well.

I am trying to "put" the result of a foreach loop into a variable as follows:

$msg = '<html><head>
        <title>Commande de photos</title>
 </head><body>
        <p>Voici la liste des photos demand&eacute;es :<br />
 <ul> HERE I WANT TO "PUT" THE RESULT OF THE FOREACH LOOP</ul>';

Here is my foreach loop:

foreach($tab as $val){
echo('<li>'.$val.'</li>');
}

Then $msgenter the mail()following into the composition of the function :

mail($destinataire,$sujet,$msg,$headers);

So, how can I do this to include the foreach result in the message because I already have an error?

+3
source share
4 answers
$msg = '<html><head><title>Commande de photos</title></head><body><p>Voici la liste des photos demand&eacute;es :</p><ul>';
foreach($tab as $val){
     $msg .= '<li>' . $val . '</li>';
}
$msg .= '</ul>';
mail($destinataire,$sujet,$msg,$headers);

The trick here is the operator. = concatenation. For example:

$x = 'abc';
echo $x; // echoes 'abc'
$x .= 'def';
echo $x; // echoes 'abcdef'
+4
source

How so?

$list = '';
foreach($tab as $val){
    $list .= '<li>'.$val.'</li>';
}

$msg = '<html><head>
    <title>Commande de photos</title>
</head><body>
    <p>Voici la liste des photos demand&eacute;es :<br />
<ul>'.$list.'</ul>';

mail($destinataire,$sujet,$msg,$headers);
+2
source
ob_start();
// here your loop echo'ing stuff
$content = ob_get_clean();

. php man ob_ *

0

Not much answer, but davethegr8 answer worked for me! I am reviewing my first process of integrating php templates with Savant3, and this has helped keep all the logic separate from the template file. Thanks!

0
source

Source: https://habr.com/ru/post/1717273/


All Articles