mirror of
https://github.com/jupeter/clean-code-php.git
synced 2025-09-25 21:49:04 +02:00
Merge pull request #139 from peter-gribanov/comparison
Comparison section
This commit is contained in:
30
README.md
30
README.md
@@ -390,34 +390,38 @@ function createMicrobrewery(string $breweryName = 'Hipster Brew Co.'): void
|
|||||||
|
|
||||||
## Comparison
|
## Comparison
|
||||||
|
|
||||||
**[⬆ back to top](#table-of-contents)**
|
|
||||||
|
|
||||||
### Use [identical comparison](http://php.net/manual/en/language.operators.comparison.php)
|
### Use [identical comparison](http://php.net/manual/en/language.operators.comparison.php)
|
||||||
|
|
||||||
**Not good:**
|
**Not good:**
|
||||||
|
|
||||||
|
The simple comparison will convert the string in an integer.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$a = '42';
|
$a = '42';
|
||||||
$b = 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 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:**
|
**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)**
|
**[⬆ back to top](#table-of-contents)**
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user