有没有办法在PHP包含路径中使用参数?

时间:2011-07-31 14:04:14

标签: php

  

可能重复:
  PHP - include a php file and also send query parameters

因为这对我不起作用:

<?php include ('list-options.php?format=list'); ?>

3 个答案:

答案 0 :(得分:3)

没有。您可以设置变量并使用它,就像它在文件中声明的那样。请在此处阅读:http://www.php.net/manual/en/function.include.php

<?php 
$format = "list";
include("list-options.php");
?>

答案 1 :(得分:1)

他们不直接支持它,但您可以使用以下方式模拟它:

$_GET['format'] = 'list';
include("list-options.php");

脚本会认为它是使用list-options.php?format=list调用的。

答案 2 :(得分:0)

虽然您无法向本地服务器中包含的文件添加参数,但如果您要包含远程文件,则可以。如果您在PHP.ini中启用了URL fopen,则可以执行以下操作:

include('http://www.example.com/script.php?param=1&other=2');

但是我认为你只是想在PHP和包含的文件之间共享值。包含的文件将可以访问其包含的范围中定义的所有变量。因此,此示例中的include将能够访问$a

$a = "hello include";
include('local.php');