Why is PHP file not working in HTML?

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 »


PHP include vs. require

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:

Hallo, i have installed php with xampp and all the files are is a sub folder in C:\xampp\htdocs however when I try to submit a simple with 2 fields I get the php file as a html not as php. For some reason php is not executed. How can I fix this? The code: Html:User Pass Php NAME: PASS: Thank you in advance!

If the PHP engine is not running, nothing consumes the <?php …?> tag, and the form is still displayed.

Yes, the PHP code is visible in the source but not in the browser window. I was not trying to hide the code, I was trying to render it correctly in Chrome. I tested it on my local server and on a live server by disabling PHP. On local server, Firefox and IE displayed the .php page fine. Chrome and Safari displayed it as code. On live server, all browsers displayed the page fine. They rendered the HTML/CSS as a normal page and ignored PHP code which is what I was trying to achieve. Also, I hid the form (if PHP is not running) using JS within the IF statement. You can try viewing the install.php page of WordPress by disabling PHP on your server/localhost and you will see what I mean. Unlike mine, that page still displays the broken form code if PHP is not running.

Why is PHP file not working in HTML?
Mittineague:

Might it be that the browser parses the HTML tags and text but treats the PHP as comments? i.e. It looks like the page but a closer look would prove the PHP stuff is missing.

Yes, I guess that’s the case.

My conclusion is that Chrome (or webkit) behaves differently for pages hosted on localhost and on a live server. Anyway, my issue is solved and thanks for your contributions.

How do I make my PHP file work in HTML?

You can add PHP tags to your HTML Page. You simply need to enclose the PHP code with the PHP starts tag <?

Why is my PHP file not working?

Make Sure PHP Is Installed and Running Properly. When PHP is not working on your server, the first thing to do is check if it's even present and available. You can not execute anything written in PHP if the programming language is not even installed on the server.

How to embed PHP in HTML?

We can use PHP in HTML code by simply adding a PHP tag without doing any extra work. Example 1: First open the file in any editor and then write HTML code according to requirement. If we have to add PHP code then we can add by simply adding <?

Will PHP code run in .HTML file?

PHP is a server-side scripting language. That means a PHP script is executed on the server, the output is built on the server, and the result is sent as HTML to the client browser for rendering. It is quite common to code PHP within HTML in a script.