How to link php file to html

PHP Include Files


The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.


PHP include and require Statements

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.

The include and require statements are identical, except upon failure:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue

So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

Syntax

include 'filename';

or

require 'filename';


PHP include Examples

Example 1

Assume we have a standard footer file called "footer.php", that looks like this:

echo "

Copyright © 1999-" . date("Y") . " W3Schools.com

";
?>

To include the footer file in a page, use the include statement:

Example


Welcome to my home page!


Some text.


Some more text.



Run example »



Example 2

Assume we have a standard menu file called "menu.php":

echo 'Home -
HTML Tutorial -
CSS Tutorial -
JavaScript Tutorial -
PHP Tutorial';
?>

All pages in the Web site should use this menu file. Here is how it can be done (we are using a

element so that the menu easily can be styled with CSS later):

Example


Welcome to my home page!


Some text.


Some more text.


Run example »


Example 3

Assume we have a file called "vars.php", with some variables defined:

$color='red';
$car='BMW';
?>

Then, if we include the "vars.php" file, the variables can be used in the calling file:

Example


Welcome to my home page!


echo "I have a $color $car.";
?>


Run example »


The require statement is also used to include a file into the PHP code.

However, there is one big difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute:

Example


Welcome to my home page!


echo "I have a $color $car.";
?>


Run example »

If we do the same example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:

Example


Welcome to my home page!


echo "I have a $color $car.";
?>


Run example »

Use require when the file is required by the application.

Use include when the file is not required and application should continue when file is not found.


PHP Exercises



If you’re a web developer and working in PHP, you’ve probably encountered the scenario where you need to link external PHP file to HTML rather than putting all of your code in just one file. In this guide, I will discuss how to link external PHP file to HTML and the reasons why you should do it.

  • Why Link External PHP files to HTML?
  • 1) Link External PHP file to HTML by changing File Extension
  • 2) Link an External PHP file to HTML by Creating a .htaccess File
  • FAQ( HOW TO LINK EXTERNAL PHP FILE TO HTML )
    • How do I make my PHP file work in HTML?
  • conclusion

Reasons to link external PHP files include:

  • Your code will be cleaner and more organized, and you will be less likely to make coding mistakes.
  • Easily maintain multiple scripts as you only need to make changes in one location rather than search for and change it on each page of the site.
  • Allow different pages within a website to share common functions/variables without having to repeat the code multiple times.
  • It makes debugging and testing functions easier as you can test your scripts from a separate file rather than having them embedded in HTML, which complicates things.

As we all know, HTML and PHP are two different languages. Thus we can’t link them together directly, so I’ll show you 2 methods to link an external PHP file to HTML.

This is probably the most straightforward way to link external PHP file to HTML. All you need to do is rename your HTML file so that it has a .php extension, such as “test.html” renamed as “test.php”

How to link php file to html
How to link php file to html

After the file has been renamed to a .php extension, we can either use the include or require functions to link an external PHP script to HTML.

The include() function is used when the file is not necessary, and the program should continue when the file is not found. The require() function, on the other hand, is used to indicate that a file is necessary by the program and that if it is not found it will result in a fatal error.

include(" name of external PHP file you want to connect ");
//like

or if you want to use require then

require(" name of external PHP file you want to connect ");
//like

so in this way you can link external PHP file to HTML pages.

This will help you to create links between your HTML files and PHP scripts without having them in the same file. This way, it makes maintaining multiple PHP scripts easier as you only need to make changes in one location rather than search for and change it on each page of the site.

But for some reason, if you don’t want to change the extension of your HTML file to PHP then what you should do? Well, we got you covered. I will show you an alternate method to link external PHP file to HTML.

You May like:

How to Make a Phone Number Clickable in WordPress ( 4 Ways )

How to Redirect 404 Page to Homepage in WordPress

How to Find WordPress Page Id or Post Id

This is the method I personally prefer and it’s very easy to do. All you need to do is create a .htaccess file within the directory where your project files are located, and then add this line of code in htaccess file:

AddType application/x-httpd-php .html 

so what does that mean? well, when you put this code It will force the Apache server to interpret HTML files as PHP scripts. So now without changing the file extension you can still write PHP scripts on an HTML file.

Again you will use include() or require() functions to link external PHP file to HTML

include(" name of external PHP file you want to connect ");
//like

or if you want to use require then

require(" name of external PHP file you want to connect ");
//like

How do I make my PHP file work in HTML?

As we all know, HTML and PHP are two different languages. Thus you can’t link them together directly. So to make a PHP file work in HTML, you have to either change the file extension of HTML and make it PHP, and then with the include() and require() functions, you can make the PHP file work in HTML.

Or you can create a .htaccess file and then add the below code:

AddType application/x-httpd-php .html 

This will force the Apache server to interpret HTML files as PHP scripts without changing its extension.

conclusion

So in this guide, you learned how to link external PHP file to HTML and that you can do it in two ways. The first method is by changing the file extension from HTML to PHP, while the second method is by creating a .htaccess file and adding the code to make the Apache server interpret HTML files as PHP files.

Thank you for reading! I hope this guide will help you create links between your HTML files and PHP scripts without having them in the same file. This way, it makes maintaining multiple PHP scripts easier.

 If there is anything else, you would like us to cover feel free to contact us via the comments section below. Also, If you have any questions or feedback, feel free to leave a comment. We will respond to you as soon as possible.

Thank you for reading!

Thus you can't link them together directly. So to make a PHP file work in HTML, you have to either change the file extension of HTML and make it PHP, and then with the include() and require() functions, you can make the PHP file work in HTML.

Can I use PHP file in HTML?

As you can see, you can use any HTML you want without doing anything special or extra in your PHP file, as long as it's outside and separate from the PHP tags. In other words, if you want to insert PHP code into an HTML file, just write the PHP anywhere you want (so long as they're inside the PHP tags).

How do I open a PHP file in HTML?

Open PHP/HTML/JS In Browser.
Click the button Open In Browser on StatusBar..
In the editor, right click on the file and click in context menu Open PHP/HTML/JS In Browser..
Use keybindings Shift + F6 to open more faster (can be changed in menu File -> Preferences -> Keyboard Shortcuts ).

How does PHP work with HTML?

PHP processor scans the page, line by line. It build a processed HTML page. If it finds HTML, it passes that on as part of the processed HTML page it is building. If it finds PHP scripts, the PHP processor may or may not output HTML.