Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include PHP file into HTML file [duplicate]

I'm working on a project that may have to change the same content on all html pages. So I figured I would create a php file and only have to change that so it changes on all pages over the web.

The files are saved as:

index.html
number.php

EXAMPLE:

------------------------(HTML FILE)----------------------------

<html>
   <head>
      <title>Home</title>
   </head>
   <body>
      <h1>Phone Number</h1>
      <?php include('number.php') ?>
   </body>
</html>

------------------------(PHP FILE)----------------------------

<?php
   echo 4895553268;
?>

What could I do without changing the file extension of all my html's into php. I've found that works but I would like to only change the code in the html page. I've tried include require tags and that didn't work so I tried script tags and can't seem to make it work right.

like image 346
AlexWillis21 Avatar asked Nov 20 '13 19:11

AlexWillis21


People also ask

HOW include multiple PHP file 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.

Can we include PHP file in HTML file?

We can insert any PHP file into the HTML code by using two keywords that are 'Include' and 'Require'. PHP include() function: This function is used to copy all the contents of a file called within the function, text wise into a file from which it is called. This happens before the server executes the code.

How can I have HTML and PHP in same file?

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).

Can we include one PHP file multiple times?

If the file is included twice, PHP will raise a fatal error because the functions were already declared. It is recommended to use include_once instead of checking if the file was already included and conditionally return inside the included file.


1 Answers

In order to get the PHP output into the HTML file you need to either

  • Change the extension of the HTML to file to PHP and include the PHP from there (simple)
  • Load your HTML file into your PHP as a kind of template (a lot of work)
  • Change your environment so it deals with HTML as if it was PHP (bad idea)
like image 62
Rob Baillie Avatar answered Oct 23 '22 11:10

Rob Baillie