How to Tidy Your PHP Code: Essential Formatting Rules and Best Practices

This article outlines practical PHP coding standards—including line breaks, spacing, naming conventions, PSR guidelines, and useful habits like method‑chaining layout and array formatting—to help developers write cleaner, more maintainable backend code.

21CTO
21CTO
21CTO
How to Tidy Your PHP Code: Essential Formatting Rules and Best Practices

1. Unified Coding Standards

Consistent coding standards in PHP focus on three main aspects: line breaks, spaces, and variable naming. Following PSR recommendations and using tools such as php‑cs‑fixer can enforce these rules automatically.

Without a standard, developers may write wildly different styles for the same construct, e.g., various if / else layouts, leading to messy and hard‑to‑read files.

# Single‑statement without braces
if (true) doSomething();

# Braces with line breaks
if (true) {
    doSomething();
} else {
    doElseThings();
}

# Keyword spacing, indentation, etc.

Variable and function naming should also be consistent. Examples include all‑lowercase names, underscore‑prefixed names, or class names that mix case and underscores, but mixing styles within a project should be avoided.

# All lowercase
$someparam1 = null;

# Underscore prefix
$_some_param_1 = null;

# Mixed case with underscore in class name
class Abstract_ClassA {
    // ...
}

2. Good Coding Habits

PSR does not dictate line‑break placement or indentation for long expressions. Developers should adopt habits such as breaking method‑chaining calls onto separate lines, especially for database queries.

# Long chain without breaks (hard to read)
$result = $this->db->select('id')->where('a', 1)->groupBy('a')->orderBy('id','DESC')->result();

# Recommended: one condition per line with proper indentation
$result = $this->db->select('id')
    ->where('a', 1)
    ->groupBy('a')
    ->orderBy('id', 'DESC')
    ->result();

When defining arrays with long string elements, split the array across multiple lines for clarity.

$array = [
    'abcdefg',
    'acbdfeg',
    'bcadgfe',
    'cdadgef',
];

3. Finding the Best Approach

Optimal code leverages PHP language features and framework capabilities to reduce redundancy. Examples include using the ternary operator efficiently, encapsulating repeated logic, and employing functions like array_map with references ( &) when appropriate.

# Safe retrieval of a POST parameter with a default value
$param = isset($_POST['param']) ? $_POST['param'] : '';

# Framework‑specific shortcut (may be too verbose)
$param = $this->request->data['param'] ?? '';

When processing arrays, array_map and careful handling of array pointers can simplify code, and using return statements helps keep functions focused.

4. Review Your Own Code

Periodically reviewing previously written code often reveals better implementations. As experience grows, new insights emerge, making code reviews a valuable practice for continuous improvement.

5. Share with Your Team

Teamwide adoption of a unified standard and strong enforcement ensures consistent code quality across the project. One person ignoring the rules can quickly degrade the entire codebase.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

best practicescoding standardscode stylePSRPHP CS Fixer
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.