Skip to main content
Inspiring
May 9, 2007
Question

Variable interplay between include files and parent files?

  • May 9, 2007
  • 10 replies
  • 878 views
How can I define a variable in a parent file which is then used in some
logic in an include file? For example, if I have -

<?php

$foo = 1;

?>
....
<?php require_once('foo.php'); ?>

and in foo.php, I have -

<?php if ($foo == 1) { echo "FOO"; } ?>

it doesn't seem to work. Am I testing improperly, or what?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================



This topic has been closed for replies.

10 replies

Inspiring
May 10, 2007
On Thu, 10 May 2007 11:38:48 -0400, "Murray *ACE*"
<forums@HAHAgreat-web-sights.com> wrote:

>I'll verify that I correctly reported this and post back....

The first thing I'd look for is whether the variable is defined within a
function, or the reference to it is inside a function. For example:

$var="Hello";
doit();

function doit(){
echo $var;
}

In this case, the variable is defined outside the function, but you're
trying to access it inside a function. The variable is out of scope
there. To fix this type of problem, you can do one of two things. First,
you could pass the variable as a parameter:

$var="Hello";
doit($var);

function doit($x){
echo $x;
}

The other would be to declare the variable as global in the function:

function doit(){
global $var;
echo $var;
}

The first method is preferable because it has the least chance of
disturbing other code that might use the variable.

Gary
Inspiring
May 10, 2007
Mine, too. But when you don't know what you are doing, sometimes the 'proof
of concept' tests you design are a bit off-kilter, doncha know? I can only
guess that i was suffering from that.

I'll verify that I correctly reported this and post back....

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Gary White" <reply@newsgroup.please> wrote in message
news:2p6643pe4k3ncefjuhf2l2v3huvanuqfgm@4ax.com...
> On Wed, 9 May 2007 20:26:02 -0400, "Murray *ACE*"
> <forums@HAHAgreat-web-sights.com> wrote:
>
>>Yeah - of course, I changed everything about it, too!
>
>
> I'm still bewildered by what caused the problem. If I have two files,
> first (includevar.php) like this:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> " http://www.w3.org/TR/html4/strict.dtd">
> <html>
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
> <title>Variable in an Include Test</title>
> <meta http-equiv="imagetoolbar" content="no">
> </head>
> <body>
> <p><?php
> $x='This is $x defined in includevar.php';
> include 'foo.php';
>
> print "$y";
> ?></p>
> </body>
> </html>
>
> And foo.php like this:
>
> <?php
> $y='This is $y defined in foo.php';
> print "foo.php says $x<br>\n";
> ?>
>
> The output I get is:
>
> foo.php says This is $x defined in includevar.php
> This is $y defined in foo.php
>
> Clearly, the variable defined in the parent file is recognized in
> foo.php and the variable defined in foo.php is recognized in the parent
> file. Hence my bewilderment.
>
> Gary


Inspiring
May 10, 2007
On Wed, 9 May 2007 20:26:02 -0400, "Murray *ACE*"
<forums@HAHAgreat-web-sights.com> wrote:

>Yeah - of course, I changed everything about it, too!


I'm still bewildered by what caused the problem. If I have two files,
first (includevar.php) like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
" http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Variable in an Include Test</title>
<meta http-equiv="imagetoolbar" content="no">
</head>
<body>
<p><?php
$x='This is $x defined in includevar.php';
include 'foo.php';

print "$y";
?></p>
</body>
</html>

And foo.php like this:

<?php
$y='This is $y defined in foo.php';
print "foo.php says $x<br>\n";
?>

The output I get is:

foo.php says This is $x defined in includevar.php
This is $y defined in foo.php

Clearly, the variable defined in the parent file is recognized in
foo.php and the variable defined in foo.php is recognized in the parent
file. Hence my bewilderment.

Gary
Inspiring
May 10, 2007
Yeah - of course, I changed everything about it, too!

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Gary White" <reply@newsgroup.please> wrote in message
news:r7l4435l2n0bnbdkmcpf1u2s5qk4dq7lju@4ax.com...
> On Wed, 9 May 2007 14:59:44 -0400, "Murray *ACE*"
> <forums@HAHAgreat-web-sights.com> wrote:
>
>>That's what I thought, too. But I solved the problem by moving the PHP to
>>the include file!
>
>
> Curious. That sounds like one of those solutions that shouldn't work,
> but the problem goes away when you do it.
>
> Gary


Inspiring
May 9, 2007
On Wed, 9 May 2007 14:59:44 -0400, "Murray *ACE*"
<forums@HAHAgreat-web-sights.com> wrote:

>That's what I thought, too. But I solved the problem by moving the PHP to
>the include file!


Curious. That sounds like one of those solutions that shouldn't work,
but the problem goes away when you do it.

Gary
Inspiring
May 9, 2007
That's what I thought, too. But I solved the problem by moving the PHP to
the include file!

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Gary White" <reply@newsgroup.please> wrote in message
news:qvu343hed44c0i68bca39jd4i6na8oolt6@4ax.com...
> On Wed, 9 May 2007 15:45:35 +0000 (UTC), "geschenk"
> <webforumsuser@macromedia.com> wrote:
>
>>For me it always works with both "require_once" and "include" when
>>introducing
>>the to-be-passed variable immediately before the "include":
>
> Really doesn't matter whether it is immediately before or 10,000 lines
> before. If it's in scope, it should be just fine.
>
> Gary


Günter_Schenk
Inspiring
May 9, 2007
>>
That's what I thought, too. But I solved the problem by moving the PHP to the include file!
>>

huh ? ;-) makes me wonder what you did "move" -- the $foo variable ?
Inspiring
May 9, 2007
On Wed, 9 May 2007 15:45:35 +0000 (UTC), "geschenk"
<webforumsuser@macromedia.com> wrote:

>For me it always works with both "require_once" and "include" when introducing
>the to-be-passed variable immediately before the "include":

Really doesn't matter whether it is immediately before or 10,000 lines
before. If it's in scope, it should be just fine.

Gary
Inspiring
May 9, 2007
Hmmm - I thought it was problematic having ANYTHING within the include
statement's block.

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"geschenk" <webforumsuser@macromedia.com> wrote in message
news:f1sqav$8vk$1@forums.macromedia.com...
> For me it always works with both "require_once" and "include" when
> introducing
> the to-be-passed variable immediately before the "include":
>
> <?php
> $foo = 1;
> include (?foo.php?); // or "require_once"
> ?>
>


Günter_Schenk
Inspiring
May 9, 2007
>>
Hmmm - I thought it was problematic having ANYTHING within the include statement's block.
>>

no why, the $foo = 1; variable is separated from the include statement and just preceeds it
Günter_Schenk
Inspiring
May 9, 2007
For me it always works with both "require_once" and "include" when introducing the to-be-passed variable immediately before the "include":

<?php
$foo = 1;
include (“foo.php”); // or "require_once"
?>
Inspiring
May 9, 2007
On Wed, 9 May 2007 08:21:24 -0400, "Murray *ACE*"
<forums@HAHAgreat-web-sights.com> wrote:

>How can I define a variable in a parent file which is then used in some
>logic in an include file? For example, if I have -
<snip>
>it doesn't seem to work. Am I testing improperly, or what?

It would seem so. It should work. The only thing that comes to mind is
possibly a scope problem if the variable is defined inside a function
and you're echoing output outside the function, or you are echoing the
output inside a function for a variable that was defined outside the
function.

Gary