File: basic-syntax.xml

package info (click to toggle)
phpdoc 20020310-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 35,272 kB
  • ctags: 354
  • sloc: xml: 799,767; php: 1,395; cpp: 500; makefile: 200; sh: 140; awk: 51
file content (260 lines) | stat: -rw-r--r-- 7,783 bytes parent folder | download
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
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.26 $ -->
 <chapter id="language.basic-syntax">
  <title>Basic syntax</title>

   <!--
   
    NOTE:                             Last modified: 2001-05-16 13:00 GMT
    
    the language part is currently under heavy revision. Please do not
    not make any heavy (i.e. structural) modifications to this part 
    for a moment. 
    
    You'd also better not start any translation yet.
    
    Comments are always welcome at phpdoc@lists.php.net
    
    Progress:
    
     intro : DOESN'T EXIST - yet?
             new chapter, with some introductionary remarks?
             Will be discussed on the ML soon.
     basic-syntax: 
             FINISHED
             except maybe moving the 'advanced escaping'
             to a better place?
             TODO: 
              - nada
     types : Being revised. Added all new types
             Boolean and Integer are more or less finished.
             The rest isn't.
             TODO: 
              - why is $foo[bar] bad syntax?
              - what's the difference between unset($bla) and
                $bla = NULL; (it is different!)
              - $obj->{expr} syntax
              - (unset) cast?????
              - $bla = unset <== should've been nuked, don't mention it
              - $str{offset} syntax, rather than $str[offset]
              - read notes and apply when any of them are useful
              - remove notes which have been included here.
              - ...
     the rest: Not yet started with.
             TODO: 
              - ?
     oop   : has been revised by Kristian, DONE.
   -->
   
  <sect1 id="language.basic-syntax.phpmode">
   <title>Escaping from HTML</title>
  
   <para>
    When PHP parses a file, it simply passes the text of the file
    through until it encounters one of the special tags which tell it
    to start interpreting the text as PHP code. The parser then
    executes all the code it finds, up until it runs into a PHP
    closing tag, which tells the parser to just start passing the text
    through again. This is the mechanism which allows you to embed PHP
    code inside HTML: everything outside the PHP tags is left utterly
    alone, while everything inside is parsed as code.
   </para>

   <para>
    There are four sets of tags which can be used to denote blocks of
    PHP code. Of these, only two (&lt;?php. . .?&gt; and &lt;script
    language="php"&gt;. . .&lt;/script&gt;) are always available; the
    others can be turned on or off from the
    <filename>php.ini</filename> configuration file. While the
    short-form tags and ASP-style tags may be convenient, they are not
    as portable as the longer versions. Also, if you intend to embed
    PHP code in XML or XHTML, you will need to use the
    &lt;?php. . .?&gt; form to conform to the XML.
   </para>

   <para>
    The tags supported by PHP are:
   </para>

    <para>
     <example>
      <title>Ways of escaping from HTML</title>
      <programlisting role="php">
<![CDATA[
1.  <? echo ("this is the simplest, an SGML processing instruction\n"); ?>
    <?= expression ?> This is a shortcut for "<? echo expression ?>"
    
2.  <?php echo("if you want to serve XHTML or XML documents, do like this\n"); ?>

3.  <script language="php">
        echo ("some editors (like FrontPage) don't
              like processing instructions");
    </script>

4.  <% echo ("You may optionally use ASP-style tags"); %>
    <%= $variable; # This is a shortcut for "<% echo . . ." %>
]]>
      </programlisting>
     </example>
    </para>

   <para>
    The first way is only available if short tags have been
    enabled. This can be done via the <function>short_tags</function>
    function (PHP 3 only), by enabling the <link
    linkend="ini.short-open-tag">short_open_tag</link> configuration
    setting in the PHP config file, or by compiling PHP with the
    --enable-short-tags option to <command>configure</command>.
   </para>

   <para>
    Again, the second way is the generally preferred method, as it
    allows for the the use of PHP in XML-conformant code such as
    XHTML.
   </para>

   <para>
    The fourth way is only available if ASP-style tags have been
    enabled using the <link linkend="ini.asp-tags">asp_tags</link>
    configuration setting.

    <note>
     <para>Support for ASP-style tags was added in 3.0.4.</para>
    </note>
   </para>

   <para>
    The closing tag for the block will include the immediately
    trailing newline if one is present. Also, the closing tag
    automatically implies a semicolon; you do not need to have a
    semicolon terminating the last line of a PHP block.
   </para>
    
    <para>
    PHP allows you to use structures like this:
    <example><title>Advanced escaping</title>
     <programlisting role="php">
<![CDATA[
<?php
if ($expression) { 
    ?>
    <strong>This is true.</strong>
    <?php 
} else { 
    ?>
    <strong>This is false.</strong>
    <?php 
}
?>
]]>
     </programlisting>
    </example>
    This works as expected, because when PHP hits the ?&gt; closing
    tags, it simply starts outputting whatever it finds until it hits
    another opening tag. The example given here is contrived, of
    course, but for outputting large blocks of text, dropping out of
    PHP parsing mode is generally more efficient than sending all of
    the text through <function>echo</function> or
    <function>print</function> or somesuch.
   </para>
  </sect1>
  
  <sect1 id="language.basic-syntax.instruction-separation">
   <title>Instruction separation</title>
   
   <simpara>
    Instructions are separated the same as in C or Perl - terminate
    each statement with a semicolon.</simpara>

   <para>
    The closing tag (?&gt;) also implies the end of the statement, so
    the following are equivalent:

    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
    echo "This is a test";
?>

<?php echo "This is a test" ?>
]]>
     </programlisting>
    </informalexample>
   </para>
  </sect1>

  <sect1 id="language.basic-syntax.comments">
   <title>Comments</title>
   
   <para>
    PHP supports 'C', 'C++' and Unix shell-style comments. For example:

    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
    echo "This is a test"; // This is a one-line c++ style comment
    /* This is a multi line comment
       yet another line of comment */
    echo "This is yet another test";
    echo "One Final Test"; # This is shell-style style comment
?>
]]>
     </programlisting>
    </informalexample>
   </para>

   <simpara>
    The "one-line" comment styles actually only comment to the end of
    the line or the current block of PHP code, whichever comes
    first.
   </simpara>
   <informalexample>
    <programlisting role="php">
<![CDATA[
<h1>This is an <?php # echo "simple";?> example.</h1>
<p>The header above will say 'This is an example'.
]]>
    </programlisting>
   </informalexample> 

   <simpara>
    You should be careful not to nest 'C' style comments, which can
    happen when commenting out large blocks.
   </simpara>

   <informalexample>
    <programlisting role="php">
<![CDATA[
<?php
 /* 
    echo "This is a test"; /* This comment will cause a problem */
 */
?>
]]>
    </programlisting>
   </informalexample>
  </sect1>
 </chapter>
 
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->