【问题标题】:included file variables not accessable in calling file调用文件中无法访问包含的文件变量
【发布时间】:2016-02-16 11:09:23
【问题描述】:

注意:变量是在包含文件中设置的,问题不是未定义的变量,因此重复的问题与我的问题无关。 PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

编辑: 我刚刚注意到我的本地主机包含路径设置为 C:\xampp\php\PEAR,当我执行 echo get_include_path();但我没有得到包含文件未找到的错误,而是变量未定义。

我在C:/xampp/htdocs/test/index.phpconfig.php中有2个文件

index.php 的内容:

require 'config.php';
echo $name;

config.php 的内容:

$name = 'xyz';

我得到Notice: Undefined variable: name $name = 'xyz' 在 config.php 中的位置,在使用变量之前已包含。

我还必须在实时服务器上对其进行测试。请指导。

【问题讨论】:

  • 尝试使用error_reporting(E_ALL ^ E_NOTICE);获取错误
  • @PHPExpert 嗨,我按照你的建议尝试了。它会抑制通知,并且名称永远不会以这种方式回显。
  • 使用require 'config.php'; 而不是include 'config.php';。如果找不到文件,这将引发致命错误。
  • @maxhb 要求/包括相同的结果,显示通知。我刚刚注意到我的本地主机包含路径设置为C:\xampp\php\PEAR 当我做echo get_include_path();

标签: php


【解决方案1】:
    hi i tried your code but i didn't get any error..

    config.php

    <?php
    $name='hello';
    ?>

    index.php

    <?php
    require 'config.php';
    echo $name;
    ?>

include and require both are working

【讨论】:

    【解决方案2】:

    冬青牛。

    文件中没有错误。 php 开始标签不正确,它是短标签 &lt;? ?&gt; 而不是 &lt;?php ?&gt; 这导致了问题。

    我在本地主机上使用 php7。但当然 t 可以在旧版本的 PHP 和主机上工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 2011-05-17
      • 2011-02-20
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 2017-12-29
      相关资源
      最近更新 更多