3

I know that it is possible to include php file with variables. It looks like this sample.

include 'http://www.example.com/file.php?foo=1&bar=2';

How to include php file on local file system (instead of url) with variables? Smth like that.

include 'file.php?foo=1&bar=2';

Is it possible?

UPDATE I wanna get variable from index page and include the file with exact variable from local system to content div. smth like that

<?php
$foo=$_GET['foo'];
$bar=$_GET['bar'];

?>
<body>
<div id="main">
<div id="content">
<?php
include 'file.php?foo='.$foo.'&bar='.$bar;
?>
</div>
</div>
</body>
1
  • for local files it's impossible. Set your variables before including the other files. As you can get GET variables by $_GET so you can set them also with $_GET Commented Jul 25, 2011 at 5:04

4 Answers 4

6

Variables are simply available in the file to be included. There is no file scope for variables.

so if you do this

$foo=1; $bar=2;
include 'file.php';

$foo and $bar will be available in the file.php.

The variables declared above this included statement will be available in the file.php file.

Sign up to request clarification or add additional context in comments.

1 Comment

this wouldn't work. he wants to pass the variables explicit. so you have to assume, that in file.php he's setting the variables via $_GET
1

Included files have access to whatever variables are defined in the scope they were called in. So, anything you define before the include will be set in the included file.

# foo.php

$foo = 'bar';
include 'bar.php';

# bar.php

echo $foo;

When foo.php is run, the output will be 'bar'.

Comments

1

What do you expect the variables to do? Maybe something like this:

<?
$_GET["foo"] = "1";
$_GET["bar"] = "2";
include "file.php";
?>

Comments

1

I think you have it reversed: should be $foo = $_GET['foo'] etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.