PHP TCPDF heredoc

I got this in my heredoc in my TCPDF. I basically want to create a dynamic pdf with the data of my database.

        $html = <<<EOD
        <table border="1">
            <thead>
                <tr>
                    <th>firstname</th>
                    <th>lastname<th>
                </tr>
            </thead>
            <tbody>
                <tr>
                   <th></th>
                   <th></th>
                </tr>
            </tbody>
        </table>
EOD;

I want to make this dynamic like this, to create the data from my database dynamically.

    <?php
    foreach($result_set as $result) {
    ?>
    <tr>
        <td>
            <?php echo $result['firstname']; ?>
        </td>
        <td>
            <?php echo $result['lastname']; ?>
        </td>
    </tr>
    <?php
    }
    ?>

I tried this so far but I cannot find a suitable solution:

        $html = <<<EOD
        <table border="1">
            <thead>
                <tr>
                    <th>Vorname</th>
                    <th>Nachname</th>
                    <th>Von</th>
                    <th>Bis</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <?php echo $result['firstname']; ?>
                    </td>
                    <td>
                        <?php echo $result['lastname']; ?>
                    </td>
                </tr>
            </tbody>
        </table>
EOD;

can anyone help please.

Ok I somehow did it, but still if there is any better solution feel free to post:)

Code:

    $loopHereFirstname = '';
    $loopHereLastname  = '';
    foreach($result_set_random_01 as $result_dish_usr_01) {
        $tr_start = '<tr>';
        $tr_end   = '</tr>';
        $td_start = '<td>';
        $td_end   = '</td>';
        $loopHereFirstname .= $result_dish_usr_01['firstname']."
";
        $loopHereLastname  .= $result_dish_usr_01['lastname']."
";
    }

    $html = <<<EOD
    <table border="1">
        <thead>
            <tr>
                <th>firstname</th>
                <th>lastname</th>
            </tr>
        </thead>
        <tbody>
            $tr_start
                $td_start
                    $loopHereFirstname
                $td_end
                $td_start
                    $loopHereLastname
                $td_end
            $tr_end
        </tbody>
    </table>

EOD;

You've to break your code into two part if you want to use a loop with <<

<?php 
     $html =<<<EOD
     <table border="1">
        <thead>
            <tr>
                <th>Vorname</th>
                <th>Nachname</th>
                <th>Von</th>
                <th>Bis</th>
            </tr>
        </thead>
        <tbody>
     EOD;

foreach($result_set as $result) {
$html.=<<<EOD
<tr>
    <td>
        {$result['firstname']}
    </td>
    <td>
        {$result['lastname']}
    </td>
</tr>
EOD;

}


$html .= '</tbody></table>';