繁体   English   中英

PHP可以在定义变量之前使用变量吗?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM