language/basic-syntax.xml
6b09bb638aa64d1fad5f4a630a8da9a2692ce733
...
...
@@ -12,14 +12,47 @@
12
12
everything outside of a pair of opening and closing tags is ignored by the
13
13
PHP parser.
14
14
</para>
15
+

16
+
<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>.
19
+
</para>
20
+

21
+
<para>
22
+
<example>
23
+
<title>PHP Opening and Closing Tags</title>
24
+
<programlisting role="php">
25
+
<![CDATA[
26
+
1. <?php echo 'if you want to serve PHP code in XHTML or XML documents,
27
+
use these tags'; ?>
28
+

29
+
2. You can use the short echo tag to <?= 'print this string' ?>.
30
+
It's equivalent to <?php echo 'print this string' ?>.
31
+

32
+
3. <? echo 'this code is within short tags, but will only work '.
33
+
'if short_open_tag is enabled'; ?>
34
+
]]>
35
+
</programlisting>
36
+
</example>
37
+
</para>
38
+

15
39
<para>
16
-
PHP also allows for short open tag <literal>&lt;?</literal> (which is
17
-
discouraged since it is only available if enabled using the
18
-
<link linkend="ini.short-open-tag">short_open_tag</link> &php.ini;
19
-
configuration file directive, or if PHP was configured with the
20
-
<option>--enable-short-tags</option> option).
40
+
Short tags (example three) are available by default but can be disabled
41
+
either via the <link linkend="ini.short-open-tag">short_open_tag</link>
42
+
&php.ini; configuration file directive, or are disabled by default if
43
+
PHP is built with the <option>--disable-short-tags</option> configuration.
21
44
</para>
22
45
<para>
46
+
<note>
47
+
<para>
48
+
As short tags can be disabled it is recommended to only use the normal
49
+
tags (<code>&lt;?php ?&gt;</code> and <code>&lt;?= ?&gt;</code>) to
50
+
maximise compatibility.
51
+
</para>
52
+
</note>
53
+
</para>
54
+

55
+
<para>
23
56
If a file contains only PHP code, it is preferable to omit the PHP closing tag
24
57
at the end of the file. This prevents accidental whitespace or new lines
25
58
being added after the PHP closing tag, which may cause unwanted effects
...
...
@@ -40,36 +73,8 @@ echo "Last statement";
40
73
</programlisting>
41
74
</informalexample>
42
75
</para>
43
-
<para>
44
-
<table>
45
-
&reftitle.changelog;
46
-
<tgroup cols="2">
47
-
<thead>
48
-
<row>
49
-
<entry>&Version;</entry>
50
-
<entry>&Description;</entry>
51
-
</row>
52
-
</thead>
53
-
<tbody>
54
-
<row>
55
-
<entry>7.0.0</entry>
56
-
<entry>
57
-
The ASP tags <code>&lt;%</code>, <code>%&gt;</code>,
58
-
<code>&lt;%=</code>, and the script tag
59
-
<code>&lt;script language="php"&gt;</code> are removed from PHP.
60
-
</entry>
61
-
</row>
62
-
<row>
63
-
<entry>5.4.0</entry>
64
-
<entry>
65
-
The tag &lt;?= is always available regardless of the short_open_tag ini setting.
66
-
</entry>
67
-
</row>
68
-
</tbody>
69
-
</tgroup>
70
-
</table>
71
-
</para>
72
76
</sect1>
77
+

73
78
<sect1 xml:id="language.basic-syntax.phpmode">
74
79
<title>Escaping from HTML</title>
75
80
<para>
...
...
@@ -86,7 +91,7 @@ echo "Last statement";
86
91
</programlisting>
87
92
</informalexample>
88
93
This works as expected, because when the PHP interpreter hits the ?&gt; closing
89
-
tags, it simply starts outputting whatever it finds (except for an
94
+
tags, it simply starts outputting whatever it finds (except for the
90
95
immediately following newline - see
91
96
<link linkend="language.basic-syntax.instruction-separation">instruction separation</link>)
92
97
until it hits another opening tag unless in the middle of a conditional
...
...
@@ -119,107 +124,50 @@ echo "Last statement";
119
124
<function>echo</function> or <function>print</function>.
120
125
</para>
121
126
<para>
122
-
In PHP 5, there are up to five different pairs of opening and closing tags
123
-
available in PHP, depending on how PHP is configured. Two of these,
124
-
<code>&lt;?php ?&gt;</code> and
125
-
<code>&lt;script language="php"&gt; &lt;/script&gt;</code>, are always
126
-
available. There is also the short echo tag <code>&lt;?= ?&gt;</code>,
127
-
which is always available in PHP 5.4.0 and later.
128
-
</para>
129
-
<para>
130
-
The other two are short tags and <productname>ASP</productname> style
131
-
tags. As such, while some people find short tags and
132
-
<productname>ASP</productname> style tags convenient, they are less
133
-
portable, and generally not recommended.
134
127
<note>
135
128
<para>
136
-
Also note that if you are embedding PHP within XML or XHTML
137
-
you will need to use the &lt;?php ?&gt; tags to remain
138
-
compliant with standards.
129
+
If PHP is embeded within XML or XHTML the normal PHP
130
+
<code>&lt;?php ?&gt;</code> must be used to remain compliant
131
+
with the standards.
139
132
</para>
140
133
</note>
141
134
</para>
135
+
</sect1>
136
+

137
+
<sect1 xml:id="language.basic-syntax.instruction-separation">
138
+
<title>Instruction separation</title>
142
139
<para>
143
-
PHP 7 removes support for <productname>ASP</productname> tags and
144
-
<code>&lt;script language="php"&gt;</code> tags. As such, we recommend
145
-
only using <code>&lt;?php ?&gt;</code> and <code>&lt;?= ?&gt;</code> when
146
-
writing PHP code to maximise compatibility.
140
+
As in C or Perl, PHP requires instructions to be terminated
141
+
with a semicolon at the end of each statement. The closing tag
142
+
of a block of PHP code automatically implies a semicolon; you
143
+
do not need to have a semicolon terminating the last line of a
144
+
PHP block. The closing tag for the block will include the immediately
145
+
trailing newline if one is present.
147
146
</para>
147
+

148
148
<para>
149
149
<example>
150
-
<title>PHP Opening and Closing Tags</title>
150
+
<title>Example showing the closing tag encompassing the trailing newline</title>
151
151
<programlisting role="php">
152
152
<![CDATA[
153
-
1. <?php echo 'if you want to serve PHP code in XHTML or XML documents,
154
-
use these tags'; ?>
155
-

156
-
2. You can use the short echo tag to <?= 'print this string' ?>.
157
-
It's always enabled in PHP 5.4.0 and later, and is equivalent to
158
-
<?php echo 'print this string' ?>.
159
-

160
-
3. <? echo 'this code is within short tags, but will only work '.
161
-
'if short_open_tag is enabled'; ?>
162
-

163
-
4. <script language="php">
164
-
echo 'some editors (like FrontPage) don\'t
165
-
like processing instructions within these tags';
166
-
</script>
167
-
This syntax is removed in PHP 7.0.0.
168
-

169
-
5. <% echo 'You may optionally use ASP-style tags'; %>
170
-
Code within these tags <%= $variable; %> is a shortcut for this code <% echo $variable; %>
171
-
Both of these syntaxes are removed in PHP 7.0.0.
153
+
<?php echo "Some text"; ?>
154
+
No newline
155
+
<?= "But newline now" ?>
172
156
]]>
173
157
</programlisting>
158
+
&example.outputs;
159
+
<screen>
160
+
<![CDATA[
161
+
Some textNo newline
162
+
But newline now
163
+
]]>
164
+
</screen>
174
165
</example>
175
166
</para>
167
+

176
168
<para>
177
-
Short tags (example three) are only available when they are
178
-
enabled via the <link linkend="ini.short-open-tag">short_open_tag</link>
179
-
&php.ini; configuration file directive, or if PHP was configured
180
-
with the <option>--enable-short-tags</option> option.
181
-
</para>
182
-
<para>
183
-
<productname>ASP</productname> style tags (example five) are only
184
-
available when they are enabled via the
185
-
<link linkend="ini.asp-tags">asp_tags</link> &php.ini; configuration file
186
-
directive, and have been removed in PHP 7.0.0.
187
-
</para>
188
-
<para>
189
-
<note>
190
-
<para>
191
-
Using short tags should be avoided when developing applications
192
-
or libraries that are meant for redistribution, or deployment on
193
-
PHP servers which are not under your control, because short tags
194
-
may not be supported on the target server. For portable,
195
-
redistributable code, be sure not to use short tags.
196
-
</para>
197
-
</note>
198
-
<note>
199
-
<para>
200
-
In PHP 5.2 and earlier, the parser does not allow the
201
-
<literal>&lt;?php</literal> opening tag to be the only thing in a file.
202
-
This is allowed as of PHP 5.3 provided there are one or more whitespace
203
-
characters after the opening tag.
204
-
</para>
205
-
</note>
206
-
<note>
207
-
<para>
208
-
Starting with PHP 5.4, short echo tag <literal>&lt;?=</literal> is always recognized and
209
-
valid, regardless of the <link linkend="ini.short-open-tag">short_open_tag</link> setting.
210
-
</para>
211
-
</note>
212
-
</para>
213
-
</sect1>
214
-
<sect1 xml:id="language.basic-syntax.instruction-separation">
215
-
<title>Instruction separation</title>
216
-
<para>
217
-
As in C or Perl, PHP requires instructions to be terminated
218
-
with a semicolon at the end of each statement. The closing tag
219
-
of a block of PHP code automatically implies a semicolon; you
220
-
do not need to have a semicolon terminating the last line of a
221
-
PHP block. The closing tag for the block will include the immediately
222
-
trailing newline if one is present.
169
+
Examples of entering and exiting the PHP parser:
170
+

223
171
<informalexample>
224
172
<programlisting role="php">
225
173
<![CDATA[
...
...
@@ -272,11 +220,6 @@ echo "Last statement";
272
220
or <literal># ... ?&gt;</literal> WILL be printed:
273
221
?&gt; breaks out of PHP mode and returns to HTML mode, and
274
222
<literal>//</literal> or <literal>#</literal> cannot influence that.
275
-
If the <link linkend="ini.asp-tags">asp_tags</link> configuration directive
276
-
is enabled, it behaves the same with <literal>// %&gt;</literal> and
277
-
<literal># %&gt;</literal>.
278
-
However, the <literal>&lt;/script&gt;</literal> tag doesn't break out of PHP mode in
279
-
a one-line comment.
280
223
</simpara>
281
224
<para>
282
225
<informalexample>
283
226