Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit de217b2

Browse files
authored
Update GRAMMAR.md
1 parent bca9936 commit de217b2

File tree

1 file changed

+10
-117
lines changed

1 file changed

+10
-117
lines changed

‎GRAMMAR.md‎

Lines changed: 10 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,35 @@
11
# Grammar
22

3-
* Global variables
3+
* Global variables.
44
* No. ಠ_ಠ
55
* If you are using `$_GLOBALS`, you have a pattern problem.
66

77
* 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))
88

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-
1049
## Strings
10510

10611
* Single quotes most of the time
10712

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`.
10914

11015
* For complex string interpolation, always use `sprintf()`.
11116

112-
* Use HEREDOC syntax for multi-line strings.
17+
* Use HEREDOC or NOWDOC syntax for multi-line strings.
11318

11419
## Ternary statements
11520

11621
* Don't test if the statement is `false`. That becomes confusing fast.
11722

118-
```php
119-
// Bad
120-
$value = !condition ? false : true;
23+
* Preferred formatting is:
12124

122-
// Good
123-
$value = condition ? true : false;
25+
```php
26+
$condition
27+
? true
28+
: false;
12429
```
12530

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.
12932

13033
```php
131-
// Encouraged
132-
if ('abc' === $abc) {}
133-
134-
// Discouraged
135-
if ($abc === 'abc') {}
34+
return $data[0] ?? null;
13635
```
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /