---
title: Do Not Leave Unused Variables
impact: LOW
impactDescription: reduces code noise and prevents potential bugs
tags: variables, cleanup, quality, php
---

## Do Not Leave Unused Variables

Unused variables make the code harder to read and often indicate incomplete refactoring or accidental logic errors.

**Incorrect (unused variables):**

```php
function processOrder($order) {
    $user = $order->user;      // Declared but never used
    $timestamp = time();       // Never used
    $items = $order->items;
    
    return array_map(fn($item) => $item->name, $items);
}

// Unused method parameters
function deleteUser($id, $force = false) {
    // $force is never used in the logic
    return User::destroy($id);
}
```

**Correct (clean logic):**

```php
function processOrder($order) {
    return array_map(fn($item) => $item->name, $order->items);
}

// Remove unused parameters if they aren't part of an interface
function deleteUser($id) {
    return User::destroy($id);
}
```

**Common Scenarios:**
- Variables assigned but never read.
- Function parameters that are not used in the body.
- Loop variables (like `$key => $value`) where the key is never used.

**Exceptions:**
- Implementing an interface where a method signature is fixed, but you don't need all parameters. In some tools, prefixing with an underscore (e.g., `$_param`) can signal intentional non-use.

**Tools:** PHPStan, Psalm, PHP_CodeSniffer (`SlevomatCodingStandard.Variables.UnusedVariable`)
