unknown os platform
Free Beginner PHP Tutorials - Embedding PHP inside HTML | Whats-online.info

Free Beginner PHP Tutorials - Embedding PHP inside HTML

2016/08/26 / Science and Tutorials

How to embed PHP string or text inside an HTML tag tutorial. See example:-

Embedding PHP in Web Pages

Although it is achievable to write and run standalone PHP programs, mostly, PHP code is embedded in either HTML or XML files. Processing such data involves replacing each chunk of PHP source code with the output it produces when executed.

There are several techniques one can use to embed PHP inside HTML code. For this tutorial I will show how to embed PHP inside HTML file using, the PHP makers, include and require functions.

Using PHP markers

You can directly embed PHP code snippets by using  <?PHP  ?>  markers as shown below.

<!doctype HTML public "-//w3c//dtd HTML 4.0 transitional//en"> <html>   <head>           <title>Embedding PHP inside HTML!</title>    </head> <body> <p>        Embedding PHP inside HTML!:<br />   <?php              echo "Hello, world";  ?><br />   How cool is that? </p> </body> </html>

The above code snippet will output,

Embedding PHP inside HTML!:
Hello, world
How cool is that?

Note that; there's no trace of the PHP source code from the original file. The user sees only its output because PHP is a scripting server side language and does not output its tags, unlike HTML.

using require and include functions

You can use require and include functions to PHP output results inside HTML tags. This requires you create a separate file besides the HTML file.

For instance, we will create two files. An HTML file and a PHP file. Like I, I save all my files either HTML or PHP files with an extension of PHP.

Create a file using your code editor and save it as index.php. This will handle the HTML code snippets. Include the following code which I will explain it later.


<html>   <head>           <title>Embedding PHP inside HTML!</title>    </head> <body> <p>        Embedding PHP inside HTML!:<br />   <?php          include"php-code.php"; ?><br />   How cool is that? </p> </body> </html>

Secondly, create another file using your code editor and save it as PHP-code.php. This will handle the PHP code snippets.


<?php   echo "Hello, world"; ?>

Include and require functions are used in the same way. Besides, you can use the two functions with or without parenthesis as shown below.

include("PHP-code.php") and include"PHP-code.php" are the same.

Include and require functions adds the PHP code inside an HTML code.

You may also like:

Leave a comment

Subscribe to get replies direct to your mailbox

Submit comment