开发者

Undefined Variable? But I defined it

开发者 https://www.devze.com 2022-12-28 01:45 出处:网络
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.)

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: 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 = a开发者_开发技巧pache_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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号