How to run a multiline PHP script in HTML page? - php

I am trying to wrap my head around php and html, and wanted to ask some help with what I guess are basics.
I am aware that you can run a php script on a single line in a .html page with tags. The problem comes when I have a longer script on multiple lines (It generates a table) that I run from localhost:
<?php
$rows = 10; // define number of rows
$cols = 10;// define number of columns
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
echo "<td>Fill</td>";
}
echo "</tr>";
}
echo "</table>";
?>
The above will come out as plain text after first ">" sign, so I guess for some reason the page reads it as I close the php snippet and executes rest as plain HTML (I run the file through apache). I am aware that you can rename the file to .php instead, and get whole thing working, but I want to understand know how PHP behave on HTML pages.
So I have two questions:
1) Is it possible to make the .html read the above code snippet without breaking it apart? I've tried htmlspecialchars, didn't help.
2) Is it possible to have the above snippet as a separate .php file, that I call from .html page like I can call a javascript function? I've tried <?php include 'field.php';?> which didn't work. I've looked into .htaccess, but it seems that's a solution for live versions, which didn't work for me.
Thank you!

The above will come out as plain text after first ">" sign,
PHP is not being processed. Probably because you're using the .html extension. Use .php.
.html will only work if you specifically tell Apache to treat .html as a PHP file.

It looks like you actually aren't evaluating the PHP at all. I suggest you view source, and then you'll see that the browser is actually trying to render all of this as a tag in the document:
<?php
$rows = 10; // define number of rows
$cols = 10;// define number of columns
echo "<table border='1'>
While it isn't impossible to have files with the extension .html render PHP (or Perl, or Python, or what have you), it is uncommon for a server to have that set up by default. I suggest changing the file's extension to .php. If your server is set up to process PHP at all, that will cause it to properly evaluate the script.

You need to set Apache to read HTML files as PHP. Find your php.conf, its usually is the etc directory like : /etc/apache/mods-enabled/php.conf
Add the following to the file:
<FilesMatch ".+\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
And then restart the apache service:
service apache restart
The modules on your server might have different names, too. Apache may be apache or apache2 and your PHP may have the version after the name like php5. This should be reflected in both the directory in which your PHP conf file is located and the service that you restart.
Once your conf file is updated, Apache will treat all html files as PHP and you should be able to put as much PHP code as you want throughout the file. Note: The PHP parser will only work if you use <?php to open (or <? open if short tags are enabled) and ?> to close your PHP bits.

Related

How can I use PHP in HTML? do I need to include a tag? [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 3 years ago.
I am trying to do something basic with PHP and HTML first before I get into something big. However, my html page doesn't seem to be processing PHP correctly.
I have the following php code in my html:
<?php echo '<p>Hello World</p>'; ?>
However, the output on the html page is:
'Hello World' on one line
but:
'; ?>
follows hello world
How can I fix this so I get 'hello world'?
PHP doesn't run on browsers, it's executed on the server. To work with PHP on your local machine, you'll want to set up a server, and write in HTML and PHP in a file with a .php extension, not a .html one. The easiest way to get started would be to use xampp as a beginner.
It looks like you are trying to put php code inside a .htm or .html file.
I ran this code as just html and got the following:
Hello World
'; ?>
So, it is properly interpreting the <p></p> tags but everything else is meaningless to it and doesn't know what to do with what comes after it, so it just prints it as is.
To use php, you need to be doing it inside a .php file and you need to be accessing it from a server that recognises .php files. If you are doing this locally, simply opening the file in a browser won't work by default. You will need to setup a local web server that is running a version of PHP. if you don't have much experience, I recommend WAMP because it is easy to set up and run php with.
If you are doing this on a website that is actually hosted (not local), most of them have support by default for PHP, so if you are using a .php file and it isn't working, you should contact the host or read their documentation to figure out how to get their servers to interpret php files.

When would I want to use .html, vs. .php, as a file extension?

I've noticed that the .html and .php file extensions can be interchanged without apparent effect. Why would I want to use one file extension over the other?
A page ending in .php can include both HTML and/or PHP code (also javascript, css, etc inside their appropriate tags). Note that it is perfectly fine for a page without any PHP code to still have the .php extension.
However, if your page does include PHP code, the filename extension must be .php. Try it - on most web servers this won't work:
FILENAME: test.html
<?php
echo 'Hello there';
The above page will be blank. But if you rename it to test.php, you will see the hello message.
Filename extensions are also an indicator to yourself, or other programmers, as to what type of code the file contains. It is clear at a glance that a file ending in .HTML does not contain any PHP code (especially since any PHP code contained within won't work unless the webserver config is specifically modified to allow it).
One Final Note: these days it is pleasing to have web pages that do not end with an extension at all. Of course, you cannot leave off the extension of a .php or .html page... but you can hide the extension (including the period), making the page look like it was served by Flask or React or etc. You do this via a .htaccess file (yes, exactly like that, dot and all) that sits in the root folder of your website (same folder as the index.php or index.html). See:
https://code-boxx.com/hide-php-extension-url-htaccess/
https://tecadmin.net/remove-file-extension-from-url-using-htaccess/
Here is an interesting tool to help build .htaccess files
Use .html as a default.
If your page is running phpscripts then use .php
So, if you are communicating with server, use .php
.html and .php are file extensions but the more important question is how they are run.
A .php file can run server side script and take in mysql queries and open a connection etc...all of which are server-side functions.
Html is static and only displays static content but that has now changed with HTML 5.I suggest you do a simple search to learn more about php and html and their fundamental differences.
Files are handled depending on config and context. Shebangs, default programs, Apache Handler's, HTTP Headers, etc. describe handling files in various scenarios.
Executing Files In Terminal
The .php extension indicates that it is a PHP script, but the extension isn't necessary.
example-file.php
<?php
echo 'Hello World';
The script can be executed with PHP, which is clear because of the extension:
> php example-file.php
example2-file
#!/usr/bin/env php
<?php
echo 'Hello World';
With a shebang on the first line the OS can try to use the correct interpreter for the user so that the command is simplified to:
> ./example2-file
Some of the implementation details are hidden from the user by removing the file extension.
Packages often retain the extension on the source, but drop the extension during installation.
Default Programs
An extension can indicate to an OS which program to use to open a file.
Files ending in .php on my computer open in an IDE for editing whereas .html files open in a browser.
Servers and Headers
Web servers can send a file with any extension and content-type since many files don't actually exist, but are dynamically generated.
PHP web servers will serve .php files with the text/html content-type because the PHP is interpreted into text. Servers configured to return the raw PHP file as another content-type, i.e. servers not configured for PHP, will cause the web browser to download the source file rather than view the rendered file as HTML.
Since the resulting file after execution is HTML and web servers can dictate the extension, some developers decide to use .html in the URL and have them correlate to .php files to execute and return. Or the URL can not use an extension at all.
Using distinct extensions has the same purpose in PHP as it does in any language -- it makes it easier to determine the type of file you're using.
You may want to ease your web server's burden by having .html files not ran through the PHP processor, or you may want to have your PHP files not labeled .php to help hide what technology you're using server-side.

.php with HTML versus .html with PHP

When I am starting to build a site that is going to require both HTML and PHP, should I be making a .html file with PHP in it (as in the file would be, say, index.html but within it there would be various tags)? Or, should I be making the files .php files and simply include HTML within it (as in the file would be, again say, index.php and it would start as PHP and I would simply intertwine HTML)?
TL;DR: Should I be weaving HTML into .php files or weaving PHP into .html files?
It should be a PHP file with HTML "weaved" into it. By default if your server sees an HTML file it does not think it needs to process scripts on the page and will render it. If it sees a PHP extension, it knows it needs to run through the PHP Processor.
You can modify your htaccess to allow HTML to be rendered through the processor, but there really is no need for you to be modding that, especially if you are a beginner.
You use PHP files with HTML in it
You should "weave" html into php files That way you know for sure your code will work on any server, and not just on servers that renders html files as php.
You need to specify in your .htaccess file to be able to parse PHP inside of a .html file. The easier way to go is just to make everything .php.
Inevitably, when you get more comfortable with PHP, you'll learn that you'll always have a little PHP in the file (like a require or something), so best to plan for that.
If you are new to PHP, I would recommend creating files with the .php extension, as the .php file can be executed by default. Depending on your server configuration, you may have to add some .htaccess directives to allow php code to run in an .html file.
If you like .html extensions, you can use .phtml files for templating your system, but only for the files that containing html code. And I prefer to use .php files that containing only php code like classes etc (this is what Zend or similar libs do).

Can you add a .php file to your .html file?

I have a website made and i have saved the file as .html, I now want to add a .php file. Is this possible as i have tried so hard to do this. Id appreciate any help given :)
You can do several things.
Rename your .html files to .php.
Tell your web server to pass .html files to the PHP interpreter. In Apache you'd do this by putting AddType application/x-httpd-php .html .htm in your .htaccess file or your Apache configuration.
If you have installed PHP, you can rename your file to *.php and insert PHP codes!
You want to put PHP code inside a .html file? You'd have to configure the webserver to treat the .html file as a PHP script. Technically, there's no such thing as a PHP script. There's only files which have at least one <?php ?> code block within them. As such, ANY file can contain PHP code. The problem is getting that file into the PHP environment.
Unless your webserver is told to hand the .html file over to the PHP interpreter, you cannot execute the PHP code contained within it - the raw code will get sent out as part of the html page.
Just rename your .html file to .php, if your webhost supports it.
If you run your own server you can even configure PHP to also run .html files, but it's not recommended because of the performance penalty on all html files.
Yes, you can include php file into html file. i m sure that this link will help you.
http://php.about.com/od/advancedphp/p/html_php.htm

Can't run php code from inside html file

I am having a problem running my php code from inside an html file. I have tried the .htaccess method and the handler method. I'm using a webserver with cpanel(version 11), apache(2.2.15) and php(5.2.13).I have also tried the files on xampp and they work perfect, but when I upload them to the webserver the php code from inside the html will not run.
Any suggestions, please?
You need to inform Apache that files of type 'html' need to be parsed by PHP. You can do this by adding this to the Apache configuration file (httpd.conf):
AddType application/x-httpd-php .html
However if you have other HTML files I would advise just rename the file to PHP (or set up some internal mapping so you don't specify an extension all together). If you have static HTML files that you serve, it's redundant to have them being executed by the PHP interpreter.
Why don't make it a PHP file and then rewrite PHP extension with HTML in .htaccess?

Categories