开发者

Is is possible to call a variable that has been assigned after the call (PHP)?

The title开发者_开发知识库 is in the question (EDIT: :P I mean the question is in the title), basically can I call variable $x before defining it further down the page?


Short answer, no.

Long answer, noooooooooooooooooooooooooooooooooooooooooo.

But seriously, you can refer to it, it just won't do what you want.


Depending on how strict your warnings on you can call an undeclared variable as much as you want. However until you assign it a value it won't have a value.


I am not quite sure to understand your point but if you want to write

echo $x;
$x = "2";

you will not get "2" as a result.

PHP will usually not issue a warning when you reference a variable that has not yet been assigned a value. PHP will create it on the fly and assign it the null value which will then be casted to whatever scope you have. For example

$a = $b + 5;
echo $a;

will print 5 because in this case $b will be interpreted as beeing 0.

I hope this will help Jerome


No, the execution goes down the file. You can use a function though, to call later on once the variable has been defined. For example:

<?php
function meow() {
    echo $kitty_noise;
}
?>

And then later on down the file...

<?php
$kitty_noice = 'meowwwwww!';
meow();
?>

Horrible example....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜