简体   繁体   中英

Could PHP use variable before defining them?

In php manual http://php.net/manual/en/mysqli-stmt.bind-param.php

I see code like this:

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

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

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

The bind_param() method stores references to the values of the $code , $language , $official and $percent variables. The references are stored inside the $stmt object.

When you then give the variables values the $stmt object already knows where to look for the values.

We can create a class, that does this, ourselves:

class Play {
    protected $reference;
    public function bind( & $variable) {
        $this->reference = &$variable;
    }
    public function show() {
        echo "{$this->reference}<br>\n";
    }
}

The & character is the reference operator. When you use it you get a reference to the value of another variable.

With this class we can create an object and have some fun:

$play = new Play;
$play->bind($string);

$string = 'Hello!';
$play->show();

$string = 'World!';
$play->show();

No php can't use a variable before you write the declaration!

No language is obviously able to output a value, before it exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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