You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: wordpress-coding-standards/php.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ While not all code may fully comply with these standards (yet), all newly commit
6
6
7
7
Keep the following rules in mind when writing PHP code for WordPress, whether for core programming code, plugins, or themes.
8
8
9
-
See also:[PHP Inline Documentation Standards](https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/).
9
+
Also see[PHP Inline Documentation Standards](https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/) for further guidelines.
10
10
11
11
## Single and Double Quotes
12
12
@@ -133,7 +133,7 @@ Note that requiring the use of braces means that _single-statement inline contro
133
133
134
134
Using long array syntax ( `array( 1, 2, 3 )` ) for declaring arrays is generally more readable than short array syntax ( `[ 1, 2, 3 ]` ), particularly for those with vision difficulties. Additionally, it's much more descriptive for beginners.
135
135
136
-
Arrays must be declared using the long array syntax.
136
+
Arrays must be declared using long array syntax.
137
137
138
138
## Closures (Anonymous Functions)
139
139
@@ -237,11 +237,11 @@ Incorrect:
237
237
238
238
## Remove Trailing Spaces
239
239
240
-
Remove trailing whitespace at the end of each line. Omitting the closing PHP tag at the end of a file is preferred. If you use the tag, make sure you remove the trailing whitespace.
240
+
Remove trailing whitespace at the end of each line. Omitting the closing PHP tag at the end of a file is preferred. If you use the tag, make sure you remove trailing whitespace.
241
241
242
242
## Space Usage
243
243
244
-
Always put spaces after commas, and on both sides of the logical, comparison, string, and assignment operators.
244
+
Always put spaces after commas, and on both sides of logical, comparison, string and assignment operators.
245
245
246
246
```php
247
247
x === 23
@@ -279,7 +279,7 @@ When performing logical comparisons, do it like so:
279
279
if ( ! $foo ) { ...
280
280
```
281
281
282
-
[Type casts](https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting) must be lowercase. Always prefer the short form of type casts, `(int)` instead of `(integer)` and `(bool)` rather than `(boolean)`. For float casts use `(float)`, not `(real)` which is [deprecated](https://wiki.php.net/rfc/deprecations_php_7_4#the_real_type) in PHP 7.4, and removed in PHP 8:
282
+
[Type casts](https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting) must be lowercase. Always prefer the short form of type casts, `(int)` instead of `(integer)` and `(bool)` rather than `(boolean)`. For float casts use `(float)`, not `(real)` which is [deprecated](https://www.php.net/manual/en/migration74.deprecated.php#migration74.deprecated.core.real) in PHP 7.4, and removed in PHP 8:
283
283
284
284
```php
285
285
foreach ( (array) $foo as $bar ) { ...
@@ -348,7 +348,7 @@ See [Data Validation](https://developer.wordpress.org/plugins/security/data-vali
348
348
349
349
Avoid touching the database directly. If there is a defined function that can get the data you need, use it. Database abstraction (using functions instead of queries) helps keep your code forward-compatible and, in cases where results are cached in memory, it can be many times faster.
350
350
351
-
If you must touch the database, consider discussing it in Slack and creating a [Trac](https://core.trac.wordpress.org/) ticket.
351
+
If you must touch the database, consider creating a [Trac](https://core.trac.wordpress.org/) ticket. There you can discuss the possibility of adding a new function to cover the functionality you wanted, for the next WordPress version.
352
352
353
353
## Naming Conventions
354
354
@@ -457,7 +457,7 @@ Dynamic hooks should be named using interpolation rather than concatenation for
457
457
458
458
Dynamic hooks are hooks that include dynamic values in their tag name, e.g. `{$new_status}_{$post->post_type}` (publish_post).
459
459
460
-
Variables used in hook tags should be wrapped in curly braces `{` and `}`, with the complete outer tag name wrapped in double-quotes. This is to ensure PHP can correctly parse the given variables' types within the interpolated string.
460
+
Variables used in hook tags should be wrapped in curly braces `{` and `}`, with the complete outer tag name wrapped in doublequotes. This is to ensure PHP can correctly parse the given variables' types within the interpolated string.
@@ -575,11 +575,11 @@ The `eval()` construct is _very dangerous_ and is impossible to secure. Addition
575
575
576
576
As noted in the [PHP docs](https://www.php.net/manual/en/language.operators.errorcontrol.php):
577
577
578
-
> PHP supports one error control operator: the at sign (`@`). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
578
+
> PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any diagnostic error that might be generated by that expression will be suppressed.
579
579
580
580
While this operator does exist in Core, it is often used lazily instead of doing proper error checking. Its use is highly discouraged, as even the PHP docs also state:
581
581
582
-
> Warning: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.
582
+
> 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.
0 commit comments