tags:

views:

55

answers:

2

Well before anyone claims that theres a duplicate question... (I've noticed that people who can't answer the question tend to run and look for a duplicate, and then report it.)

Here is the duplicate you are looking for: http://stackoverflow.com/questions/2481382/php-claims-my-defined-variable-is-undefined

However, this isn't quite a duplicate. It gives me a solution, but I'm not really looking for this particular solution.

Here is my problem:

Notice: Undefined variable: custom

Now here is my code:

                $headers = apache_request_headers(); // Request the visitor's headers.
                $customheader = "Header: 7ddb6ffab28bb675215a7d6e31cfc759"; //This is what the header should be.
                        foreach ($headers as $header => $value) { 
                                $custom .= "$header: $value"; 
}

Clearly, $custom is defined. According to the other question, it's a global and should be marked as one. But how is it a global? And how can I make it a (non-global)? The script works fine, it still displays what its supposed to and acts correctly, but when I turn on error messages, it simply outputs that notice as well. I suppose its not currently necessary to fix it, but I'd like to anyway, as well as know why its doing this.

+4  A: 

The problem is here:

$custom .= "$header: $value"; 

You are appending to $custom, but until the first run through the loop, the value in $custom is undefined.

Put a $custom = ''; before the loop and the error should go away.

Billy ONeal
Oooh I see. Alright thank you. That makes sense.
Rob
You win. _This time._ :-)
James McNellis
A: 

I don't see an initial definition for $custom there - I see you concatenating to it, but you never give it an initial value.

Put in a line $custom = ''; before the foreach loop.

Amber