Glossary
Variable
A variable gives a program a named reference to a value. A well-chosen name explains what the data means without requiring the reader to dissect every line of code.
Short definition
Name, value, and scope belong together.
A variable stores a value that the program is currently working with: a price in cents, an order state, a list of items, or a customer object. The name itself is neither the value nor a business rule. It is a readable way to refer to the value in a calculation, pass it to a function, or build an API response.
In PHP, a variable’s type is usually determined by its value at runtime. That does not make types and naming unimportant: parameter and return declarations, data objects, PHPDoc, and static analysis help reveal whether an amount is accidentally text or a missing customer is being used as a valid customer object.
What it is used for
Name an intermediate result or the input to the next step.
Variables hold the local state of an algorithm. Their names should describe meaning in the domain, not the mechanics of the line just written.
- an item’s price, quantity, and calculated total order amount
- a verified user identity and the current tenant during authorization
- data received from an API before validation and the data object after it
- a temporary query, mapping, or calculation result
- configuration passed to a dependency when it is not global state
Practical example
An order amount in minor currency units
The calculation uses variables named according to their meaning. Instead of an ambiguous $price, the amount and the unit being used are stated. The example deliberately omits tax and discounts; it shows only a readable local calculation.
Such a variable does not validate user input. If the quantity comes from a request, the backend must validate it before the calculation and authorize the related operation where necessary.
PHP
$quantity = 2;
$unitPriceInCents = 1_990;
$totalInCents = $quantity * $unitPriceInCents;
// 3 980, tedy 39,80 Kč při zobrazení v této měně
How a variable works
From an input value to a clear result
A variable usually has limited scope: it is created in a function, helps perform one step, and is no longer available after the function ends.
- Creating a value The program reads a parameter, query result, configuration, or value from an earlier calculation.
- Naming The name expresses meaning and unit, such as totalInCents instead of generic data or value.
- Working within scope A function reads, combines, compares, or passes the value to another function within local scope.
- Result The variable is returned as a value, stored in an object, or used to create a response.
- End of lifetime After execution leaves the local scope, the variable is no longer available to other parts of the application.
Important concepts
A variable is not a constant, parameter, or object property.
Similar notation can carry different responsibilities. Distinguishing them correctly limits hidden state and confusing names.
Name and value
The name $orderId refers to a value, such as 123. The name should explain the value’s meaning, not its current contents.
Scope
A local variable in a function is not normally available outside it. A smaller scope limits unexpected side effects.
Assignment and references
PHP assigns values by default. A reference using & creates an alias for the same variable and can make changes harder to understand; it should be used only for a clear reason.
Parameter and property
A parameter is a variable passed to a function. A property belongs to a specific object and can represent its longer-lived state.
Type and contract
A PHP variable can hold different values at runtime. Type declarations around functions and properties help define what is permitted at a given point.
Benefits and limitations
A good name reduces cognitive load but does not replace design.
Benefits
- a readable intermediate step instead of a repeated complex expression
- easier transfer of a value between functions
- a named unit and meaning for data, such as amountInCents
- smaller local scope limits unintended state sharing
Common mistakes
- generic names such as data, value, or result without context
- one variable that changes both meaning and type within a function
- global variables used as hidden input or output
- relying on an undefined variable instead of explicit initialisation and checking
Practical boundary
A variable should be a short explanation, not a substitute data model.
Simple local variables make a calculation, API payload transformation, or decision in an application service clearer. Once a value carries several connected rules, such as an amount and currency or an order state, a small data object or value object is often more readable than several unrelated variables.
For a value shared across requests, a variable in one PHP process’s memory is not enough. Depending on its purpose, such state belongs in a database, cache, session, or explicitly passed context.
What to consider
Meaning, unit, and scope are part of code quality.
A small decision about a variable name affects review, testing, and safe changes in a larger system.
- name a boolean as a question, such as isPaid or hasAccess
- state the unit when it cannot be inferred safely from the type
- do not put sensitive values in names, logs, or error messages
- keep a variable in the smallest reasonable scope
- do not overwrite a value with a different meaning; create a new variable or type instead
Common questions
Variables in practice
Is a variable the same as a constant?
No. A variable can take on a different value during execution. A constant names a value that does not change during that execution of the program.
Must every PHP variable have an explicitly written type?
No. PHP determines a value’s type at runtime. At public boundaries for functions, properties, and return values, however, type declarations make the contract considerably more precise.
Is an object property just a variable?
Technically, it holds a value, but it has a different role: it belongs to a specific object and usually represents its state. A local variable lives only within the given block of code.
Why not simply name everything $data?
Because the name then does not say whether it represents an order, validated input, a price, an API payload, or a response. A precise name makes comprehension and review quicker.
How I work with PHP in practice
I build readable code on clear names and responsibilities.
In backend applications, I connect types, tests, and a clear domain model so that changes remain safely traceable.