BlueFox
BlueFox

Reputation: 171

HTML in PHP which contains PHP

Is it possible to put PHP-code surrounded by HTML which is surrounded by PHP? Here is a little code-snippet. All of that is in PHP-brackets. The browser displays the table but not the PHP values.

echo "<tr>";
echo '<td class="tg-031e"><?php echo $Auftragsnummer;?></td>';
echo '<td class="tg-031e"><?php echo $Modellbezeichnung;?></td>';
echo '<td class="tg-031e"><?php echo $Untersuchungsart;?></td>';
echo '<td class="tg-031e"><?php echo $TUEV_Stelle;?></td>';
echo '<td class="tg-031e"><?php echo $TUEV_Termin;?></td>';
echo "</tr>";

Regards Bluefox

Upvotes: 0

Views: 89

Answers (6)

Mehdi Brillaud
Mehdi Brillaud

Reputation: 1866

Concatenation

echo '<td class="tg-031e">' . $Auftragsnummer . '</td>';

But if you have a lot of HTML it's better to close your php tag

<?php your code ?>
  <tr>
   <td class="yourClass"> <?php echo $yourVar; ?> </td>
  <tr>

Upvotes: 6

Adam T
Adam T

Reputation: 675

Using sprintf().

<?php
echo sprintf('<td class="tg-031e">%s</td>', $Auftragsnummer);

Or, as a loop/array construct:

foreach(array(
   $Auftragsnummer, 
   $Modellbezeichnung, 
   $Untersuchungsart, 
   $TUEV_Stelle, 
   $TUEV_Termin) as $item) {
    echo sprintf('<td class="tg-031e">%s</td>', $item);
}

Upvotes: 0

xNeyte
xNeyte

Reputation: 622

You can go for HEREDOC :

$html = <<<HTML
            <tr>
                <td class="tg-031e">{$Auftragsnummer}</td>
                <td class="tg-031e">{$Modellbezeichnung}</td>
                <td class="tg-031e">{$Untersuchungsart}</td>
                <td class="tg-031e">{$TUEV_Stelle}</td>
                <td class="tg-031e">{$TUEV_Termin}</td>
            </tr>
HTML
;

echo $html;

Upvotes: 1

keikoku92
keikoku92

Reputation: 151

you do not need to close the php bracket do more like this

 echo '<td class="tg-031e">' .$Auftragsnummer. '</td>';

Upvotes: 1

Ben
Ben

Reputation: 9001

Look at Concatenation.

You can put a string in an echo command like so:

$string = 'hello world';
echo 'I wanted to say '.$string.'!'; //outputs "I wanted to say hello world!"

so your code should look like:

echo '<tr>';
    echo '<td class="tg-031e">'.$Auftragsnummer.'</td>';
    echo '<td class="tg-031e">'.$Modellbezeichnung.'</td>';
    echo '<td class="tg-031e">'.$Untersuchungsart.'</td>';
    echo '<td class="tg-031e">'.$TUEV_Stelle.'</td>';
    echo '<td class="tg-031e">'.$TUEV_Termin'.</td>';
echo '</tr>';

Upvotes: 1

Random
Random

Reputation: 3236

echo is PHP. So you should just do something like this :

echo '<td class="tg-031e">'.$Auftragsnummer.'</td>';

Upvotes: 0

Related Questions