Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP可以在定义变量之前使用它们吗?_Php - Fatal编程技术网

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

PHP可以在定义变量之前使用它们吗?,php,Php,在php手册中 我看到这样的代码: $stmt->bind_param('sssd', $code, $language, $official, $percent); $code = 'DEU'; $language = 'Bavarian'; $official = "F"; $percent = 11.2; /* execute prepared statement */ $stmt->execute(); 没有php不能在编写声明之前使用变量 显然,在值存在之前,没有任何

在php手册中

我看到这样的代码:

$stmt->bind_param('sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

/* execute prepared statement */
$stmt->execute();

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

显然,在值存在之前,没有任何语言能够输出它。

方法存储对
$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();