0

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

2
  • What does this have to do with CSS? Commented May 19, 2015 at 13:33
  • 1
    I would use sprintf() instead of co-mingling the HTML and php. It does more-or-less the same thing and is easier to read. Commented May 19, 2015 at 13:33

6 Answers 6

6

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>
Sign up to request clarification or add additional context in comments.

1 Comment

My HTML is rather small. Your first solution worked great! I just needed to add a '.' after the variable.
1

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>';

3 Comments

Of course I added it, it clarifies what I'm explaining, it's making the answer better, and will hopefully help the OP understand the reasoning behind using a string in an echo command. I know he's aware of echo, I'm saying that there is a way to put a string in an echo command. What's with the hostility?
LOL what hostility? "clarifies what I'm explaining" - You aren't explaining anything in your answer so I don't understand what you mean. "OP understand the reasoning" - what reasoning?
"I like how you added concatenation after you noticed the other guy got some upvotes" "why are you even mentioning echo" - sorry if I misread that, but it felt a bit aggressive mate
1

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

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

Comments

1

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;

1 Comment

I wish more people used HEREDOC. Much cleaner code
0

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

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

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.