PHP

PHP

Made by DeepSource

Useless post increment/decrement PHP-W1090

Bug risk
Major

Post increment and decrement return the existing value, and then increments, or decrements it. Therefore, assigning incremented value to itself doesn't actually change the value.

Since this code has no effect, it can safely be removed without any side effects for the existing logic. If the intention was to increment or decrement the value, please remove the assignment.

Bad practice

$count = 0;
foreach ($users as $user) {
    // loop implementation here

    $count = $count++; // Incrementing `$count` variable doesn't have any effect.
}

// Value of `$count` is zero here.
$count = 0;
foreach ($users as $user) {
    // loop implementation here

    $count = $count--;
}

// Value of `$count` is still zero here.

Recommended

$count = 0;
foreach ($users as $user) {
    // loop implementation here

    $count++;
}
$count = 0;
foreach ($users as $user) {
    // loop implementation here

    $count--;
}

References