How to write PHP code in HTML?

For a experience php web developer  it will be a very easy thing. But for a new developer , who recently started learning php programming it will be a questionable thing. So today we will embed php code in regular html code.

Embedding PHP code in regular HTML –

Lets  create a hello script named hello.php. For php code, please always save file with .php extension.

<title>PHP Test</title>
<?php echo '<p>Hello World</p>'; ?>

In above html code, we printed Hello world in php code. To write php code in html we should use <?php ?> tags. Integrating php and html is very easy thing.


We can write more complex php code in html. We need to just start php code with <?php tag and end with ?> tag.

More advanced example –

<head>PHP HTML Integration</head>


<?php for($i=1;$i<=5;$i++){ ?>

<li>Item No <?php echo $i; ?></li>
<?php } ?>


This will out put as –

Item No 1
Item No 2
Item No 3
Item No 4
Item No 5

Leave a Reply