tags:

views:

62

answers:

4

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?

+2  A: 

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.

plod
+3  A: 

Short answer, no.

Long answer, noooooooooooooooooooooooooooooooooooooooooo.

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

David
I like the long answer
Carson Myers
+1  A: 

Hello James, 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

Jerome WAGNER
A: 

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....

whalesalad