[英]Could PHP use variable before defining them?
在php手冊中http://php.net/manual/en/mysqli-stmt.bind-param.php
我看到這樣的代碼:
$stmt->bind_param('sssd', $code, $language, $official, $percent);
$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;
/* execute prepared statement */
$stmt->execute();
bind_param()
方法存儲對 $code
, $language
, $official
和$percent
變量的值的引用 。 引用存儲在$stmt
對象中。
然后,當您為變量提供值時, $stmt
對象已經知道在哪里尋找值。
我們可以自己創建一個類,這樣做:
class Play {
protected $reference;
public function bind( & $variable) {
$this->reference = &$variable;
}
public function show() {
echo "{$this->reference}<br>\n";
}
}
&
字符是參考運算符。 使用它時,您會引用另一個變量的值。
通過此類,我們可以創建一個對象並獲得一些樂趣:
$play = new Play;
$play->bind($string);
$string = 'Hello!';
$play->show();
$string = 'World!';
$play->show();
在編寫聲明之前,沒有php不能使用變量!
很明顯,沒有一種語言能夠在它存在之前就輸出一個值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.