We help IT Professionals succeed at work.

Variables and PHP includes problem

error77
error77 asked
on
Medium Priority
194 Views
Last Modified: 2012-05-12
Hi all,

I have an issue with php includes..

On page A I have:

$id = 1;
include ('pageB.php');

then in page B I've got:

echo $id;

But it's returning nothing ...I though includes where like having a page inside a page right?

Thanks
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Your thinking is correct, but your example does not include all the code to recreate the failure.  It is an issue of variable scope.  Look for functions or classes.
Freelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
In pageB try:

global $i;
echo $i;
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
And add

include('pageA');

in pageB
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Please, keep only my last comment: global is for other things...

Commented:
Conceptually what you say is correct, so there must be some minor discrepancy.  Show the full code.  

Also doublecheck that you are getting to the right page, by changing Page B to

echo "Debug 1 $id";
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I wrote only stupid things: maybe I have to sleep ;-)
If you create two simple scripts doing what you say, they works, so the problem must reside in other code you have in your actual files: post your full code to get help.

Cheers
Most Valuable Expert 2011
Author of the Year 2014
Commented:
In case you have not found it yet, required reading here...
http://php.net/manual/en/language.variables.scope.php

Explore More ContentExplore courses, solutions, and other research materials related to this topic.