每次调用start()函数时,此函数始终只输出一个选项卡。当它输出$ tabs的值时,从start()显示时始终为1,从stop()显示时为-1。为什么$ tabs不是全局的?
$parser = xml_parser_create();
$tabs = 0;
function start($parser, $element_name, $element_attrs) {
$tabs = $tabs + 1;
echo str_repeat("\t", $tabs).$element_name.": ";
}
function stop($parser, $element_name) {
$tabs = $tabs - 1;
echo "<br />";
}
function char($parser, $data) {
echo $data;
}
xml_set_element_handler($parser, "start", "stop");
xml_set_character_data_handler($parser, "char");
$fp = fopen("$SignedRequest", "r");
while ($data = fread($fp, 4096)) {
xml_parse($parser, $data, feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
答案 0 :(得分:1)
你忘记了
global $tabs;
答案 1 :(得分:1)
不,它不是,在PHP中必须声明全局变量,如下所示:
function start($parser, $element_name, $element_attrs) {
global $tab;
$tabs = $tabs + 1;
echo str_repeat("\t", $tabs).$element_name.": ";
}
function stop($parser, $element_name) {
global $tab;
$tabs = $tabs - 1;
echo "<br />";
}