Question:
Will PHP commands work in an html file?
?
2009-08-05 19:42:29 UTC
Or do they have to be a .php file to work?

If they have to be in a .php file, can someone explain why a .php can have HTML, but an .html won't accept PHP commands...
Six answers:
Will
2009-08-05 19:50:40 UTC
Yes, you can put html in a .php file but you can't put php in a .html file. As far as I personally know, I don't know why you can't put php in a .html file, there's no rhyme or reason.



-Billy
gd7
2009-08-06 03:02:00 UTC
In order to answer the question, I'll give you a little summary of the way PHP works.



Let's say I go to example.com/example.html



The server that hosts example.com will receive the request for example.html. It'll look for example.html in its filesystem. Then it'll sent the HTML file to my computer, and my browser will display it.



Now let's say I go to example.com/example.php



The server gets the request for example.php. Let's assume that the server has the PHP software (let's call it php.exe) installed and running, which is needed to use PHP. When it gets ready to serve the file I requested, it looks at the extension on the file. If the file has the .php extension, the server will pass the PHP file to php.exe. Then php.exe will parse the PHP in the file and run all the commands. Then php.exe will sent the results of the commands back to the server. Then the server will send that file to my computer where it is displayed by the browser.



By default, the server will only send a file to php.exe if the file has the .php extension. If the file has the .html extension, is skips php.exe and sends it to my computer directly.



The server doesn't even know whether there are PHP commands inside the .html file--that's php.exe's job. The only thing the server knows is the file's extension. If it has the PHP extension, it'll send it to php.exe regardless of whether or not there are PHP extensions in the file.



HOWEVER...



It's possible to customize a server. You can specify which file extensions you want the server to treat as PHP files. On my server, just for fun, I configured it so that files ending in .z will be sent to php.exe.



There are a couple ways to do this customization. If you're setting up your own server, you can set it up however you want. If you have a website hosted somewhere else, different hosting companies will give you different privileges for customization.



If you're allowed to use .htaccess files in your hosting account, you can add the following line to the main .htaccess file:

addhandler x-httpd-php5 .html

(This assumes the PHP version is PHP5.)

Change ".html" to whatever extension you want, as long as it doesn't conflict with any other special types of extensions. Keep in mind that if you do this for HTML files, this will make every HTML file a little bit slower to serve, because every HTML file will then be sent to php.exe even if it doesn't have PHP code in it. But, this will make PHP code work in HTML files.



If you have a GoDaddy hosting account, you can actually go into your control panel and add extensions to be treated as PHP.



If you want to add this functionality, I suggest that you call your hosting provider and ask if they support this functionality.
just "JR"
2009-08-06 07:22:40 UTC
On the web, you have TWO entities: the Client (with its browser), and the Server (where the website is stored).

A server NEVER send anything to a Client, unless requested.

The user (Client) calls a Server. The server replies. The browser displays.

HTML resides on the Client. Php resides on the server.

A file on the server with extention ".htm(l)" will be sent directly to the Client: if it contains php functions, these will NOT be processed.

A file on the server with extention ".php" will be sent FIRST to the Php processor (also on the server). The Php code is executed, and its output is an HTML file that is, finally, sent to the Client.

PhP stands for PRE-hypertext-Processor: it does run BEFORE "html".
rock da party
2009-08-06 03:10:07 UTC
PHP is server side scripting language it executes on server php engine translates and returns a plain html file to browser. When you use php tags in html file it can never be translated into plain html as it doesn't encounter php engine. You got name files that contain php tags with .php extension as by default when you create and place .php files in the folder of server such as httdocs of apache it translates it else you need to have some directives.
Andy T
2009-08-06 03:28:49 UTC
Often has to be in .php file, have not seen it before but .php can be changed to .whatever in a configuration file.



A piece of HTML code is indistinguishable to text as far as PHP is concerned; the other way is technically out of HTML processing league but often angular brackets are accepted and thus a piece of PHP code is text as far as HTML is concerned. So they end up as mere text to each other in their respective processing.



However the magic is where PHP gets processed and thus eclipses HTML in scope but not in functionalities. And again that .php thing.
Abraham
2009-08-06 02:56:19 UTC
yes as long as you place the php tags inside of your html



for example:







Sample





Test









echo "Hello World!"; // This will print out Hello World!

?>










This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...