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

时间:2014-11-10 03:34:00

标签: php

在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();

2 个答案:

答案 0 :(得分:1)

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();

答案 1 :(得分:-1)

在编写声明之前,没有php不能使用变量!

在语句存在之前,任何语言显然都无法输出。

相关问题