Separate lines for each declaration

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: php-code-style/single-var-declaration

Language: PHP

Severity: Notice

Category: Best Practices

Description

This rule requires that each variable declaration should be on its own separate line. This is important for readability and maintainability of the code. If multiple variables are declared on the same line, it can be easy to miss a declaration or confuse the values of the variables.

In order to comply with this rule, when declaring multiple variables, each one should be placed on its own line with its own assignment statement. This makes it clear what each variable is and what its initial value is.

Non-compliant code can be corrected by moving each declaration to its own line. For example, instead of writing $field1 = 1, $field2 = 2;, you can write: $field1 = 1; $field2 = 2;. This makes the code easier to read and understand.

Non-Compliant Code Examples

<?php
class Test
{
    $field1 = 1, $field2 = 2;
}

Compliant Code Examples

<?php
class Test
{
    $field1 = 1;
    $field2 = 2;
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis