gpt4 book ai didi

PHP函数将数据添加到字符串

转载 作者:行者123 更新时间:2023-12-02 08:53:23 24 4
gpt4 key购买 nike

我的代码:

$myText = "";

function addText($textString){
$myText .= $textString;
}

addText("Hello there...");

echo $myText;

预期输出:

Hello There...

$myText 为空。

为什么会发生这种情况?

最佳答案

您需要告诉函数使用全局变量$myText

function addText($textString){
global $myText;
$myText .= $textString;
}

尽管在函数中使用全局变量被认为是有害的。

关于PHP函数将数据添加到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5492400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com