Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 执行外部文件后,如何获取外部变量?_Php_Variables - Fatal编程技术网

Php 执行外部文件后,如何获取外部变量?

Php 执行外部文件后,如何获取外部变量?,php,variables,Php,Variables,我试图从外部文件中获取一个变量,但我需要先执行该文件。我该怎么做 我需要获取变量:$title、$description、$link、$date和$author rss.php: $rss2_file = 'http://www.rune-pk.org/forums/external.php?forumids=2&type=rss2'; $is_item = false; $tag = ''; $title = ''; $description = ''; $link = ''; $

我试图从外部文件中获取一个变量,但我需要先执行该文件。我该怎么做

我需要获取变量:$title、$description、$link、$date和$author

rss.php:
$rss2_file = 'http://www.rune-pk.org/forums/external.php?forumids=2&type=rss2';


$is_item = false;
$tag = '';
$title = '';
$description = '';
$link = '';
$date = '';
$author = '';

function character_data($parser, $data) {
    global $is_item, $tag, $title, $description, $link, $date, $author;
    if ($is_item) {
        switch ($tag) {
            case "TITLE":
                $title .= $data;
                break;

            case "DESCRIPTION":
                $description .= $data;
                break;

            case "LINK":
                $link .= $data;
                break;

            case "PUBDATE":
                $date .= $data;
                break;

            case "AUTHOR":
                $author .= $data;
                break;
        }
    }
}

function begin_element($parser, $name) {
    global $is_item, $tag;
    if ($is_item) {
        $tag = $name;
    } else if ($name == "ITEM") {
        $is_item = true;
    }
}

function end_element($parser, $name) {
    global $is_item, $title, $description, $link, $date, $author, $rss2_output;
    if ($name == "ITEM") {
        $rss2_output .= "<dt><strong><a href='" . trim($link) . "'>" . htmlspecialchars(trim($title)) . "</a></strong> - " . htmlspecialchars(trim($date)) . " by <em>" . htmlspecialchars(trim($author)) . "</em></dt><dd>" . htmlspecialchars(trim($description)) . "</dd>";
        $title = "";
        $description = "";
        $link = "";
        $date = "";
        $author = "";
        $is_item = false;
    }
}

$parser = xml_parser_create();

xml_set_element_handler($parser, "begin_element", "end_element");
xml_set_character_data_handler($parser, "character_data");
$fp = fopen($rss2_file, "r");

while ($data = fread($fp, 4096)) {
    xml_parse($parser, $data, feof($fp));
}

fclose($fp);

xml_parser_free($parser);

echo "Latest Announcements:";
echo $rss2_output;
?>

您尝试的是用于访问类中存储的变量。但这里的情况并非如此,所以请这样做:

<?php
    include 'rss.php';
    echo $title;
?>

您尝试的是用于访问类中存储的变量。但这里的情况并非如此,所以请这样做:

<?php
    include 'rss.php';
    echo $title;
?>

我的问题是,变量已经初始化,但它们没有通过方法运行,因此没有用于它们的数据。不过,谢谢你,我知道它很小。在我回显变量之前,你能告诉我如何处理rss文件吗?如果你能帮忙,我会记下来的。如果不是的话,我无论如何都会做标记,但是是的您包含的文件将始终在回显$titleMy之前执行,问题是变量已初始化,但它们未通过方法运行,因此没有用于它们的数据。不过,谢谢你,我知道它很小。在我回显变量之前,你能告诉我如何处理rss文件吗?如果你能帮忙,我会记下来的。如果不是的话,我无论如何都会做标记,但是是的您包含的文件将始终在回显$title之前执行