igonejack
igonejack

Reputation: 2532

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

Upvotes: 1

Views: 53

Answers (2)

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13273

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

Upvotes: 2

Rizier123
Rizier123

Reputation: 59681

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

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

Upvotes: -1

Related Questions