Dav*_*ave 2 php string-building
我的代码:
$myText = "";
function addText($textString){
$myText .= $textString;
}
addText("Hello there...");
echo $myText;
Run Code Online (Sandbox Code Playgroud)
预期产量:
你好...
$myText 空了.
为什么会这样?
您需要告诉函数使用全局变量$ myText
function addText($textString){
global $myText;
$myText .= $textString;
}
Run Code Online (Sandbox Code Playgroud)
虽然在函数中使用全局变量被认为是有害的.