Home » Web development » PHP » How to Use HTML Inside PHP on the Same Page

How to Use HTML Inside PHP on the Same Page

  • We know that it is a web server that processes a PHP file before sending the processed results to the web browser. But the question is how does a server know whether a file is a PHP file or not. Answer is by default servers look for PHP only in files that end with the .php extension.
  • Now a PHP file can contain elements for example HTML tags that are not part of your PHP script and how would a web server distinguish between a PHP script and other elements of the web-page Answer to this question is server recognizes a PHP script when it sees PHP delimiters which contains your PHP script.
  • To begin a PHP script, you must include a opening delimiter <php and start coding. To finish,
  • you simply add ?> to the end of the script. Anything outside of these delimiters will be treated as HTML or plain text.
  • There are two ways we can put HTML Inside PHP
    (a) Html code outside PHP Tags
    (b)Html code inside PHP tags and using echo or print to show those HTML elements
  • Let check in detail now both the ways

Html code outside PHP Tags

Given below is the example I first used to embed PHP code in a HTML file but since you are putting PHP code in the file you would have to give “PHP” extension to the file

<html>
<body>
<?php
$name="your name";
print $name;
?>
<hr>
<b>
<?php
print $name;
?>
</b>
</body>
</html>

If you execute this page you will get following result as shown below in this figure

HTML Inside PHP

Here in this file as you can see  that PHP code is being put inside html tags namely HTML tag and BODY tag and php code is written inside PHP delimiters (lines 4 and 7) $name=”your name”; is a variable that stores the string inside ” ” and here string stores in variable name is Your name $ is used to declare a variable in PHP. Right now this is the only thing I know about variables and how to declare them in PHP. I am planning to read more about it later and then I’ll record what I understand through the blog post. print $name; Here I have used Print to show the value stored in the variable $name .

See also  PHP tutorial: Variables in PHP

According to php.net

print is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.

There is more to this function I suppose and will study it in detail. I could also have used echo to obtain the same thing. In line 10 see that I have put PHP code inside the bold html tag (<b>) which resulted in bold faced “your name” string. So this way you can put your PHP scripts inside any HTML tags. There are other alternative PHP delimiters you can use to tell server to distinguish between your php script and other webpage elements.
Although these alternative PHP delimiters can be used but I have read that these forms should be avoided and you in practice should use following PHP delimiter as used firstly inside HTML file

Html code inside PHP tags and using echo or print to show those HTML elements

We can use html tags inside PHP also as given below

<?php
echo "<html>"
echo "<body>"
$name="your name";
print $name;
echo "<hr>"
echo "<b>"
print $name;
echo "</b>"
echo "</body>"
echo "</html>"
?>

So we can basically echo all the HTML construct and get it working.Another example could be

<?php
$name='your name';
echo '<table>
<tr><th>Name</th></tr>
<tr><td>'.$name.'</td></tr>
</table>';
?>

Hope you like post on HTML Inside PHP

Also Reads
How to run php file in windows : This post talks about how we can php file on the the window and do the testing before moving to the web server
uses of PHP : This has detailed description why PHP is so much used through out the world.
first php program : This is good starting point for anybody starting with PHP and talk in good detail about how to write your PHP program and execute it
Define constant in PHP
https://en.wikipedia.org/wiki/PHP

Leave a Comment

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

Scroll to Top