Separate lines for each declaration

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

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