File: cs-for-examples.md

package info (click to toggle)
php-doc 20250827~git.abe740d%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 71,968 kB
  • sloc: xml: 985,760; php: 25,504; javascript: 671; sh: 177; makefile: 37
file content (275 lines) | stat: -rw-r--r-- 6,126 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Coding standard for examples

Examples need to be clear and simple, but should show the possibilities and
usage of the functions used.  Only use OOP features where you would like to
present them, use simple functions in other areas.

## Requirements

- When appropriate, use superglobals
- Never generate PHP errors (`E_ALL|E_STRICT` friendly)
- Be short and generic
- Follow the current [PHP-FIG PER Coding Style][per-cs]

## Program listing roles  (`<programlisting role="xxx">`)
   
PHP examples should always be in `<programlisting role="php">`.
Only PHP examples should have this role.
Other possible roles are:
   
 - c           (C code) 
 - html        (100% XHTML)
 - php         (Some PHP)
 - shell       (commandline, bash, etc)
 - sql         (SQL statements)
 - apache-conf (Apache)

## Titles

When appropriate, it's encouraged to include the function name in
the title, for example:

```xml
<title>A <function>strlen</function> example</title>
```

## Code placement

The contents start at column/row 0 in the example.  For example, this
means your example's content will be flush against the `<![CDATA[` tag.

## PHP tags

The old short and alternative tags (`<?` and `<%`) are obsolete and
should no longer be used in the documentation.

## CDATA

Always use `<![CDATA[ ... ]]>` as this increases the readability of
the examples.  For example, you literally write `<` instead of `&lt;`
inside of CDATA.  Nothing in CDATA is parsed, it's taken literally.
So, you cannot use links, dev-comments, `<function>`, etc.

## Deprecated code

Do not use aliases or deprecated syntax.
   
## Use of newer PHP features

If an example uses features specific to a newer version of PHP, such
as new arguments that has been added, this should be made clear in the
title of the example.

```php   
<example>
 <title><function>foo</function> with second argument added as of PHP 8.1.0<title>
 <programlisting role="php">
<![CDATA[
<?php
foo('bar', 'baz');
?>
]]>
 </programlisting>
</example>
```
   
If appropriate, show examples that work in older versions of PHP but
do not use reserved function names.  For example, a PHP 4.2.3 version
of `file_get_contents()` should not be named `file_get_contents()`.

## Use of booleans in examples

Do not use entities such as `&true;` in examples but instead write them
out as `true`, `false`, and/or `null`.

## Spacing

Never use tabs, only use spaces.  Indentation levels are four spaces 
and do not indent the first level of code.  For example:
   
### Good:
```php
<?php
$str = 'Hello World';
function foo($str)
{
   return $str;
}
?>
```
   
### Bad:

```php
<?php
   $str = 'Hello World';
   function foo($str) 
   {
       return $str;
   }
?>
```

## IDs

It is a good idea to add xml:id to the examples. IDs generate anchors and
make it possible to list them in an Example Listing Appendix in the future.


## Error handling:

This section isn't yet complete but there are three main ways to
implement error handling in the PHP manual:

a) Use of the `or` operator.
  
This is okay for development code but not ideal for production as use
of `or` is rather limiting.  An example use:
  
```php
foobar($lname) or die(...);
```

b) A boolean check, along with braces
  
This allows additional expressions inside the braces but requires
more code.  This is the preferred method.  An example use:
     
```php
     if (!foobar($lname)) {
         ...
         exit;
     }
```
     
c) `trigger_error()`
  
There is debate on whether to use `trigger_error()` in the examples so for
now, do not use it (at least until the error handling docs are updated).

## About Variables/Constants/Strings

1. Don't use variables which are not set in examples.

2. Constants should always be all-uppercase.

3. Use single quotes ' when appropriate.

4. For output use `echo`, instead of `print`. Don't use unnecessary
   string concatenation (`.`) with `echo`, use `,` instead.

5. Lowercase HTML tags.

6. Variables in strings:

* Strings in strings

It is acceptable to use either string interpolation or concatenation
when composing strings, but `,` should be used to separate multiple
expressions for `echo` is preferred to string concatenation.

```php
$output = "bar is $bar";
echo "bar is $bar";
$output = "bar is {$bar}";
echo "bar is {$bar}";
```
vs
```php
$output = 'bar is ' . $bar;
echo 'bar is ', $bar;
```
   
* Arrays in strings
   
As constants aren't looked for in strings, the following is fine but
may confuse newbies so it's not to be used in examples:
     
```php
$variable = "an $array[key] key";
echo "an $array[key] key";
```

Instead, consider these:
     
```php
$variable = "an {$array['key']} key";
echo "an {$array['key']} key";
echo 'an ', $array['key'], ' key';
```

## Function naming

Procedural function names should be lowercase.  If multiple words are
needed in the function name, use a `_` (also known as snake\_case).
Example: `foo_function();`

OOP method names should follow the standard coding style which uses
camelCase, such as `fooFunction()`.

## Example output

For very short example printouts, use C++ style comment (`//`) on the
line where the output occurs, or in the description above the line:

```php
echo $var; // 32
```

For longer example printouts, there are, a couple methods which are
acceptable.  Medium sized output may be inline with the example
itself through use of `/* comments */`, for example:

```php
<?php
$arr = foo();
var_dump($arr);

/* Outputs:

array(3) {
  [0]=>
  string(5) "bread"
  [1]=>
  string(13) "peanut butter"
  [2]=>
  string(3) "jam"
}
*/
?>
```

For longer example printouts, use the `<screen>` container in conjunction
with `<![CDATA[...]]>`

```xml
 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <title>A <function>foo</function> example</title>
   <programlisting role="php">
<![CDATA[
<?php
$arr = foo();
var_dump($arr);
?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
}
]]>
   </screen>
  </example>
 </refsect1>
```

[per-cs]: https://www.php-fig.org/per/coding-style/