I have created a file called index.php, inside the same folder index.php is there is a folder called "includes". Inside it are two php files head.php and header.php. The problem is that those two files are not being included into index.php. Now, I have searched all over stack overflow and I can't seem to find the answer.
<!doctype html>
<html>
<?php include 'includes/head.php'; ?>
<body>
<?php include 'includes/header.php'; ?>
<div id="container">
<aside>
<div class="widget">
<h2>Widget Header</h2>
<div class="inner">
Widget contents
</div>
</div>
</aside>
</div>
</body>
</html>
As per my comment to the OP:
@canthandlehtml question is now, if you have a webserver/PHP installed, if it's properly configured, and how you're accessing this. As
http://localhost/file.php
or asc://file.php in
your web browser? Those are 2 different animals altogether. – Fred -ii-
As per your comment:
I am indeed accessing it as c://file.php, I was not aware this might be a problem, forgive for the inadequacy, php is something completely new for me
You're accessing it as c://file.php
instead of http://localhost/file.php
which is the reason why your includes don't work.
A web browser will not parse/execute PHP directives in that manner. It requires it to be running through a host with a web server/PHP installed, running and properly configured.
Sidenote:
The code that are to be included should "echo" something in order to "show" something, if that is the intention; to echo HTML etc.
Seeing <?php include 'includes/header.php'; ?>
then I take it you have a form of navigation menu.
If it contains something like:
<?php
$var = "Hello world";
and it is not "echo'd", then it won't show up in your rendered HTML, it needs to be echo'd.
<?php
echo $var = "Hello world"; // this is a valid directive
// it both echo's and assigns
It's unsure if <?php include 'includes/head.php'; ?>
contains meta's, or CSS to be included etc. and if it does contain <head></head>
tags. If not, then you will need to add those if you are including that, or JS that is required to be in head tags, etc.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
try using
include("includes/head.php");
Assuming that the webpages are being accessed via a webserver rather than directly from the filesystem then using / setting the include_path
should help alleviate the issue ( that said, use echo
within the included files is the more usual approach! )
You can include a file using the filesystem path but you cannot run / execute php in a browser without the webserver.
<?php
/* Once the include path is set it is easy to include the file by name alone */
set_include_path( __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR );
?>
<!doctype html>
<html>
<head>
<?php
include('head.php');
?>
</head>
<body>
<?php
include('header.php');
?>
<div id='container'>
<aside>
<div class='widget'>
<h2>Widget Header</h2>
<div class='inner'>
Widget contents
</div>
</div>
</aside>
</div>
</body>
</html>