437

This is probably a very trivial question, but I haven't been able to find the answer neither through web search engines, nor on php.net. Please just direct me to where I can read about this, if you haven't got time to explain.

  1. What does the 'var' keyword mean in PHP?
  2. Are there any differences between PHP4 and PHP5?
John Carter
  • 53,924
  • 26
  • 111
  • 144
joelpet
  • 4,869
  • 3
  • 21
  • 17
  • 4
    I guess when you asked this question on July 30th, 2009, this had not been published yet at http://php.net/manual/en/language.oop5.visibility.php?: "Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning." Or maybe you had not found it. But the answer is clearly stated at php.net. – Jaime Montoya Jun 16 '17 at 20:21
  • That's really amazing to see what journeys PHP has passed these years! – Mehrdad Shokri Mar 23 '20 at 22:29

8 Answers8

394

It's for declaring class member variables in PHP4, and is no longer needed. It will work in PHP5, but will raise an E_STRICT warning in PHP from version 5.0.0 up to version 5.1.2, as of when it was deprecated. Since PHP 5.3, var has been un-deprecated and is a synonym for 'public'.

Example usage:

class foo {
    var $x = 'y'; // or you can use public like...
    public $x = 'y'; //this is also a class member variables.
    function bar() {
    }
}
Abraham Philip
  • 648
  • 9
  • 18
karim79
  • 339,989
  • 67
  • 413
  • 406
  • 10
    _"Note: The PHP 4 method of declaring a variable with the `var` keyword is still supported for compatibility reasons (as a synonym for the `public` keyword). In PHP 5 before 5.1.3, its usage would generate an `E_STRICT` warning."_ http://php.net/manual/en/language.oop5.visibility.php EDIT: I just saw that is has already been quoted [in another answer](http://stackoverflow.com/a/3844703/517705). But you should edit yours accordingly. – Sk8erPeter Jan 12 '13 at 10:38
  • This implies that it is *necessary* to use the `public` keyword with for a member variable. Is that true? Can't one simply put `$x;`? – limeandcoconut Feb 03 '15 at 20:55
  • 1
    so is it recommended to just stick with public/private and ignoring var entirely since we aren't worried about working with deprecated versions of PHP? – rolling_codes Jul 05 '15 at 17:46
  • 2
    As of 2019: https://www.php-fig.org/psr/psr-12/#43-properties-and-constants states that `var` must not be used, and visibility must be declared on all properties. – Charles Wood Nov 11 '19 at 14:23
79

The var keyword is used to declare variables in a class in PHP 4:

class Foo {
    var $bar;
}

With PHP 5 property and method visibility (public, protected and private) was introduced and thus var is deprecated.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 17
    In PHP 5.3 `var` is de-deprecated :) – Ionuț G. Stan Jul 30 '09 at 11:58
  • 8
    If it's de-deprecated, what's the best practice now in 5.3 - to use it or not to use it? Can you use it like `private var $foo = 'bar';`? – Tom Auger May 11 '11 at 15:01
  • 1
    Anyone know why it was de-deprecated? – Simon East May 16 '12 at 23:32
  • 19
    It was undeprecated [in 5.1.3](http://www.php.net/releases/5_1_3.php) - the only reasons given are ["for compatibility reasons"](http://www.php.net/manual/en/language.oop5.visibility.php). I think best practice is to use it only if you need to be compatible with PHP 4. Certainly don't do `private var` - that will break things real quick, as `var` is just a synonym for `public` in PHP 5. – cincodenada Sep 19 '12 at 23:31
  • 2
    `var` is not a true synonym for `public` as it cannot be used for declaring static members or constants. – Colin O'Dell Mar 31 '16 at 17:05
  • @Colin O'Dell's comment is the best =) – mzalazar Jun 04 '19 at 23:46
27

I quote from http://www.php.net/manual/en/language.oop5.visibility.php

Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.

Roshan
  • 664
  • 5
  • 23
taatparya
  • 31
  • 2
  • 3
  • I did tests. No returned E_STRICT ! http://sandbox.onlinephpfunctions.com/code/84652903a92588c23085e688803abfaf0c26a822 – Wallace Vizerra Jul 13 '15 at 12:07
  • 1
    @WallacedeSouza, that's because you have used PHP 7 in your example. It was invalid only in versions 5.0 - 5.1.3 –  May 24 '17 at 13:58
8

Answer: From php 5.3 and >, the var keyword is equivalent to public when declaring variables inside a class.

class myClass {
  var $x;
}

is the same as (for php 5.3 and >):

class myClass {
  public $x;
}

History: It was previously the norm for declaring variables in classes, though later became depreciated, but later (PHP 5.3) it became un-depreciated.

Webeng
  • 7,050
  • 4
  • 31
  • 59
7

So basically it is an old style and do not use it for newer version of PHP. Better to use Public keyword instead;if you are not in love with var keyword. So instead of using

class Test {
    var $name;
}

Use

class Test {
   public $name;
}
kta
  • 19,412
  • 7
  • 65
  • 47
  • why you say that? is there news of `var` being deprecated that I havent heard? the two should be synonymous – NappingRabbit Jan 18 '18 at 17:43
  • 1
    From the PHP manual: "The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword)." https://www.php.net/manual/en/language.oop5.visibility.php – Tomark Mar 30 '20 at 11:00
2

var is used like public .if a varable is declared like this in a class var $a; if means its scope is public for the class. in simplea words var ~public

var $a;
public
kumar
  • 47
  • 2
2

In PHP7.3 still working...

https://www.php.net/manual/en/language.oop5.visibility.php

If declared using var, the property will be defined as public.

xayer
  • 413
  • 4
  • 11
1

here and now in 2018 using var for variable declaration is synonymous with public as in

class Sample{
    var $usingVar;
    public $usingPublic;

    function .....

}
NappingRabbit
  • 1,888
  • 1
  • 13
  • 18