Contact for Expert ServicesAdvertise with Ecomspark.

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.

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

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.

php-in-html

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 –

<html>
<head>PHP HTML Integration</head>
<body>

<ul>

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

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

</body>
</html>

This will out put as –

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

Add a Comment

Your email address will not be published. Required fields are marked *

Translate »