PHP函数将数据添加到字符串
我的代码:
$myText = "";
function addText($textString){
$myText .= $textString;
}
addText("Hello there...");
echo $myText;
预期输出:
你好...
$myText
为空。
为什么会发生这种情况?
My code:
$myText = "";
function addText($textString){
$myText .= $textString;
}
addText("Hello there...");
echo $myText;
Expected output:
Hello There...
$myText
was empty.
Why does this happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您需要告诉函数使用全局变量 $myText
尽管在函数中使用全局变量被认为是有害的。
You need to tell the function to use the global variable $myText
Though using global variables within functions is considered harmful.
试试这个:
你必须这样做的原因是 bc
$myText
不在函数的范围内。因此,要将其放入范围内,您必须告诉函数要使用哪些全局变量,
这里是演示: http:// /codepad.org/DeH89No6
try this:
the reason you have to do the is is bc
$myText
is not in the scope on the function.so to put it in scope, you have to tell the function which global variables to use
here is the demo: http://codepad.org/DeH89No6
好吧,让我真正帮助您,而不是仅仅告诉您如何让当前的示例发挥作用。基本上,在自定义函数外部定义的变量在其内部不可见。在您的示例中,您的函数不知道 $myText 是什么,因为它是在函数范围之外声明的。要将变量传递给函数,您需要将它们添加为参数 - 在定义和调用函数时通过函数传递的变量,例如:
function addText($param1, $param2)
。正如其他人提到的,您可以使用 global 关键字允许函数查看某些变量,但仅将其用于数据库连接等抽象事物,而不是您要配置的实际数据,执行所有这些操作在你的函数内。
希望这能帮助您更多地了解 PHP。
Ok let me actually help you as opposed to just telling you how to get your current example to work. Basically, variables defined outside a custom function are not visible inside it. In your example your function has no idea what $myText is because it was declared out side of the functions scope. To pass variables to a function you will need to add them as parameters - the variables you pass with a function when you define and then when you call it, eg:
function addText($param1, $param2)
.As other have mentioned, you can allow functions to see certain variables by using the
global
keyword, but only really use this for abstract things like database connections, not the actually data you want to configure, do all that inside your function.Hopefully this will help you understand PHP a bit more.
这就是所谓的变量作用域。这是代码中您的变量被称为此处。
简而言之,即使函数内部的 $myText 与外部的名称相同,它们也是完全不同的。它们是不同的盒子,一个在您的房间内(一个在您的功能内),一个在外面(另一个)。即使它们的标签相同,您放入其中一个的内容也不会显示在另一个中。
有两种方法可以做你想做的事。
第一种简单但糟糕的方法:通过将 global 关键字添加到函数内部的关键字来制作一个大的 all 包含框,就像之前发布的那样。这就像说“在外面寻找带有此标签的盒子并使用这个”。
但请记住:全局变量是不好的。
尽管黑暗面可能很诱人,但还有其他方法......
带上你的盒子...
愿源头与你同在。
This is what you call variable scope. That's the part of the code where your variable is known as explained here.
To make a short story even shorter, even though your $myText inside the function shares the name with the one outside, they're completely different. They're different boxes, one in your room (the one inside your function) and one outside (the other one). Even if they're labeled the same, things you put into one of them wouldn't show up inside the other one as well.
There are two ways to do what you want to do.
First the easy but bad way: Make one big all including box by adding the global keyword to the one inside the function like posted before. This is like saying "Look outside for a box with this label and use this one."
But remember: Global variables are BAD.
Tempting as the dark side may be there is an other way...
Taking your box with you...
May the source be with you.
获取返回字符串的函数
$myText = addText('string');
get the function to return the string
$myText = addText('string');
它与变量范围有关。基本上,
addText
外部的$myText
与函数内部的$myText
不同。您可以使用它将函数内部的变量设置为函数外部的变量:It has to do with variable scope. Basically
$myText
outside ofaddText
is different from$myText
inside the function. You could use this to set the variable inside the function to the one outside of it:只是另一种看法......
Just another take on it...
函数的作用域(变量的生命周期)不会到达函数之外。对于这个简单的任务,我不明白你为什么要创建一个函数。但是,传递到函数中的变量会被复制到函数作用域中,即使您将变量命名为相同,它也不会相同。为了能够检索函数之外的内容,您必须使用 return 语句。
(使用 C++,如果将函数的参数作为引用传递,则您的方法是可能的)
但是,您的问题的可能解决方案可能是:
但同样,我不认为为什么要为这个简单的函数创建一个函数任务
the scope (life time of variables) of a function doesn't reach outside the function. To this simple task I don't see why you make a function. However, a variable passed into a function is copied into the functions scope, even if you name the variable the same it will not be the same. To be able to retrieve something outside the function you have to use a return statement.
(with C++, your approach would have been possible if the parameter to the function was passed as a reference)
But, a possible solution to your problem may be:
But again, I don't see the reason to make a function for this simple task
PHP 变量作用域与您可能习惯的其他语言的作用域略有不同。为了使变量
$myText
在函数addText
内可见,您必须使用global
关键字:或者,如果代码位于函数内一个类,您可以使用关键字
$this
将 $myText 设为类级变量:PHP variable scope is a little different than scope in other languages that you might be used to. In order for the variable
$myText
to be visible inside the functionaddText
, you must use theglobal
keyword:Alternatively, if the code is inside a class, you could make $myText a class-level variable using the keyword
$this
: