|
1 | 1 | # Grammar |
2 | 2 |
|
3 | | -* Global variables |
| 3 | +* Global variables. |
4 | 4 | * No. ಠ_ಠ |
5 | 5 | * If you are using `$_GLOBALS`, you have a pattern problem. |
6 | 6 |
|
7 | 7 | * Code must not produce any warnings or errors when PHP's error reporting level is set to `error_reporting(-1)`. ([Source](https://twitter.com/rasmus/status/7448448829)) |
8 | 8 |
|
9 | | -* PHP-only files should have an opening `<?php` tag, but should not have a closing tag! Adding a closing PHP tag can occasionally cause strange issues when importing. As such, leave out the closing `?>` tag. |
10 | | - |
11 | | -* Do not use short open tags (`<?`) for files which are entirely PHP. |
12 | | - |
13 | | -* All control structures use braces, even if they’re optional, for files which are entirely PHP. |
14 | | - |
15 | | - ```php |
16 | | - // Bad |
17 | | - if (!is_null($args)) $abc = 'abc'; |
18 | | - |
19 | | - // Bad |
20 | | - if (!is_null($args)) |
21 | | - $abc = 'abc'; |
22 | | - |
23 | | - // Good |
24 | | - if (!is_null($args)) { |
25 | | - $abc = 'abc'; |
26 | | - } |
27 | | - ``` |
28 | | - |
29 | | -## View/Template Intermingling |
30 | | - |
31 | | -View/Template-layer code (code which intermingles PHP with HTML/non-PHP) allows the following exceptions: |
32 | | - |
33 | | -* Template echoes (`<?=`) are OK. |
34 | | - |
35 | | -* Short-open tags (`<?`) may be used to wrap control structures. |
36 | | - |
37 | | - ```php |
38 | | - <? if (condition): ?> |
39 | | - <tag>View-layer code.</tag> |
40 | | - <? endif ?> |
41 | | - ``` |
42 | | - |
43 | | -* `if`, `for`, `foreach`, `while`, and similar kinds of blocks use the `:` character to begin their statements. |
44 | | - |
45 | | - ```php |
46 | | - <? if (condition): ?> |
47 | | - ``` |
48 | | - |
49 | | -* `endif`, `endfor`, `endforeach`, `endwhile`, and similar kinds of blocks do NOT use a trailing `;` character. |
50 | | - |
51 | | - ```php |
52 | | - <? endif ?> |
53 | | - ``` |
54 | | - |
55 | | -* All `<? ... ?>` blocks have a single space just inside the open/close tokens. |
56 | | - |
57 | | - ```php |
58 | | - <? if (condition): ?> |
59 | | - <?= $variable ?> |
60 | | - <? endif ?> |
61 | | - ``` |
62 | | - |
63 | | -## Arrays |
64 | | - |
65 | | -* "New" array syntax should be used. |
66 | | - |
67 | | -* For multi-line arrays, the use of a trailing comma for the last item in the array is encouraged. This minimizes the impact of adding new items on successive lines, and helps to ensure no parse errors occur due to a missing comma. |
68 | | - |
69 | | - ```php |
70 | | - // Good |
71 | | - $indexed_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
72 | | - |
73 | | - // Good |
74 | | - $months_english = [ |
75 | | - 'January', |
76 | | - 'February', |
77 | | - 'March', |
78 | | - 'April', |
79 | | - 'May', |
80 | | - 'June', |
81 | | - 'July', |
82 | | - 'August', |
83 | | - 'September', |
84 | | - 'October', |
85 | | - 'November', |
86 | | - 'December', |
87 | | - ]; |
88 | | - ``` |
89 | | - |
90 | | -* If any array elements have their own line, they all should. Avoid inconsistency where some are on the same line and others are not. |
91 | | - |
92 | | - ```php |
93 | | - // Bad |
94 | | - $associative_array = array('Key1' => 'Value', |
95 | | - 'Key2' => 'Value', |
96 | | - 'Key3' => 'Value'); |
97 | | - |
98 | | - // Bad |
99 | | - $associative_array = array('Key1' => 'Value', |
100 | | - 'Key2' => 'Value', |
101 | | - 'Key3' => 'Value'); |
102 | | - ``` |
103 | | - |
104 | 9 | ## Strings |
105 | 10 |
|
106 | 11 | * Single quotes most of the time |
107 | 12 |
|
108 | | -* For simple string interpolation with double-quoted strings, use the `${variable}` format. Do NOT use `{$variable}` or `$variable`. |
| 13 | +* For simple string interpolation with double-quoted strings, use the `{$variable}` format. Do NOT use `${variable}` or `$variable`. |
109 | 14 |
|
110 | 15 | * For complex string interpolation, always use `sprintf()`. |
111 | 16 |
|
112 | | -* Use HEREDOC syntax for multi-line strings. |
| 17 | +* Use HEREDOC or NOWDOC syntax for multi-line strings. |
113 | 18 |
|
114 | 19 | ## Ternary statements |
115 | 20 |
|
116 | 21 | * Don't test if the statement is `false`. That becomes confusing fast. |
117 | 22 |
|
118 | | - ```php |
119 | | - // Bad |
120 | | - $value = !condition ? false : true; |
| 23 | +* Preferred formatting is: |
121 | 24 |
|
122 | | - // Good |
123 | | - $value = condition ? true : false; |
| 25 | + ```php |
| 26 | + $condition |
| 27 | + ? true |
| 28 | + : false; |
124 | 29 | ``` |
125 | 30 |
|
126 | | -* No more than a single condition should go into a ternary statement. Use a switch-case or if-else condition instead. |
127 | | - |
128 | | -* In conditions, the value should be on the left and the variable should be on the right. ([Supporting evidence](https://www.securecoding.cert.org/confluence/display/c/VOID+EXP21-C.+Place+constants+on+the+left+of+equality+comparisons)) |
| 31 | +* Use `??` when possible. |
129 | 32 |
|
130 | 33 | ```php |
131 | | - // Encouraged |
132 | | - if ('abc' === $abc) {} |
133 | | - |
134 | | - // Discouraged |
135 | | - if ($abc === 'abc') {} |
| 34 | + return $data[0] ?? null; |
136 | 35 | ``` |
137 | | - |
138 | | -## `==` or `===` |
139 | | - |
140 | | -* If you are comparing against an absolute value (e.g., a string, integer, boolean), always use `===`. |
141 | | - |
142 | | -* If you are comparing against a _truthy_ or _falsey_ value, always use `==`. |
0 commit comments