Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between declaring a variable as "var $foo" or "$foo" in PHP?

Tags:

variables

php

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.

like image 580
rogeriopvl Avatar asked Feb 19 '09 16:02

rogeriopvl


People also ask

What is the correct way to declare a PHP variable?

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.

What does VAR do in PHP?

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.

Can I use var in PHP?

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.

What does & before a variable mean in PHP?

An ampersand just before the function name will return a reference to the variable instead of returning its value.


1 Answers

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

like image 174
Paige Ruten Avatar answered Sep 23 '22 14:09

Paige Ruten