1
    function some ($html, $name) {
        return $html;
    }

    $outer_html = "<div>".$name."</div>";

    echo some($outer_html, "Jon");

我明白我得到Notice: Undefined variable: name in

但是如何设置函数参数,它本身包含其他参数?

我想做这个是因为,我需要生成$outer_html函数,然后设置这个准备好的 html 代码来运行。

4

2 回答 2

2

为什么不使用sprintf()

function some ($html, $name) {
    return sprintf($html, $name); 
}

echo some("<div>%s</div>", "Jon");

输出

<div>Jon</div>
于 2013-03-02T12:06:43.563 回答
0

您需要使用 global 关键字。文档在这里

您将拥有:

function yourFunction() {
    global $yourVar;

    // ...
}

// $yourVar keep its value from yourFunction
于 2013-03-02T11:47:29.327 回答