Forum Moderators: coopster

Message Too Old, No Replies

Passing a variable to an included

Can't see why this doesnt work.

         

mack

11:11 am on Jun 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Lets imaging index.php is a static php page.

It sets a value

$value="abc";

Then does an include

include"file.php";

within file .php we have the following.

<?php
echo"$value";
?>

Expected abc, Result no output?

Any ideas.

Mack.

vincevincevince

11:21 am on Jun 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only thing I can think of is that the print statement (or the original setting) is inside a function.

dreamcatcher

11:25 am on Jun 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Mack,

You might also want to enable error reporting to see if that picks up anything.

<?php
error_reporting(E_ALL);
echo $value;
?>

dc

mack

12:26 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



This is just wierd....
On index.php if I call the value using...

Echo"$value";

it works....

But on the file included it displays nothing. If I enable error messages, "Undefined variable:"

Just not sure why the value isn't being passed from main page to the include. In php the included file should fall within the scope of the parent file. I guess this could be blocked by the host if they have made changes to php.ini file? but it seams to break logic to block a frequently used process.

Mack.

vincevincevince

12:56 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Keep moving the line 'Echo"$value";' downward until it is the line immediately above the include()

Next - if you are including by URL, then you will get the effect you mention - use a direct link (i.e. /var/www/html/mysite/includeme.inc not [mysite.foo...]

mack

1:28 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Thanks! Using he full file path worked. Not quite sure why this was required, but at least it is not working.

Thanks again.

Mack.

eelixduppy

1:32 pm on Jun 13, 2007 (gmt 0)



>> Not quite sure why this was required

Because if you are using a URI, the file is parsed before it is included.