chapters/tutorial.xml
46ffb4b3fa11ea5733de780a8bcc211d8dc3d92b
...
...
@@ -69,13 +69,14 @@
69
69
<info><title>Our first PHP script: <filename>hello.php</filename></title></info>
70
70
<programlisting role="php">
71
71
<![CDATA[
72
+
<!DOCTYPE html>
72
73
<html>
73
-
<head>
74
-
<title>PHP Test</title>
75
-
</head>
76
-
<body>
77
-
<?php echo '<p>Hello World</p>'; ?>
78
-
</body>
74
+
<head>
75
+
<title>PHP Test</title>
76
+
</head>
77
+
<body>
78
+
<?php echo '<p>Hello World</p>'; ?>
79
+
</body>
79
80
</html>
80
81
]]>
81
82
</programlisting>
...
...
@@ -90,13 +91,14 @@
90
91
</simpara>
91
92
<screen role="html">
92
93
<![CDATA[
94
+
<!DOCTYPE html>
93
95
<html>
94
-
<head>
95
-
<title>PHP Test</title>
96
-
</head>
97
-
<body>
98
-
<p>Hello World</p>
99
-
</body>
96
+
<head>
97
+
<title>PHP Test</title>
98
+
</head>
99
+
<body>
100
+
<p>Hello World</p>
101
+
</body>
100
102
</html>
101
103
]]>
102
104
</screen>
...
...
@@ -174,20 +176,6 @@
174
176
</para>
175
177
</note>
176
178
177
-
<note>
178
-
<info><title>A Note on Windows Notepad</title></info>
179
-
<para>
180
-
If you are writing your PHP scripts using Windows Notepad, you will need
181
-
to ensure that your files are saved with the <filename>.php</filename> extension.
182
-
(Notepad adds a <filename>.txt</filename> extension to files automatically unless
183
-
you take one of the following steps to prevent it.) When you save the file and
184
-
are prompted to provide a name for the file, place the filename in quotes
185
-
(i.e. "<filename>hello.php</filename>"). Alternatively, you can click on the
186
-
'Text Documents' drop-down menu in the 'Save' dialog box and change the setting
187
-
to "All Files". You can then enter your filename without quotes.
188
-
</para>
189
-
</note>
190
-
191
179
<para>
192
180
Now that you have successfully created a working PHP script, it is
193
181
time to create the most famous PHP script! Make a call to the
...
...
@@ -226,12 +214,7 @@
226
214
special reserved PHP variable that contains all web server information.
227
215
It is known as a superglobal. See the related manual page on
228
216
<link linkend="language.variables.superglobals">superglobals</link>
229
-
for more information. These special variables were introduced in PHP
230
-
<link xlink:href="&url.php.release4.1.0;">4.1.0</link>. Before this time, we used
231
-
the older <varname>$HTTP_*_VARS</varname> arrays instead,
232
-
such as <varname>$HTTP_SERVER_VARS</varname>. As of PHP 5.4.0
233
-
these older variables have been removed. (See also the note on
234
-
<link linkend="tutorial.oldcode">old code</link>.)
217
+
for more information.
235
218
</para>
236
219
</note>
237
220
<para>
...
...
@@ -251,14 +234,16 @@ echo $_SERVER['HTTP_USER_AGENT'];
251
234
A sample output of this script may be:
252
235
</para>
253
236
<screen role="html">
254
-
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
237
+
<![CDATA[
238
+
Mozilla/5.0 (Linux) Firefox/112.0
239
+
]]>
255
240
</screen>
256
241
</example>
257
242
</para>
258
243
<para>
259
244
There are many <link linkend="language.types">types</link> of
260
-
variables available in PHP. In the above example we printed
261
-
an <link linkend="language.types.array">Array</link> element.
245
+
variables available in PHP. In the above example we printed an element
246
+
from an <link linkend="language.types.array">Array</link> variable.
262
247
Arrays can be very useful.
263
248
</para>
264
249
<para>
...
...
@@ -272,7 +257,7 @@ Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
272
257
<para>
273
258
You can put multiple PHP statements inside a PHP tag and create
274
259
little blocks of code that do more than just a single echo.
275
-
For example, if you want to check for Internet Explorer you
260
+
For example, if you want to check for Firefox you
276
261
can do this:
277
262
</para>
278
263
<para>
...
...
@@ -282,8 +267,8 @@ Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
282
267
<programlisting role="php">
283
268
<![CDATA[
284
269
<?php
285
-
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
286
-
echo 'You are using Internet Explorer.<br />';
270
+
if (str_contains($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
271
+
echo 'You are using Firefox.';
287
272
}
288
273
?>
289
274
]]>
...
...
@@ -293,7 +278,7 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
293
278
</para>
294
279
<screen role="html">
295
280
<![CDATA[
296
-
You are using Internet Explorer.<br />
281
+
You are using Firefox.
297
282
]]>
298
283
</screen>
299
284
</example>
...
...
@@ -308,14 +293,13 @@ You are using Internet Explorer.<br />
308
293
Reference</link> part of the manual.
309
294
</para>
310
295
<para>
311
-
The second concept we introduced was the <function>strpos</function>
312
-
function call. <function>strpos</function> is a function built into
313
-
PHP which searches a string for another string. In this case we are
314
-
looking for <literal>'MSIE'</literal> (so-called needle) inside
296
+
The second concept we introduced was the <function>str_contains</function>
297
+
function call. <function>str_contains</function> is a function built into
298
+
PHP which determines if a given string contains another string. In this case we are
299
+
looking for <literal>'Firefox'</literal> (so-called needle) inside
315
300
<varname>$_SERVER['HTTP_USER_AGENT']</varname> (so-called haystack). If
316
-
the needle is found inside the haystack, the function returns the position
317
-
of the needle relative to the start of the haystack. Otherwise, it
318
-
returns &false;. If it does not return &false;, the <link
301
+
the needle is found inside the haystack, the function returns true. Otherwise, it
302
+
returns &false;. If it returns &true;, the <link
319
303
linkend="control-structures.if">if</link> expression evaluates to &true;
320
304
and the code within its {braces} is executed. Otherwise, the code is not
321
305
run. Feel free to create similar examples,
...
...
@@ -338,15 +322,15 @@ You are using Internet Explorer.<br />
338
322
<programlisting role="php">
339
323
<![CDATA[
340
324
<?php
341
-
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
325
+
if (str_contains($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
342
326
?>
343
-
<h3>strpos() must have returned non-false</h3>
344
-
<p>You are using Internet Explorer</p>
327
+
<h3>str_contains() returned true</h3>
328
+
<p>You are using Firefox</p>
345
329
<?php
346
330
} else {
347
331
?>
348
-
<h3>strpos() must have returned false</h3>
349
-
<p>You are not using Internet Explorer</p>
332
+
<h3>str_contains() returned false</h3>
333
+
<p>You are not using Firefox</p>
350
334
<?php
351
335
}
352
336
?>
...
...
@@ -357,8 +341,8 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
357
341
</para>
358
342
<screen role="html">
359
343
<![CDATA[
360
-
<h3>strpos() must have returned non-false</h3>
361
-
<p>You are using Internet Explorer</p>
344
+
<h3>str_contains() returned true</h3>
345
+
<p>You are using Firefox</p>
362
346
]]>
363
347
</screen>
364
348
</example>
...
...
@@ -368,8 +352,8 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
368
352
of PHP mode and just sent straight HTML. The important and powerful point
369
353
to note here is that the logical flow of the script remains intact. Only
370
354
one of the HTML blocks will end up getting sent to the viewer depending on
371
-
the result of <function>strpos</function>. In other words, it depends on
372
-
whether the string <literal>MSIE</literal> was found or not.
355
+
the result of <function>str_contains</function>. In other words, it depends on
356
+
whether the string <literal>Firefox</literal> was found or not.
373
357
</para>
374
358
</section>
375
359
...
...
@@ -390,9 +374,13 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
390
374
<programlisting role="html">
391
375
<![CDATA[
392
376
<form action="action.php" method="post">
393
-
<p>Your name: <input type="text" name="name" /></p>
394
-
<p>Your age: <input type="text" name="age" /></p>
395
-
<p><input type="submit" /></p>
377
+
<label for="name">Your name:</label>
378
+
<input name="name" id="name" type="text">
379
+

380
+
<label for="age">Your age:</label>
381
+
<input name="age" id="age" type="number">
382
+

383
+
<button type="submit">Submit</button>
396
384
</form>
397
385
]]>
398
386
</programlisting>
...
...
@@ -454,52 +442,6 @@ Hi Joe. You are 22 years old.
454
442
</para>
455
443
</section>
456
444

457
-
<section xml:id="tutorial.oldcode">
458
-
<info><title>Using old code with new versions of PHP</title></info>
459
-
<para>
460
-
Now that PHP has grown to be a popular scripting language, there are
461
-
a lot of public repositories and libraries containing code you can reuse.
462
-
The PHP developers have largely tried to preserve backwards compatibility,
463
-
so a script written for an older version will run (ideally) without changes
464
-
in a newer version of PHP. In practice, some changes will usually be needed.
465
-
</para>
466
-
<para>
467
-
Two of the most important recent changes that affect old code are:
468
-
<itemizedlist>
469
-
<listitem>
470
-
<simpara>
471
-
The old <varname>$HTTP_*_VARS</varname> arrays are not available as of
472
-
PHP 5.4.0. The following
473
-
<link linkend="language.variables.superglobals">superglobal arrays</link>
474
-
were introduced in PHP <link xlink:href="&url.php.release4.1.0;">4.1.0</link>.
475
-
They are: <varname>$_GET</varname>, <varname>$_POST</varname>,
476
-
<varname>$_COOKIE</varname>, <varname>$_SERVER</varname>,
477
-
<varname>$_FILES</varname>, <varname>$_ENV</varname>,
478
-
<varname>$_REQUEST</varname>, and <varname>$_SESSION</varname>.
479
-
</simpara>
480
-
</listitem>
481
-
<listitem>
482
-
<simpara>
483
-
External variables are no longer registered in the global scope by
484
-
default. In other words, as of PHP
485
-
<link xlink:href="&url.php.release4.2.0;">4.2.0</link> the PHP directive
486
-
<literal>register_globals</literal> is
487
-
<emphasis>off</emphasis> by default in &php.ini;. The preferred
488
-
method of accessing these values is via the superglobal arrays mentioned
489
-
above. Older scripts, books, and tutorials may rely on this
490
-
directive being <literal>on</literal>. If it were <literal>on</literal>,
491
-
for example, one could use <varname>$id</varname> from the URL
492
-
<literal>http://www.example.com/foo.php?id=42</literal>. Whether on
493
-
or off, <varname>$_GET['id']</varname> is available.
494
-
</simpara>
495
-
</listitem>
496
-
</itemizedlist>
497
-
For more details on these changes, see the section on
498
-
<link linkend="language.variables.predefined">predefined variables</link>
499
-
and links therein.
500
-
</para>
501
-
</section>
502
-
503
445
<section xml:id="tutorial.whatsnext">
504
446
<info><title>What's next?</title></info>
505
447
<para>
506
448