Search code examples
phphtmlrequire

php/html: Require doesn't work in basic html / php code


So im trying to import a header.php into my index.php file file but it just doesnt work.

header.php:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <header>
      <p> this should be on top</p>
    </header>

index.php:

<?php require "header.php"; ?>

<main>
  <p> this is the index</p>
</main>

<?php require "footer.php"; ?>

footer.php:

<p> This should be on the bottom </p>
</body>

</html>

If i open index.php it will only show whats written in index.php. I got all of the files saved in a folder within a folder on my desktopt.

I open index.php by just dragging into my browser (firefox).


Solution

  • You need to run your PHP Scripts using a web server.

    PHP Scripts need to be interpreted by a Server before be opened by the browser.

    To run locally your project there are multiple applications like Xampp (for Windows) or Mampp (For Mac), Docker or event install the web server directly in your Machine, installing Nginx or Apache.