如何在php的同一页面中将变量从一个函数传递到另一个函数?

如何在php的同一页面中将变量从一个函数传递到另一个函数?

问题描述:

Ok now i have another problem i want to send one or more variable from one function to another like this:

class test{

    var $text2;

    function text1($text1){ // This will catch "This is text" from $text variable.

        $text1 = $this -> text2; // Giving $text1 value to $text2 

        return $this -> text2; // Now here is the trouble i think, i want to send the variable to the next function that is text2(). How do i do that?

    }
    function text2($text2){ // Here is the place that says undefined variable i want the variable $text2 from function text1() to be called here.

        echo $text2; // Now the variable $text2 should echo "This is text".

    }
}

$test = new test();

$text1 = "This is text"; // Assigning value to the variable.

$test -> text1($text1); // Passing the variable as parameter in the function text1().

echo $test -> text2($text2); // Trying to display the value of $text2 that is "This is text".

@authors of most other answers: why do you just take bad code and re-use it instead of pointing out the obvious bad practice?


You have troubles with your code mainly because it's extremely hard to read! Your functions and variables have the same names which is bad and they are bad variable and function names in general!

  • Function names should have an 'action word' in them, a verb that shows what they do
  • Variable names should not contain numbers
  • Variable names should describe a container
  • Data types make for bad variable names ($string, $text, $int)

Here is an example of how your class could look like, I named in 'TextPuzzler' because of @Matteo Riva 's comment.

class TextPuzzler
{
    protected $myText;

    public function setMyText($text)
    {
        $this->myText = $text;
    }

    public function getMyText()
    {
        return $this->myText;
    }

    public function printMyText()
    {
        echo $this->myText;
    }
}

$puzzler = new TextPuzzler();

$text = "This is a random text";

//this is how you set your text
$puzzler->setMyText($text);

//this is how you echo it via a special echo function, not really needed ...
$puzzler->printMyText();

//... because you can also use the getter and echo it like this
echo $puzzler->getMyText();

TRY THIS

class test{

var $text2;

function text1($text1){ // This will catch "This is text" from $text variable.

$text2 = $text1;  // Giving $text1 value to $text2 

$this->text2($text2);

}

function text2($text2){

echo $text2; // Now the variable $text2 should echo "This is text".

}

}

$test = new test();

$text1 = "This is text"; // Assigning value to the variable.

$test -> text1($text1); // Passing the variable as parameter in the function text1().

$test -> text2($text2); // Trying to display the value of $text2 that is "This is text".

I think that you simply have an assignment error.

Try the following in the text1 function


function text1($text1){ // This will catch "This is text" from $text variable.

        $this->text2 = $text1; //CHANGE HERE

        return $this->text2; 

}

function text2(){ //CHANGE HERE
        echo $this->text2;
}

Assignment happens from right to left.

left = right;

The left side will get the value of the right side.

When you are creating text1 function i think the code should be like this

var $text2;

function text1($text1){ // This will catch "This is text" from $text variable.

text2 = $text1; // Giving $text1 value to $text2 

return text2; // Now here is the trouble i think, i want to send the variable to the next function that is text2(). How do i do that?

}

function text2($text2){ // Here is the place that says undefined variable i want the variable $text2 from function text1() to be called here.

    echo $text2; // Now the variable $text2 should echo "This is text".

}

and one more thing $text2 should be global var

it's not functions but class methods. You don't have to pass anything inside of class, yoy just have to set up class variables

The issue as Markus states is that there is a clash between the variable name and the function name. Also, the assignment was backwards in function text1, php, not smalltalk.