Variables across includes

Permalink
Hello,

Given the following in a pagetype file:
$this->inc('elements/content_top.php');
      $this->inc('elements/content_main_local_projects.php'); 
      $this->inc('elements/footer.php');

Why is it that a variable set in 'content_top.php' is NOT available in 'content_main-local_projects.php' ?

Thanks!

 
fastcrash replied on at Permalink Reply
fastcrash
maybe you already knew,
you can use global param
$GLOBALS['areaHandle'] = 'I'm here';

with this, you var will be available from header to footer, from hell to heaven, from hero to zero? hehee..
Mainio replied on at Permalink Best Answer Reply
Mainio
Because includes work in their own context. Also, the parts of the script work in their own context, so therefore variables used in other scripts are not ALWAYS available to others.

This is PHP-scripting issue, not C5 issue. I would imagine there would be A LOT of possible bugs in software written in PHP if this was possible.

So, PHP works is basically like this:
// This is the main script, e.g. index.php
$someVar = "This is var";
echo "I can use '$someVar' value in this script!";


And then if you include some files FROM THAT SCRIPT, you can still use the variables in the other scripts:
// index.php
$someVar = "This is var";
include 'other_script.php';
// other_script.php
echo "I can also use '$someVar' value in this script because it's included from the main script!";
$someOtherVar = "This variable can only be used in THIS script and scripts included from THIS script, not e.g. from index.php!";


So this is the reason why you cannot use them.

===

However, if you want a workaround for this problem do this.
1. Change your includes to this:
$this->inc('elements/content_top.php');
$this->inc('elements/footer.php');


2. And then FROM elements/content_top.php, you should include the other script:
// elements/content_top.php
$someVar = 'Something';
$this->inc('elements/content_main_local_projects.php', array('someVar' => $someVar));


OR

Change your first try to this:
$someVar = 'Something';
$this->inc('elements/content_top.php', array('someVar' => $someVar));
      $this->inc('elements/content_main_local_projects.php', array('someVar' => $someVar)); 
      $this->inc('elements/footer.php');




Or, if you want to go with your first choise, just use global variables or constant definitions.


Antti / Mainio


EDIT: @fastcrash actually already pointed out the global variables possibility!
jordanlev replied on at Permalink Reply
jordanlev
I think @Mainio is on the right track, but there is a simpler solution (in my opinion) that I've used successfully in the past: just don't use $this->inc and instead use the normal "include" or "require" php function.

The $this->inc() function is a special Concrete5 thing that intentionally isolates the variables from one another, whereas php's built-in "include" or "require" functions do *not* isolate the variables.

Note that with Concrete5's $this->inc() function, there are two separate things you need to worry about here: one is sending variables TO an element, and another is getting variables back OUT of the element. As @Mainio suggests, you can pass variables TO an element by sending them as an array argument in the 2nd argument of the $this->inc() function. *BUT* concrete5 provides no way that I know of to get variables OUT of an element -- so you need to use php's "include" or "require" instead of $this->inc() (or use @fastcrash's suggestion of $GLOBALS).

Good luck!