Tural Ali
Tural Ali

Reputation: 23290

Include php page with variable

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>

Upvotes: 3

Views: 7343

Answers (4)

bozdoz
bozdoz

Reputation: 12890

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

Upvotes: 1

David Wolever
David Wolever

Reputation: 154664

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

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

Upvotes: 1

cantlin
cantlin

Reputation: 3226

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'.

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86476

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.

Upvotes: 6

Related Questions