在PHP xmlparser中,为什么可以';t我从我的字符\数据\处理程序()中存储一个全局变量?

在PHP xmlparser中,为什么可以';t我从我的字符\数据\处理程序()中存储一个全局变量?,php,global-variables,xml-parsing,Php,Global Variables,Xml Parsing,(或以下是代码要点: $host = ""; ... xml_set_character_data_handler($xmlparser, "tagContents"); ... function tagContents($parser, $data) { global $current; global $host; if ($current == "HOST") { $host = $data; // Trying to store

(或以下是代码要点:

$host = "";
...
xml_set_character_data_handler($xmlparser, "tagContents");
...
function tagContents($parser, $data) { 
    global $current; 
    global $host;
    if ($current == "HOST") { 
        $host = $data;         // Trying to store a global here
    }
    if ($current == "PATH") { 
        echo $host.$data;      // But its null when I get here.  WHY??
    }
}
我正试图像这样将路径附加到主机以创建一行URL,因为xmlparse会在每个回显后放置一个换行符。因此,如果有人能告诉我如何防止换行符,这也会解决我的问题

顺便说一下:

  • 我还尝试引用超级全局$GLOBALS['host'],得到了相同的结果
  • 我的主机服务器()上只有PHP4可用
谢谢,
bob

尝试使用superglobal$GLOBALS['host']无论如何都会更快。这是您的固定代码

$host = "";
...
xml_set_character_data_handler($xmlparser, "tagContents");
...
function tagContents($parser, $data) 
{ 
    global $current; 

    if ($current == "HOST") { 
        $GLOBALS['host'] = $data;         // Trying to store a global here
    }
    if ($current == "PATH") { 
        echo $GLOBALS['host'].$data;      
    }
}