1
0
mirror of https://github.com/jupeter/clean-code-php.git synced 2025-09-25 13:39:04 +02:00

Merge pull request #139 from peter-gribanov/comparison

Comparison section
This commit is contained in:
Tomáš Votruba
2018-02-26 20:19:14 +01:00
committed by GitHub

View File

@@ -390,34 +390,38 @@ function createMicrobrewery(string $breweryName = 'Hipster Brew Co.'): void
## Comparison
**[⬆ back to top](#table-of-contents)**
### Use [identical comparison](http://php.net/manual/en/language.operators.comparison.php)
**Not good:**
The simple comparison will convert the string in an integer.
```php
$a = '42';
$b = 42;
// Use the simple comparison that will convert the string in an int
if( $a != $b ) {
if ($a != $b) {
// The expression will always pass
}
```
The comparison $a != $b returns false but in fact it's true!
The string '42' is different than the int 42.
The comparison `$a != $b` returns `FALSE` but in fact it's `TRUE`!
The string `42` is different than the integer `42`.
**Good:**
Use the identical comparison will compare type and value
```php
if( $a !== $b ) {
//The expression is verified
}
The identical comparison will compare type and value.
```php
$a = '42';
$b = 42;
if ($a !== $b) {
// The expression is verified
}
```
The comparison $a !== $b returns true.
The comparison `$a !== $b` returns `TRUE`.
**[ back to top](#table-of-contents)**