What does PHP keyword 'var' do?

PhpKeyword

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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() {
    }
}

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

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.

Solution 5 - Php

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;
}

Solution 6 - Php

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

Solution 7 - Php

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.

Solution 8 - Php

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

class Sample{
    var $usingVar;
    public $usingPublic;

    function .....

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjoelpetView Question on Stackoverflow
Solution 1 - Phpkarim79View Answer on Stackoverflow
Solution 2 - PhpGumboView Answer on Stackoverflow
Solution 3 - PhptaatparyaView Answer on Stackoverflow
Solution 4 - PhpWebengView Answer on Stackoverflow
Solution 5 - PhpktaView Answer on Stackoverflow
Solution 6 - PhpkumarView Answer on Stackoverflow
Solution 7 - PhpxayerView Answer on Stackoverflow
Solution 8 - PhpNappingRabbitView Answer on Stackoverflow