---
title: Do not assign a variable to itself
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Do not assign a variable to itself
---

# Do not assign a variable to itself

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site). ().
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `php-best-practices/no-self-assign`

**Language:** PHP

**Severity:** Notice

**Category:** Error Prone

## Description{% #description %}

This rule aims to prevent redundancy in code. Assigning a variable to itself is a pointless operation that can lead to confusion and clutter in the code, making it harder to read and understand. It's also a potential indicator of a mistake or oversight in the code, where a different assignment was intended.

This rule is important because clean, efficient code is a hallmark of good programming. Unnecessary assignments can slow down the runtime of the script and, more importantly, make the code harder to maintain and debug. In a large codebase, such redundancies can add up to significant inefficiencies.

To avoid this, always ensure that the right-hand side of an assignment is not the same as the left-hand side. If you find yourself writing `var = var`, it's probably a mistake. Double-check your code to ensure that you're assigning the correct variables. If you're reassigning a variable to itself intentionally, consider whether this is really necessary and if there might be a cleaner way to achieve your goal.

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```php
<?php
$var = 1;
$var2 = 2;
$var = $var;
```

## Compliant Code Examples{% #compliant-code-examples %}

```php
<?php
$var = 1;
$var2 = 2;
$var = $var2;
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 