【问题标题】:PHP Include ErrorPHP 包含错误
【发布时间】:2010-12-28 06:58:15
【问题描述】:

将我的包含标头更改为添加功能后。它禁用了我的 php echo 用户名代码。

原来是这样的

htp://example.com/register.php?r=用户名

htp://example.com/register.php?r=

<?php

function headertop($page) {

 include('header.php');

}

headertop('members');

?>

<div id="myaccount">

<p class="reflink" align="center">To refer others, use this link: <span class="textblue">
             <?php require ("config.php"); echo $url; ?>/register.php?r=<?php echo $row["username"]; ?>
            </span></p>

【问题讨论】:

  • 标题是“PHP 包含错误”。您是在问为什么会收到有关包含文件或包含文件的错误?

标签: php include header


【解决方案1】:

我的猜测是它不起作用,因为$row 不在范围内。它在 header.php 中定义,现在是 headertop() 的本地文件。

你为什么还要以这种方式包含 header.php?

【讨论】:

  • 我正在尝试使用函数为每个页面声明一个 id,以便我可以使用 css 突出显示活动的导航链接
  • 当我将它放在函数内部时,nav css 可以完美运行,但是当我将 include 本身或函数外部包含时,nav css 不起作用,但用户名可以
【解决方案2】:

如果$row在header.php的函数中定义,尝试添加

global $row

另外,将所有包含在一个地方可能是更好的形式。而且你可能不应该把它放在一个函数中。

【讨论】:

    【解决方案3】:

    请考虑这段代码以了解发生了什么:

    <?php // bar.php
        $one = 1;
        $two = 2;
    

    <?php // foo.php
        $someVar = 'var';
    
        function foo()
        {
            $someOtherVar = 'otherVar';
            include 'bar.php';
    
            // get defined vars in current scope
            print_r(array_keys(get_defined_vars()));
        }
    
        foo();
    
        // get defined vars in current scope
        print_r(array_keys(get_defined_vars()));
    

    会输出类似的东西

    Array
    (
        [0] => someOtherVar
        [1] => one
        [2] => two
    )
    Array
    (
        [0] => GLOBALS
        [1] => _ENV
        [2] => HTTP_ENV_VARS
        [3] => argv
        [4] => argc
        [5] => _POST
        [6] => HTTP_POST_VARS
        [7] => _GET
        [8] => HTTP_GET_VARS
        [9] => _COOKIE
        [10] => HTTP_COOKIE_VARS
        [11] => _SERVER
        [12] => HTTP_SERVER_VARS
        [13] => _FILES
        [14] => HTTP_POST_FILES
        [15] => _REQUEST
        [16] => someVar
    )
    

    如您所见,在 foo() 的函数范围内,您可以访问 bar.php 中包含的变量,但是一旦执行该函数并且控制流回到全局范围,例如在调用 foo() 之后,变量不可用,因为它们没有被导出到函数范围之外。如果您在函数范围之外包含了 bar.php,例如

    include 'bar.php'; // instead of foo()
    print_r(array_keys(get_defined_vars()));
    

    你会得到

    Array
    ( 
        ... 
        [16] => someVar
        [17] => one
        [18] => two
    )
    

    将大量变量放入全局作用域会带来污染全局作用域和变量名冲突的风险。也就是说,一个包含文件可能有一个 $row 变量,而另一个可能也有一个变量,它们会相互覆盖。最好将一起属于类/对象的内容封装起来和/或用Registry pattern 替换全局范围。

    虽然我非常喜欢 OOP 方法,但您仍然可以使用适当的函数来完成它。只需从函数调用中返回所需的变量即可。

        function foo()
        {
            $someOtherVar = 'otherVar';
            include 'bar.php';
    
            // this is why I do not like this approach $one pops out all of a sudden.
            // I can deduct, it has to be in bar.php, but if you do that often, your
            // code will be very hard to read.
    
            return $one;
        }
    
        $user = foo(); // will contain $one then
    
        // get defined vars in current scope
        print_r(array_keys(get_defined_vars()));
    
        // gives
        Array
        (
            ...
            [16] => someVar
            [17] => user
        )
    

    当然,如果您希望在调用 foo() 期间声明所有变量,则必须将它们放入一个数组中,因为一次只能返回一个值。但这基本上是当您注意到它变得笨拙并且切换到课程时感觉很自然(嗯,至少对我而言)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      相关资源
      最近更新 更多