I have a header.php & footer.php in a directory called templates and index.php & example.php in the root directory.
- index.php
- example.php
- templates
- header.php
- footer.php
Inside the example.php file, there is a function:
example.php
<?php
function someFunction() {
return "Hello World";
}
?>
I included example.php in my header.php file and included header.php in my index.php file as follows:
header.php
<?php include '../example.php'; ?>
<!DOCTYPE html>
<html>
<head>
<title>Example Title</title>
</head>
<body>
<h1><?php echo someFunction(); ?></h1>
footer.php
</body>
<html>
index.php
<?php include 'templates/header.php'; ?>
<div>
<h2><?php echo someFunction(); ?></h2>
</div>
<?php include 'templates/footer.php'; ?>
The function someFunction() works fine in header.php and the echo returns Hello World but the same function is not available in index.php and returns nothing. What is the right way to go about this?