The title explains it all. What's the difference?
var $foo;
$bar;
What's the main difference between this two variables?
I've noticed that "var $foo" is mostly used to declare class attributes, but why is it so?
Thanks in advance for all your answers.
Declaring PHP variablesAll variables in PHP start with a $ (dollar) sign followed by the name of the variable. A valid variable name starts with a letter (A-Z, a-z) or underscore (_), followed by any number of letters, numbers, or underscores.
The var keyword in PHP is used to declare a property or variable of class which is public by default. The var keyword is same as public when declaring variables or property of a class.
The var keyword creates a property in a class. Since PHP 5, it is equivalent to the public keyword. Note: The var keyword is only used for compatibility reasons. Since PHP 5, the keywords private , protected and public should be used instead.
An ampersand just before the function name will return a reference to the variable instead of returning its value.
In PHP 4 var
is used to define variables in classes. In PHP 5 it is deprecated, and you must use public
, private
, or protected
instead. Outside of classes it shouldn't have any effect (although you might get a warning in PHP 5).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With