pTutorial

New way to learn PHP

PHP and html embed code

How to embed PHP script in html

PHP script is embedded anywhere and any number of times in HTML code, following example explains how to embed PHP script into HTML.

Html embedded PHP script


<html>
<head>
<title>html embedded code</title>
</Head>

<?php

echo "hello friends"."<br />"; 
echo "welcome to the PHP<br />"; 
echo "Html and php script embed";
?>

<?php

$a=15;
$b=25;
$sum=$a+$b;
echo $sum;

?>

<body>
</body>
</html>

  • You can also put the PHP script into the head section, like the title.
  • You can put multiple scripts in a single PHP file.
  • You can place PHP script anywhere in html page.

How to embed html code in PHP script.

Html code embeds anywhere in the PHP script. Following example explain, how to embed PHP script in HTML.

Php embed html code

<?php

echo "<h4>welcome to the PHP</h4>"; 

echo "<br />"; 

echo "Html and php script embed<hr>";

echo "<b>"."hello friends"."</b>";

print "<hr>"; 

?>

We used heading tag (h4), hr tag and br tag in the PHP script as shown above example.

Example of table using Php

<?php

echo "<table width='300' border='2'>"."<tr>";
echo "<td>"."Name"."</td>";
echo "<td>"."Roll number"."</td>";
echo "<td>"."Branch"."</td>";
echo "</tr>"."<tr>";
echo "<td>"."Rohan"."</td>";
echo "<td>"."C-11-38"."</td>";
echo "<td>"."Computer"."</td>";
echo "</tr>";
echo "</table>";

?>

Output

NameRoll numberBranch
RohanC-11-38Computer

In the above example only echo statement is used to create whole table. You can use any of Html tags in your PHP script using echo.

Echo construct is the best option for embedding HTML tags into the PHP script because echo construct can accept multiple arguments.

.

You can place HTML tags anywhere in a PHP script using echo and print statement.


QUOTES OF THE DAY

A single death is tragedy, a million death is a static.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER