Skip to content
On this page

Readable and descriptive variable names

When declaring variable names in Laravel (or any programming language), it's essential to prioritize readability and clarity to make your code easier to understand. Meaningful and descriptive variable names enhance the maintainability and readability of your codebase.

Use descriptive variable names

Choose names that clearly convey the purpose or content of the variable. Avoid using single-letter or abbreviated names, unless they are widely accepted and universally understood, like $i for a loop counter.

Bad:

php
$un = 'john_doe'; // unclear abbreviation
$tic = 10;        // unclear abbreviation

Good:

php
$username = 'john_doe';
$totalItemCount = 10;

Be consistent

Maintain consistency in naming conventions throughout your codebase. If you use camelCase for variables, stick with it consistently.

Bad:

php
$first_name = 'John'; // Mixing camelCase and snake_case
$LASTNAME = 'Doe';    // Inconsistent casing

Good:

php
$firstName = 'John';
$lastName = 'Doe';

Use meaningful variable names

If a variable's purpose might not be immediately apparent from its name, provide additional context or comments to clarify its role.

Bad:

php
$val = 123; // What does this represent?
$flag = true; // What is this flag for?

Good:

php
$currentPostId = 123; // ID of the currently displayed blog post
$isActive = true;     // Flag indicating whether the user is active

Avoid Overly Abbreviated Names

While brevity is good, avoid overly cryptic or abbreviated names that may be difficult to understand.

Bad:

php
$maxAtt = 3; // Unclear abbreviation
$usrCt = 1000; // Unclear abbreviation

Good:

php
$maxAttempts = 3;
$userCount = 1000;

Use Plural for Collections

When dealing with collections or arrays, use plural names to indicate that the variable holds multiple items.

Bad:

php
$user = ['john', 'jane', 'joe']; // Singular name but holds multiple users

Good:

php
$users = ['john', 'jane', 'joe'];

Avoid Using Reserved Words

Be cautious not to use PHP reserved words or names of built-in functions as variable names.

php
// Bad - 'unset' is a reserved word in PHP
$unset = 'Some value';