简体   繁体   English

包含带变量的php页面

[英]Include php page with variable

I know that it is possible to include php file with variables. 我知道可以包含带变量的php文件。 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? 如何在变量上包含本地文件系统(而不是url)的php文件? Smth like that. 像这样的Smth。

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. 更新我想从索引页面获取变量,并包含从本地系统到内容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>

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 . file.php中将提供$foo$bar

The variables declared above this included statement will be available in the file.php file. 上面声明的变量包括语句将在file.php文件中提供。

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. 包含的文件可以访问在调用它们的范围内定义的任何变量。因此,您在include之前定义的任何内容都将在包含的文件中设置。

# foo.php

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

# bar.php

echo $foo;

When foo.php is run, the output will be 'bar'. 运行foo.php ,输出将为“bar”。

What do you expect the variables to do? 您期望变量做什么? Maybe something like this: 也许是这样的:

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

我认为你已经逆转了:应该是$ foo = $ _GET ['foo']等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM