My code:

$myText = "";

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

addText("Hello there...");

echo $myText;

Expected output:

Hello There...

$myText was empty.

Why does this happen?

有帮助吗?

解决方案

try this:

$myText = "";

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

addText("Hello there...");

echo $myText;

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

其他提示

You need to tell the function to use the global variable $myText

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

Though using global variables within functions is considered harmful.

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.

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...

$myText = "";

function addText($existingText, $textToAdd) {
    return $existingText . $textToAdd;
}

addText($myText, "Look, it's there!");

echo $myText;

May the source be with you.

get the function to return the string

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

$myText = addText('string');

It has to do with variable scope. Basically $myText outside of addText is different from $myText inside the function. You could use this to set the variable inside the function to the one outside of it:

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

Just another take on it...

$myText = "The quick brown fox";

echo addText(" jumped over the lazy dog.", $myText);

function addText($addString,$toVar) {
    $newString = $toVar.$addString;
    return $newString;
} 

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:

function addText($text){
    return $text;
}
$myText .= addText("Hello there");
echo $myText;

But again, I don't see the reason to make a function for this simple task

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 function addText, you must use the global keyword:

$myText = "";

function addText($textString){
   global $myText; //added this line
   $myText .= $textString;
}

addText("Hello there...");

echo $myText;

Alternatively, if the code is inside a class, you could make $myText a class-level variable using the keyword $this:

class SomeClass{
   function __init__(){
      $this->myText = "";
   }
   function addText($textString){
      $this->myText .= $textString;
   }

   function someFunction(){
      $this->addText("Hello there...");
   echo $this->myText;
   }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top