我需要连接行以便以后输出(降价处理...)。 这就是我使用函数 我的视图代码: p>
输出 p>
\ n 我希望: p>
为什么呢? 我究竟做错了什么? p>
我使用的是PHP 7.2.6 p>
编辑: p>
有几个PHP相关的答案为< a href =“https://stackoverflow.com/questions/4127788/changing-a-global-variable-from-inside-a-function-php”>这个。 但他们没有帮助。 我想这个问题与Yii2有关,更具体到Yii2视图处理。 p>
div> l() code>和全局变量
$ content code>的原因。 p>
$ content =“”;
function l($ line =“”){
global $ content;
$ content。= $ line。“
”;
}
l(“你好”);
echo“+”;
echo $ content;
echo“ - ”;
code> pre>
+ -
code> pre>
+ Hello-
代码> PRE>
I'm need to concatenate lines for later output (markdown processing...). This is why I use a function l()
and a global variable $content
.
My view code:
$content = "";
function l($line="") {
global $content;
$content .= $line."
";
}
l("hello");
echo "+";
echo $content;
echo "-";
outputs
+-
I'd expect:
+Hello-
Why? What am I doing wrong?
I am using PHP 7.2.6
EDIT:
There are several PHP related answers as this one. But they don't help. I suppose the problem is related to Yii2 and more specific to Yii2 view handling.