0

I have an issue. I have got a template file (template_body.php) being included in an index.php (include_once("include/template_body.php");) in template_body.php i have got another file included called header.php (include("header.php"))

index.php -> template_body.php --> header.php

Now, in index.php i have got a boolean login check. But i can only access the boolean value in template_body.php and not in header.php .

Any way to achieve that?

Sunil Pachlangia
  • 2,033
  • 2
  • 15
  • 25
jQuery
  • 88
  • 8

2 Answers2

0

You should take a look on this post answer about variable scope in php:

Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?

The below example is working

index.php

<?php
  $test = "test";
  include_once("file1.php");
?>

template_body.php

<?php
  include("header.php");
?>

header.php

<?php
  echo $test;
?>

output

test

So again, from what I can read it's a scope issue.

Regards,

Eric

Eric
  • 557
  • 3
  • 18
  • Hey I know how to deal with variables and functions. But unfortunately the post is not dealing with this issue. The var is reachable 1 level deep, but not 2 levels. And havent found out why... – jQuery Aug 22 '16 at 12:26
0

I found a solution to my question:

If I use

include_once("include/template_body.php");
include_once("include/header.php");

it does not work... if I use

include_once __DIR__ ."include/template_body.php";
include_once __DIR__ ."include/header.php";

it does make the variables accessible.

But I have no explanation for that behaviour, so it would be great if somebody could explain this to me :)

(Both versions make the content appear)

jQuery
  • 88
  • 8
  • `include_once __DIR__ ."/include/template_body.php";` note: leading '/' in the literal path as `__DIR__` does not have a trailing slash. Please always use absolute paths. You can use relative directories in absolute paths. – Ryan Vincent Aug 22 '16 at 15:15