如何在字符串中创建循环

I just want to ask to you, How to make a loop on a string, like this code bellow...

$array = array("a", "B", "c", "D");

$loop = "
foreach($array as $ray):
echo $ray;
endforeach;
";

Note : I am using email function that the body of email should inside of the string, like this

$val = "A";
$to = "bla@bla.com";
$from = "ha@bla.com";
$subject = " bla bla bla";
$body = "
Hahahah
hahaa
$val
";

You have something called Anonymous functions. It's really cool: read the manual here

I don't know what you want to reach, but to get a function in a variable, you can use this. Here is an example of what you have but using Anonymous functions.

<?php
$array = array("a", "B", "c", "D");

$loop = function($array){
    foreach($array as $ray){
        echo $ray;
    }
};

// you can call that function like this. 

$loop($array);
?>

In this case the output would be: aBcD after you call it like that.

If you want to just loop a certain part, do it like this:

<?php
$array = array("a", "B", "c", "D");

foreach($array as $ray){
?>

<div class="footer">
    <?php echo $ray; ?>
</div>
<?php } ?>

Not the cleanest code I've ever done, but it will do the job