0

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?

AndrewL64
  • 15,794
  • 8
  • 47
  • 79
  • Does this https://stackoverflow.com/a/17407995/13720553 answer your question? – schmauch Aug 02 '22 at 18:23
  • @schmauch That approach would mean I have to add an `include` for the `example.php` file in all pages that need `someFunction()` even though `header.php` is already included in all those same pages. I'm trying to find a way to access a function that is included in a page (here, `header.php`) that is included in the current page (here, `index.php`). – AndrewL64 Aug 02 '22 at 18:28
  • In other words, I'm trying to access a function in `example.php` via `header.php` in `index.php`. – AndrewL64 Aug 02 '22 at 18:43
  • No, it means, you should work with absolute paths. To do so, you could use `realpath(__DIR__.'../example.php')` in `header.php` – schmauch Aug 02 '22 at 20:50
  • Little mistake: I forgot a slash. `realpath(__DIR__.'/../example.php')` is correct. – schmauch Aug 02 '22 at 21:06
  • @schmauch I think you're misunderstanding my question. The above helps with path issues when including say, `example.php` in say, `header.php`. I'm talking about accessing a function included in `header.php` from say, `index.php` which included `header.php`. Including `example.php` in `header.php` with proper paths which your example as well as the dupe you linked are talking about is something I have already figured out and it's working fine. – AndrewL64 Aug 03 '22 at 18:35
  • Do you have set `display_errors = On` in your php.ini file? Otherwise you won't see any errors. And the error message says, that `../example.php` can't be included in `header.php`. So you won't see anything because php stops execution. Follow my advice and include correctly and you'll see, it works. – schmauch Aug 03 '22 at 19:59

1 Answers1

0

Let's put my comments into an answer:

When you call index.php your current work directory is the root directory. Including templates/header.php works fine. But the include '../example.php in header.php fails because it searches for example.php in the parent directory of your root directory. This leads to a fatal error and php stops execution.

Using include realpath(__DIR__.'/../example.php') in header.php will solve your problem.

schmauch
  • 630
  • 1
  • 7
  • 10
  • `example.php` is including fine in `header.php` and any variable/function defined in `example.php` can be echoed/accessed in `header.php`. But when I try to echo the same thing on any page that includes `header.php`, it doesn't work regardless of using `realpath()` or not. – AndrewL64 Aug 05 '22 at 20:28