【问题标题】:Is there a way to use parameters in PHP include paths? [duplicate]有没有办法在 PHP 包含路径中使用参数? [复制]
【发布时间】:2011-10-16 22:07:52
【问题描述】:

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

因为这对我不起作用:

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

【问题讨论】:

    标签: php


    【解决方案1】:

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

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

    【讨论】:

    • 可以,谢谢。
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      但是我认为您只是希望在 PHP 和包含的文件之间共享值。包含的文件将可以访问在它包含的范围内定义的所有变量。因此本示例中的包含将能够访问$a

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

      【讨论】:

        猜你喜欢
        • 2021-11-15
        • 2022-01-25
        • 1970-01-01
        • 2015-06-27
        • 2011-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多