views:

68

answers:

3
+1  Q: 

PHP Include Error

After changing my include header to a add a function. It disabled my php echo username code.

So it went from

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

to

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>
+2  A: 

My guess is it doesn't work because $row isn't in scope. It's defined in header.php, which is now local to headertop().

Why are you including header.php this way anyway?

cletus
Well I'm trying to use function to declare a id for each page on so I can use css to highlight the active nav link
AskaGamer
when I have it inside the function the nav css works perfect, but when I have the include by itself or outside the function the nav css doesn't work but the username does
AskaGamer
see http://docs.php.net/language.variables.scope
VolkerK
+1  A: 

If $row is defined in a function in header.php, try adding

global $row

Also, its probably better form to just have all of your includes in one place. And you probably shouldn't put it in a function.

The.Anti.9
globals are evil
Gordon
+1  A: 

Please, consider this code to understand what is going on:

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

and

<?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()));

will output something like

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
)

As you can see, inside the function scope of foo(), you have access to the vars included in bar.php, but once the function is executed and control flow gets back to the global scope, e.g. after the call to foo() the vars are unavailable, because they have not been exported outside the function scope. If you had included bar.php outside the function scope, e.g.

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

you'd get

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

Placing a lot of variables into global scope bears the danger of polluting the global scope and risking variable name clashing. That is, one include file might have a $row variable and another might have one too and they overwrite each other. It is better to capsule stuff that belongs together into Classes/Objects and/or substitute the global scope with a Registry pattern.

While I very much favor the OOP approach, you could still do it with your function in place though. Just return the required variable from the function call.

    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
    )

Of course, if you'd want to have all the variables declared during the call to foo(), you would have to place them into an array, since only one value can be returned at a time. But this is basically when you notice it gets clumsy and switching to classes feels natural (well, at least to me).

Gordon