Difference between direct and embedded PHP code

In this article, you going to see what is the difference between direct and embedded PHP code in detail step by step.

We also going to cover up some important points regarding are as follow as.

  • How to run PHP code in the HTML file.
  • A PHP include.
  • PHP in HTML example.
  • What is PHP tag?
  • A common way to display HTML in PHP.

Let’s start with the point of direct and embedded PHP code.

Embedded PHP code: Embedding PHP in web pages

Embedded PHP is a declaration of PHP code inside different html tag.

Mostly the PHP code is used to dynamically generate the out inside the HTML document.

Below is the example how the PHP tag is declared inside HTML tag.

But first what is this PHP tag and how its declared.

   <title><?php echo $varTitle; ?></title>

Now Important point : What is PHP tag and how its declared?

Actually everyone know that PHP is script which is executed on server and plain HTML result is sent back to browser.It can be placed anywhere in the document.

What is PHP tag? :

Its start with tag '<?php' and ends with '?>'.

In the above code there is a declaration of PHP code in HTML tag.

Example :  <?php echo $varTitle; ?>

And main point is default file extension for PHP file ".php".

Php in HTML example

Below is example of a simple PHP file which consist of PHP code with HTML tag called Embedded PHP, which gives output

Output : “Sample code”.

<!DOCTYPE html>
  <h1>My first PHP page</h1>
    echo "Sample Code";

Another important point is PHP script can also embedded inside script tag, from below example you can understand how the PHP script is embedded inside script tag.

<script language="php"> echo "Sample php script"; </script>

Common way to display html in PHP

As from above points you come to understand there any difference style of embedding PHP script.

Now one more interesting point regarding PHP script is that you can also display html using PHP.

Below is example of displaying of HTML using PHP script.

	echo "<div>". $varTitle ."</div>";

As you can see in the above PHP script, how html “Div” tag is use inside PHP for displaying HTML.

How to display data in Div using PHP

How to run PHP code in HTML file

We going to see this topic of running of PHP script in html file via an example.

Below is the example of html file in which first declared html tag.

And after the tag declaration PHP script is returned.

<h1>A HTML tag</h1>
	echo "PHP script";

After executing above code it give output as A HTML tag.

Adding of PHP script below HTML tag in HTML file, it doesn’t display because the browser didn’t recognize.

In order to write PHP script in HTML file you need an .htaccess.

Copy your .htaccess file to you project folder and open it to add some code as the following.

AddType application/x-httpd-php .htm .html

After adding above code in htaccess file reload page again you will get output of PHP script which is present in html file.

Output :
A HTML tag
PHP script

A PHP include:

Similarly we can also include PHP file in that HTML.

Below is example for PHP include.

<?php include("sample.php"); ?>
<h1>Sample HTML tag</h1>
	echo "This is a PHP script";

Write some code in sample PHP file

<?php echo "A Sample PHP script"; ?>

Output: A Sample PHP script
Sample HTML tag
This is a PHP script

This is all points regarding Embedding PHP script.

Direct PHP.

Now point of discussion regarding direct PHP. Direct PHP is nothing but the only PHP script.The PHP file which consists of the only PHP script.

Below is example of PHP file consist only PHP script.

$t = date("H");

if ($t < "10") {
  echo "good morning!";
} elseif ($t < "20") {
  echo "good day!";
} else {
  echo "good night!";


Finally, we do with the discussion regarding points such as the difference between direct and embedded PHP including related topics on how to run PHP code in HTML file, PHP include, PHP in HTML example, PHP tags, and a common way to display HTML in PHP.

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.

Leave a Comment