language/basic-syntax.xml
0e618211e53c66f33762be225a4d57c08ef4b2f7
...
...
@@ -1,23 +1,27 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-
<chapter xml:id="language.basic-syntax" xmlns="http://docbook.org/ns/docbook">
3
+
<chapter xml:id="language.basic-syntax" xmlns="http://docbook.org/ns/docbook" annotations="interactive">
4
4
<title>Basic syntax</title>
5
5
<sect1 xml:id="language.basic-syntax.phptags">
6
6
<title>PHP tags</title>
7
7
<para>
8
-
When PHP parses a file, it looks for opening and closing tags, which are
9
-
<literal>&lt;?php</literal> and <literal>?&gt;</literal> which tell PHP to
10
-
start and stop interpreting the code between them. Parsing in this manner
11
-
allows PHP to be embedded in all sorts of different documents, as
12
-
everything outside of a pair of opening and closing tags is ignored by the
13
-
PHP parser.
8
+
When PHP processes a file, it recognizes the opening and closing
9
+
tags, <literal>&lt;?php</literal> and <literal>?&gt;</literal>, to
10
+
define the boundaries of PHP code execution. Content outside these
11
+
tags is ignored by the PHP parser, allowing PHP to seamlessly embed
12
+
in various document types.
14
13
</para>
15
14

16
15
<para>
17
-
PHP includes a short echo tag <literal>&lt;?=</literal> which is a
18
-
short-hand to the more verbose <code>&lt;?php echo</code>.
16
+
A whitespace character (space, tab, or newline) must follow
17
+
<literal>&lt;?php</literal> to ensure proper token separation.
18
+
Omitting this whitespace will result in a syntax error.
19
19
</para>
20
20

21
+
<para>
22
+
PHP also includes the short echo tag <literal>&lt;?=</literal>,
23
+
which is shorthand for <code>&lt;?php echo</code>.
24
+
</para>
21
25
<para>
22
26
<example>
23
27
<title>PHP Opening and Closing Tags</title>
...
...
@@ -45,7 +49,7 @@
45
49
<para>
46
50
<note>
47
51
<para>
48
-
As short tags can be disabled it is recommened to only use the normal
52
+
As short tags can be disabled it is recommended to only use the normal
49
53
tags (<code>&lt;?php ?&gt;</code> and <code>&lt;?= ?&gt;</code>) to
50
54
maximise compatibility.
51
55
</para>
...
...
@@ -53,25 +57,28 @@
53
57
</para>
54
58

55
59
<para>
56
-
If a file contains only PHP code, it is preferable to omit the PHP closing tag
57
-
at the end of the file. This prevents accidental whitespace or new lines
60
+
If a file ends with PHP code, it is preferable to omit the PHP closing tag
61
+
at the end of the file. This prevents accidental whitespace or new lines
58
62
being added after the PHP closing tag, which may cause unwanted effects
59
63
because PHP will start output buffering when there is no intention from
60
64
the programmer to send any output at that point in the script.
61
-
<informalexample>
65
+
</para>
66
+
<para>
67
+
<example>
68
+
<title>PHP Code Only File</title>
62
69
<programlisting role="php">
63
70
<![CDATA[
64
71
<?php
65
-
echo "Hello world";
72
+
echo "Hello world\n";
66
73

67
74
// ... more code
68
75

69
-
echo "Last statement";
76
+
echo "Last statement\n";
70
77

71
78
// the script ends here with no PHP closing tag
72
79
]]>
73
80
</programlisting>
74
-
</informalexample>
81
+
</example>
75
82
</para>
76
83
</sect1>
77
84

...
...
@@ -79,9 +86,12 @@ echo "Last statement";
79
86
<title>Escaping from HTML</title>
80
87
<para>
81
88
Everything outside of a pair of opening and closing tags is ignored by the
82
-
PHP parser which allows PHP files to have mixed content. This allows PHP
89
+
PHP parser which allows PHP files to have mixed content. This allows PHP
83
90
to be embedded in HTML documents, for example to create templates.
84
-
<informalexample>
91
+
</para>
92
+
<para>
93
+
<example>
94
+
<title>Embedding PHP in HTML</title>
85
95
<programlisting role="php">
86
96
<![CDATA[
87
97
<p>This is going to be ignored by PHP and displayed by the browser.</p>
...
...
@@ -89,7 +99,9 @@ echo "Last statement";
89
99
<p>This will also be ignored by PHP and displayed by the browser.</p>
90
100
]]>
91
101
</programlisting>
92
-
</informalexample>
102
+
</example>
103
+
</para>
104
+
<para>
93
105
This works as expected, because when the PHP interpreter hits the ?&gt; closing
94
106
tags, it simply starts outputting whatever it finds (except for the
95
107
immediately following newline - see
...
...
@@ -147,7 +159,7 @@ echo "Last statement";
147
159

148
160
<para>
149
161
<example>
150
-
<title>Example showing the closing tag encompasing the trailing newline</title>
162
+
<title>Example showing the closing tag encompassing the trailing newline</title>
151
163
<programlisting role="php">
152
164
<![CDATA[
153
165
<?php echo "Some text"; ?>
...
...
@@ -166,21 +178,22 @@ But newline now
166
178
</para>
167
179

168
180
<para>
169
-
Examples of entering and exiting the PHP parser:
170
-

171
-
<informalexample>
181
+
<example>
182
+
<title>Examples of entering and exiting the PHP parser</title>
172
183
<programlisting role="php">
173
184
<![CDATA[
174
185
<?php
175
-
echo 'This is a test';
186
+
echo "This is a test\n";
176
187
?>
177
188

178
-
<?php echo 'This is a test' ?>
189
+
<?php echo "This is a test\n" ?>
179
190

180
-
<?php echo 'We omitted the last closing tag';
191
+
<?php echo "We omitted the last closing tag\n";
181
192
]]>
182
193
</programlisting>
183
-
</informalexample>
194
+
</example>
195
+
</para>
196
+
<para>
184
197
<note>
185
198
<para>
186
199
The closing tag of a PHP block at the end of a file is optional,
...
...
@@ -198,20 +211,22 @@ But newline now
198
211
<title>Comments</title>
199
212
<para>
200
213
PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments. For example:
201
-

202
-
<informalexample>
214
+
</para>
215
+
<para>
216
+
<example>
217
+
<title>Comments</title>
203
218
<programlisting role="php">
204
219
<![CDATA[
205
220
<?php
206
-
echo 'This is a test'; // This is a one-line c++ style comment
221
+
echo "This is a test\n"; // This is a one-line c++ style comment
207
222
/* This is a multi line comment
208
223
yet another line of comment */
209
-
echo 'This is yet another test';
210
-
echo 'One Final Test'; # This is a one-line shell-style comment
224
+
echo "This is yet another test\n";
225
+
echo "One Final Test\n"; # This is a one-line shell-style comment
211
226
?>
212
227
]]>
213
228
</programlisting>
214
-
</informalexample>
229
+
</example>
215
230
</para>
216
231
<simpara>
217
232
The "one-line" comment styles only comment to the end of
...
...
@@ -222,18 +237,19 @@ But newline now
222
237
<literal>//</literal> or <literal>#</literal> cannot influence that.
223
238
</simpara>
224
239
<para>
225
-
<informalexample>
240
+
<example>
241
+
<title>One Line Comments</title>
226
242
<programlisting role="php">
227
243
<![CDATA[
228
244
<h1>This is an <?php # echo 'simple';?> example</h1>
229
245
<p>The header above will say 'This is an example'.</p>
230
246
]]>
231
247
</programlisting>
232
-
</informalexample>
248
+
</example>
233
249
</para>
234
250
<simpara>
235
251
'C' style comments end at the first <literal>*/</literal> encountered.
236
-
Make sure you don't nest 'C' style comments. It is easy to make this
252
+
Make sure you don't nest 'C' style comments. It is easy to make this
237
253
mistake if you are trying to comment out a large block of code.
238
254
</simpara>
239
255
<para>
240
256