62

I want to create a table with <?php ?> script.
How can I insert html code into <?php ?> to achieve my goal?

<?php html code to create table ?>
2
  • 2
    why do you want to do this? What is the example you have in mind? Commented Aug 9, 2013 at 5:04
  • I want to write <tr> <td><?php echo $rst4 ?>&nbsp;:&nbsp;</td><td><?php echo $marks4 ?> </td></tr> inside my php code .. Commented Aug 9, 2013 at 5:10

4 Answers 4

125

You can do like

HTML in PHP :

<?php
     echo "<table>";
     echo "<tr>";
     echo "<td>Name</td>";
     echo "<td>".$name."</td>";
     echo "</tr>";
     echo "</table>";
?>

Or You can write like.

PHP in HTML :

<?php /*Do some PHP calculation or something*/ ?>
     <table>
         <tr>
             <td>Name</td>
             <td><?php echo $name;?></td>
         </tr>
     </table>


<?php /*Do some PHP calculation or something*/ ?> Means:
You can open a PHP tag with <?php, now add your PHP code, then close the tag with ?> and then write your html code. When needed to add more PHP, just open another PHP tag with <?php.

Sign up to request clarification or add additional context in comments.

5 Comments

Second option is much better!
Yes @Phil because we can have the more flexible way to add more html..
What if I want to break my HTML page into multiple parts for convenience e.g. header.php, footer.php. And later include in other pages. For footer, the second method is useless, and first is neither of use.
@SantoshKumar did he mentioned to create a separate views? No right..Acc to his question I answered it..And one more thing even if he separated the views the variables will work since we are including the pages..As like we do it Codeigniter
What about the <<<HTML in PHP?
23

You can drop in and out of the PHP context using the <?php and ?> tags. For example...

<?php
$array = array(1, 2, 3, 4);
?>

<table>
<thead><tr><th>Number</th></tr></thead>
<tbody>
<?php foreach ($array as $num) : ?>
<tr><td><?= htmlspecialchars($num) ?></td></tr>
<?php endforeach ?>
</tbody>
</table>

Also see Alternative syntax for control structures

2 Comments

What does the <?= do? I couldn't get it to work unless I replaced that with <?php>
@abettermap it's short for <?php echo and is enabled by default in PHP from version 5.4
6

Try it like,

<?php 
    $name='your name';
    echo '<table>
       <tr><th>Name</th></tr>
       <tr><td>'.$name.'</td></tr>
    </table>';
?>

Updated

<?php 
    echo '<table>
       <tr><th>Rst</th><th>Marks</th></tr>
       <tr><td>'.$rst4.'</td><td>'.$marks4.'</td></tr>
    </table>';
?>

1 Comment

but my html code i.e table contains some php values like... <td><?php echo $rst4 ?>&nbsp;:&nbsp;</td><td><?php echo $marks4 ?> </td>
2

Can place code anywhere

<input class="my_<? print 'test' ?>"  />

1 Comment

I know this is WAY late to the party, but this can now be done a little easier this way (IMO): <?php echo "<input class='my_$test' />"; ?> assuming you have a variable holding $test;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.