What does PHP keyword 'var' do?

What does PHP keyword 'var' do?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What Does PHP Keyword 'var' Do?

In legacy PHP codebases, you will frequently encounter the var keyword preceding variable declarations. It often appears in functions written for older versions of the language, yet it serves no functional purpose in modern development. The short answer is that var is a pseudo-keyword used solely for visual clarity; it does not alter the behavior of the code or change how variables are handled by the engine.

When you declare a variable inside a function using var, you are essentially telling the parser to treat that scope as local. However, this declaration style was introduced in PHP 4 and is technically redundant even then. In PHP 5, the language specification removed var from the set of recognized keywords, categorizing it strictly as a pseudo-keyword. While the engine ignores it and executes the code correctly, its presence is a hallmark of pre-5 syntax that persists only due to historical inertia.

The primary distinction between PHP 4 and PHP 5 regarding this keyword lies in scope handling rather than the keyword itself. In PHP 4, if you attempted to assign a value to a variable without prefixing it with var inside a function, the engine would treat the assignment as modifying a global variable of that name. This could lead to unexpected side effects if the global scope contained a variable with the same identifier. To prevent this, developers explicitly used var.

In PHP 5, the language designers realized this pattern was confusing and unnecessary. The modern approach uses the standard assignment operator (=) to declare local variables within functions. If you define a variable without a preceding keyword inside a function, it is automatically scoped to that function. The engine handles this without needing any special instructions.

Consider the following comparison between legacy syntax and modern PHP 5+ style:

// Legacy PHP 4 style using 'var'
function calculateTax($income) {
    var $tax = $income * 0.2; // 'var' has no effect here
    return $tax;
}

// Modern PHP 5+ style
function calculateTax($income) {
    $tax = $income * 0.2;
    return $tax;
}

Both snippets produce identical results in a running PHP interpreter. The first version executes perfectly fine because the engine strips out the var token and processes $tax = ... as a standard local variable assignment. The second version is preferred today because it aligns with the current language specification and removes clutter from the codebase.

There is one specific context where the distinction between PHP 4 and PHP 5 regarding var matters, though it rarely applies in contemporary projects: property visibility in classes. In PHP 4, declaring a class property required var. For example, var $name = "John"; would define a private property. In PHP 5, this syntax was deprecated in favor of the standard $this->name = "John"; for public properties and explicit visibility modifiers like private, protected, or public.

Relying on var today is generally considered bad practice because it signals that code has not been updated to modern standards. It can confuse new developers who might assume the keyword performs some magic regarding data typing or memory management, which it does not. Furthermore, if you encounter this in a migration project from PHP 4 to PHP 5, removing these keywords is a safe and recommended step to clean up the code.

If you are working with frameworks like Laravel, you will almost never see var in official documentation or modern packages. The ecosystem has long since moved away from pseudo-keywords in favor of strict typing and clear scope definitions. While you might find references to this behavior when reading older articles on LaravelCompany discussing the history of PHP syntax, applying these legacy patterns to new projects adds no value.

The decision to drop var was driven by simplicity and consistency. Modern PHP relies on context to determine variable scope rather than relying on specific keywords inside function bodies. This shift reduces cognitive load for developers and makes codebases easier to read and maintain. When reviewing code, the presence of var is a clear indicator that the file likely originates from the PHP 4 era and should be refactored to use standard assignment operators.

Our network