Undefined Variable? But I have defined it

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

Here is the duplicate you are looking for: php claims my defined variable is undefined

However, this is not exactly a duplicate. This gives me a solution, but I'm not really looking for this specific solution.

Here's my problem:

Notice: Undefined variable: custom

Now here is my code:

                $headers = apache_request_headers(); // Request the visitor 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 another question, it is global and should be marked as one. But how is it global? And how can I do this (non-global)? The script works fine, it still displays what its supposed to and acts correctly, but when I turn on the error messages, it just displays that notification. I suppose it doesn't need to be fixed at this time, but I would still like to know why it does this.

+2


a source to share


2 answers


The problem is here:

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

      



You add to $ custom, but before the first run through the loop, the value in $ custom is undefined.

Place a $custom = '';

in front of the loop and the error should go away.

+5


a source


I don't see an initial definition for $custom

there - I can see that you are concatenating to it, but you never give it an initial value.



Place the line $custom = '';

before the foreach loop.

+1


a source







All Articles