---
title: "Comparison"
canonical_url: "https://handbook.propay.co.za/clean-code/comparison"
---
# Comparison

## Use [identical comparison](https://php.net/manual/en/language.operators.comparison.php)

Bad:<br />


The loose comparison will convert the string into an integer.

<code-group>

```php [PHP]
$a = '42';
$b = 42;

if ($a != $b) {
    // The expression will always pass
}
```

</code-group>

The comparison `$a != $b` returns `FALSE` but in fact it's `TRUE`! The string `42` is different from the integer `42`.

Good:<br />


The identical comparison will compare type and value.

<code-group>

```php [PHP]
$a = '42';
$b = 42;

if ($a !== $b) {
    // The expression is verified
}
```

</code-group>

The comparison `$a !== $b` returns `TRUE`.

## Null coalescing operator

Null coalescing is an operator [introduced in PHP 7](https://www.php.net/manual/en/migration70.new-features.php). The null coalescing operator `??` has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with `isset()`. It returns its first operand if it exists and is not `null`; otherwise it returns its second operand.

<code-group>

```php [PHP]
// BAD
if (isset($_GET['name'])) {
    $name = $_GET['name'];
} elseif (isset($_POST['name'])) {
    $name = $_POST['name'];
} else {
    $name = 'nobody';
}

// GOOD
$name = $_GET['name'] ?? $_POST['name'] ?? 'nobody';
```

</code-group>


## Sitemap

See the full [sitemap](https://handbook.propay.co.za/sitemap.md) for all pages.
