Skip to content

Commit

Permalink
Add chapter about increment/decrement operators
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo-d committed Aug 26, 2022
1 parent 0fe5f02 commit db21f68
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions wordpress-coding-standards/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,26 @@ While this operator does exist in Core, it is often used lazily instead of doing

> Warning: Prior to PHP 8.0.0, it was possible for the @ operator to disable critical errors that will terminate script execution. For example, prepending @ to a call of a function that did not exist, by being unavailable or mistyped, would cause the script to terminate with no indication as to why.
### Increment/decrement operators

When writing increment (`++`) or decrement (`--`) operators, there should be no spaces between the operator and the variable it applies to. Pre-increment/decrement should be favoured over post-increment/decrement for stand-alone statements.

Pre-increment/decrement will increment/decrement and then return, while post-increment/decrement will return and then increment/decrement.
Using the "pre" version is slightly more performant and can prevent future bugs when code gets moved around.

```php
// Correct.
for ( $i = 0; $i < 10; $i++ ) {}

--$a;

// Incorrect.
for ( $i = 0; $i < 10; $i ++ ) {}

$a--;
++ $b; // Multiple spaces.
```

## Database

### Database Queries
Expand Down

0 comments on commit db21f68

Please sign in to comment.