Last updated on November 23rd, 2015 at 08:49 am
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 webpage . Answer to this question is server recognises 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.
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
If you execute this page you will get following result as shown below in this figure 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 . 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
I think next I’m going to learn about PHP variables and how to use them in detail.