未定义的变量?但我定义了它

Rob*_*Rob 2 php variables

在有人声称这是一个重复的问题之前......(我注意到那些无法回答问题的人倾向于运行并寻找副本,然后报告它.)

这是你要找的副本: php声明我定义的变量是未定义的

但是,这并不完全重复.它给了我一个解决方案,但我并不是真的在寻找这个特定的解决方案.

这是我的问题:

Notice: Undefined variable: custom

现在这是我的代码:

                $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"; 
}
Run Code Online (Sandbox Code Playgroud)

显然,定义了$ custom.根据另一个问题,它是一个全球性的,应该标记为一个.但它如何成为一个全球性的?我怎样才能成为(非全球)?该脚本工作正常,它仍然显示它应该和正确的行为,但当我打开错误消息时,它只是输出该通知.我想它目前还没有必要修复它,但无论如何我想知道它为什么这样做.

Bil*_*eal 5

问题出在这里:

$custom .= "$header: $value"; 
Run Code Online (Sandbox Code Playgroud)

您将附加到$ custom,但在第一次循环运行之前,$ custom中的值是未定义的.

放在$custom = '';循环之前,错误应该消失.

  • 你赢了._这次._ :-) (2认同)