![](/img/trans.png)
[英]How to pass php variable from one div to another div in the same page than pass to another page?
[英]how to pass variable from one function to another in the same page in php?
好的,現在我有另一個問題,我想從一個函數向另一個函數發送一個或多個變量,如下所示:
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".
@其他大多數答案的作者:為什么您只接受不良代碼並重新使用它,而不指出明顯的不良做法?
您的代碼有麻煩,主要是因為它很難閱讀! 您的函數和變量具有相同的名稱,這很不好 ,它們通常是不好的變量和函數名稱!
這是一個類的示例,由於@Matteo Riva的評論,我在“ TextPuzzler”中命名。
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();
它不是函數,而是類方法。 您不必在類內部傳遞任何內容,只需要設置類變量
Markus指出的問題是,變量名和函數名之間存在沖突。 同樣,在函數text1,php中,而不是在smalltalk中,分配是向后的。
嘗試這個
課堂測試{
var $ text2;
function text1($ text1){//這將從$ text變量中捕獲“ This is text”。
$text2 = $text1; // Giving $text1 value to $text2 $this->text2($text2);
}
函數text2($ text2){
echo $text2; // Now the variable $text2 should echo "This is text".
}
}
$ test =新的test();
$ text1 =“這是文本”; //為變量分配值。
$ test-> text1($ text1); //在函數text1()中將變量作為參數傳遞。
$ test-> text2($ text2); //試圖顯示$ text2的值為“ This is text”。
我認為您只是有一個分配錯誤。
在text1函數中嘗試以下操作
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;
}
分配從右到左進行。
左=右;
左側將獲得右側的值。
當您創建text1函數時,我認為代碼應該像這樣
var $text2;
function text1($ text1){//這將從$ text變量中捕獲“ This is text”。
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".
}
還有一件事$ text2應該是global var
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.