HTML inside "echo" php

Asked

Viewed 6,310 times

2

I’d like to know how I get through it:

<td><a href="edita.php?id=<?php echo $objProg->getid();?>">Alterar</a></td>

Within a echo in php ?

  • 1

    <?php echo '<td><a href="edita.php? id=' . $objProg->getid() . '">Change</a></td>'; ?>

2 answers

12


You can concatenate (that is to add two or more strings) into your "echo" to have that result (as already posted as comment).

<?php
echo '<td><a href="edita.php?id=' . $objProg->getid() . '">Alterar</a></td>';
?>

Another method that "recommend" you to know and test would be the Heredoc which will allow you to add variables without worrying about quotation marks and etc., having the same effect.

<?php
echo <<<EOT
<td><a href="edita.php?id=$objProg->getid()">Alterar</a></td>
EOT;
?>

1

You can use it the way SK15 said, or you can instead use echo to write directly to HTML and just insert the info in php for example:

<td>
   <a href="edita.php?id=<?=$objProg->getid();?>">
       Alterar
   </a>
</td>

Utilizes the <?= to save code and speed up processing with a clean amis code! And it makes... And if that piece of code is used inside an if, or Else, or while, or for... Close php before it and open again after!

  • Remember that this form is affected by the directive short_open_tag in PHP version less than 5.4.0 it will not work if it is not active only in PHP 5.4.0 onwards it is not affected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.