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
+11-10Lines changed: 11 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ Text that goes into attributes should be run through `esc_attr()` so that single
21
21
22
22
### Indentation
23
23
24
-
Your indentation should always reflect logical structure. Use <strong>real tabs</strong> and <strong>not spaces</strong>, as this allows the most flexibility across clients.
24
+
Your indentation should always reflect logical structure. Use **real tabs** and **not spaces**, as this allows the most flexibility across clients.
25
25
26
26
Exception: if you have a block of code that would be more readable if things are aligned, use spaces:
27
27
@@ -32,7 +32,7 @@ Exception: if you have a block of code that would be more readable if things are
32
32
[tab]$foo5 = 'somevalue4';
33
33
```
34
34
35
-
For associative arrays, <em>each item</em> should start on a new line when the array contains more than one item:
35
+
For associative arrays, _each item_ should start on a new line when the array contains more than one item:
36
36
37
37
```php
38
38
$query = new WP_Query( array( 'ID' => 123 ) );
@@ -72,7 +72,7 @@ switch ( $type ) {
72
72
}
73
73
```
74
74
75
-
<strong>Rule of thumb:</strong> Tabs should be used at the beginning of the line for indentation, while spaces can be used mid-line for alignment.
75
+
**Rule of thumb:** Tabs should be used at the beginning of the line for indentation, while spaces can be used mid-line for alignment.
76
76
77
77
### Brace Style
78
78
@@ -111,7 +111,7 @@ foreach ( $items as $item ) {
111
111
}
112
112
```
113
113
114
-
Note that requiring the use of braces just means that <em>single-statement inline control structures</em> are prohibited. You are free to use the [alternative syntax for control structures](https://www.php.net/manual/en/control-structures.alternative-syntax.php) (e.g. `if`/`endif`, `while`/`endwhile`)—especially in your templates where PHP code is embedded within HTML, for instance:
114
+
Note that requiring the use of braces just means that _single-statement inline control structures_ are prohibited. You are free to use the [alternative syntax for control structures](https://www.php.net/manual/en/control-structures.alternative-syntax.php) (e.g. `if`/`endif`, `while`/`endwhile`)—especially in your templates where PHP code is embedded within HTML, for instance:
115
115
116
116
```php
117
117
<?php if ( have_posts() ) : ?>
@@ -219,7 +219,7 @@ if ( $a === $b ) { ?>
219
219
220
220
### No Shorthand PHP Tags
221
221
222
-
<strong>Important:</strong> Never use shorthand PHP start tags. Always use full PHP tags.
222
+
**Important:** Never use shorthand PHP start tags. Always use full PHP tags.
223
223
224
224
Correct:
225
225
@@ -568,23 +568,24 @@ switch ( $foo ) {
568
568
569
569
The `goto` statement must never be used.
570
570
571
-
The `eval()` construct is <em>very dangerous</em>, and is impossible to secure. Additionally, the `create_function()` function, which internally performs an `eval()`, is deprecated in PHP 7.2. Both of these must not be used.
571
+
The `eval()` construct is _very dangerous_, and is impossible to secure. Additionally, the `create_function()` function, which internally performs an `eval()`, is deprecated in PHP 7.2. Both of these must not be used.
572
572
573
573
### Error Control Operator `@`
574
574
575
575
As noted in the [PHP docs](https://www.php.net/manual/en/language.operators.errorcontrol.php):
576
576
577
-
<blockquote>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.</blockquote>
577
+
> 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
578
579
579
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:
580
580
581
-
<blockquote>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.</blockquote>
581
+
> 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
582
583
583
### Don't `extract()`
584
584
585
585
Per [#22400](https://core.trac.wordpress.org/ticket/22400"Remove all, or at least most, uses of extract() within WordPress"):
586
-
<blockquote>`extract()` is a terrible function that makes code harder to debug and harder to understand. We should discourage it's [sic] use and remove all of our uses of it.
587
-
</blockquote>
586
+
587
+
> `extract()` is a terrible function that makes code harder to debug and harder to understand. We should discourage it's [sic] use and remove all of our uses of it.
588
+
588
589
589
590
Joseph Scott has [a good write-up of why it's bad](https://blog.josephscott.org/2009/02/05/i-dont-like-phps-extract-function/).
0 commit comments