chapters/tutorial.xml
1fd637525fd3bbaec04f6fff80eeb33fce880b10
1fd637525fd3bbaec04f6fff80eeb33fce880b10
...
...
@@ -20,11 +20,11 @@
20
20
<info><title>What do I need?</title></info>
21
21
<para>
22
22
In this tutorial we assume that your server has activated support
23
-
for PHP and that all files ending in <filename>.php</filename>
23
+
for PHP and that all files ending in <filename class="extension">.php</filename>
24
24
are handled by PHP. On most servers, this is the default extension
25
25
for PHP files, but ask your server administrator to be sure. If
26
26
your server supports PHP, then you do not need to do anything. Just
27
-
create your <filename>.php</filename> files, put them in your
27
+
create your <filename class="extension">.php</filename> files, put them in your
28
28
web directory and the server will automatically parse them for you.
29
29
There is no need to compile anything nor do you need to install
30
30
any extra tools. Think of these PHP-enabled files as simple HTML
...
...
@@ -69,14 +69,11 @@
69
69
<info><title>Our first PHP script: <filename>hello.php</filename></title></info>
70
70
<programlisting role="php">
71
71
<![CDATA[
72
-
<html>
73
-
<head>
74
-
<title>PHP Test</title>
75
-
</head>
76
-
<body>
77
-
<?php echo '<p>Hello World</p>'; ?>
78
-
</body>
79
-
</html>
72
+
<?php
73
+
74
+
echo "Hello World!";
75
+
76
+
?>
80
77
]]>
81
78
</programlisting>
82
79
<simpara>
...
...
@@ -85,18 +82,39 @@
85
82
URL will be something like <literal>http://localhost/hello.php</literal>
86
83
or <literal>http://127.0.0.1/hello.php</literal> but this depends on the
87
84
web server's configuration. If everything is configured correctly, this
88
-
file will be parsed by PHP and the following output will be sent to
89
-
your browser:
85
+
file will be parsed by PHP and you will see the "Hello World" output displayed
86
+
in your browser.
87
+
</simpara>
88
+
<simpara>
89
+
PHP can be embedded within a normal HTML web page. That means inside your HTML document
90
+
you can write the PHP statements, as demonstrated in the following example:
91
+
</simpara>
92
+
<programlisting role="php">
93
+
<![CDATA[
94
+
<!DOCTYPE html>
95
+
<html>
96
+
<head>
97
+
<title>PHP Test</title>
98
+
</head>
99
+
<body>
100
+
<?php echo '<p>Hello World</p>'; ?>
101
+
</body>
102
+
</html>
103
+
]]>
104
+
</programlisting>
105
+
<simpara>
106
+
This will result in the following output:
90
107
</simpara>
91
108
<screen role="html">
92
109
<![CDATA[
110
+
<!DOCTYPE html>
93
111
<html>
94
-
<head>
95
-
<title>PHP Test</title>
96
-
</head>
97
-
<body>
98
-
<p>Hello World</p>
99
-
</body>
112
+
<head>
113
+
<title>PHP Test</title>
114
+
</head>
115
+
<body>
116
+
<p>Hello World</p>
117
+
</body>
100
118
</html>
101
119
]]>
102
120
</screen>
...
...
@@ -174,20 +192,6 @@
174
192
</para>
175
193
</note>
176
194
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
195
<para>
192
196
Now that you have successfully created a working PHP script, it is
193
197
time to create the most famous PHP script! Make a call to the
...
...
@@ -226,12 +230,7 @@
226
230
special reserved PHP variable that contains all web server information.
227
231
It is known as a superglobal. See the related manual page on
228
232
<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>.)
233
+
for more information.
235
234
</para>
236
235
</note>
237
236
<para>
...
...
@@ -251,14 +250,16 @@ echo $_SERVER['HTTP_USER_AGENT'];
251
250
A sample output of this script may be:
252
251
</para>
253
252
<screen role="html">
254
-
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
253
+
<![CDATA[
254
+
Mozilla/5.0 (Linux) Firefox/112.0
255
+
]]>
255
256
</screen>
256
257
</example>
257
258
</para>
258
259
<para>
259
260
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.
261
+
variables available in PHP. In the above example we printed an element
262
+
from an <link linkend="language.types.array">Array</link> variable.
262
263
Arrays can be very useful.
263
264
</para>
264
265
<para>
...
...
@@ -272,7 +273,7 @@ Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
272
273
<para>
273
274
You can put multiple PHP statements inside a PHP tag and create
274
275
little blocks of code that do more than just a single echo.
275
-
For example, if you want to check for Internet Explorer you
276
+
For example, if you want to check for Firefox you
276
277
can do this:
277
278
</para>
278
279
<para>
...
...
@@ -282,8 +283,8 @@ Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
282
283
<programlisting role="php">
283
284
<![CDATA[
284
285
<?php
285
-
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
286
-
echo 'You are using Internet Explorer.<br />';
286
+
if (str_contains($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
287
+
echo 'You are using Firefox.';
287
288
}
288
289
?>
289
290
]]>
...
...
@@ -293,7 +294,7 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
293
294
</para>
294
295
<screen role="html">
295
296
<![CDATA[
296
-
You are using Internet Explorer.<br />
297
+
You are using Firefox.
297
298
]]>
298
299
</screen>
299
300
</example>
...
...
@@ -308,14 +309,13 @@ You are using Internet Explorer.<br />
308
309
Reference</link> part of the manual.
309
310
</para>
310
311
<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
312
+
The second concept we introduced was the <function>str_contains</function>
313
+
function call. <function>str_contains</function> is a function built into
314
+
PHP which determines if a given string contains another string. In this case we are
315
+
looking for <literal>'Firefox'</literal> (so-called needle) inside
315
316
<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
317
+
the needle is found inside the haystack, the function returns true. Otherwise, it
318
+
returns &false;. If it returns &true;, the <link
319
319
linkend="control-structures.if">if</link> expression evaluates to &true;
320
320
and the code within its {braces} is executed. Otherwise, the code is not
321
321
run. Feel free to create similar examples,
...
...
@@ -338,15 +338,15 @@ You are using Internet Explorer.<br />
338
338
<programlisting role="php">
339
339
<![CDATA[
340
340
<?php
341
-
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
341
+
if (str_contains($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
342
342
?>
343
-
<h3>strpos() must have returned non-false</h3>
344
-
<p>You are using Internet Explorer</p>
343
+
<h3>str_contains() returned true</h3>
344
+
<p>You are using Firefox</p>
345
345
<?php
346
346
} else {
347
347
?>
348
-
<h3>strpos() must have returned false</h3>
349
-
<p>You are not using Internet Explorer</p>
348
+
<h3>str_contains() returned false</h3>
349
+
<p>You are not using Firefox</p>
350
350
<?php
351
351
}
352
352
?>
...
...
@@ -357,8 +357,8 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
357
357
</para>
358
358
<screen role="html">
359
359
<![CDATA[
360
-
<h3>strpos() must have returned non-false</h3>
361
-
<p>You are using Internet Explorer</p>
360
+
<h3>str_contains() returned true</h3>
361
+
<p>You are using Firefox</p>
362
362
]]>
363
363
</screen>
364
364
</example>
...
...
@@ -368,8 +368,8 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
368
368
of PHP mode and just sent straight HTML. The important and powerful point
369
369
to note here is that the logical flow of the script remains intact. Only
370
370
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.
371
+
the result of <function>str_contains</function>. In other words, it depends on
372
+
whether the string <literal>Firefox</literal> was found or not.
373
373
</para>
374
374
</section>
375
375
...
...
@@ -390,9 +390,13 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
390
390
<programlisting role="html">
391
391
<![CDATA[
392
392
<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>
393
+
<label for="name">Your name:</label>
394
+
<input name="name" id="name" type="text">
395
+
396
+
<label for="age">Your age:</label>
397
+
<input name="age" id="age" type="number">
398
+
399
+
<button type="submit">Submit</button>
396
400
</form>
397
401
]]>
398
402
</programlisting>
...
...
@@ -410,7 +414,7 @@ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) {
410
414
<programlisting role="php">
411
415
<![CDATA[
412
416
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
413
-
You are <?php echo (int)$_POST['age']; ?> years old.
417
+
You are <?php echo (int) $_POST['age']; ?> years old.
414
418
]]>
415
419
</programlisting>
416
420
<para>
...
...
@@ -445,59 +449,6 @@ Hi Joe. You are 22 years old.
445
449
superglobal, if you do not care about the source of your request data. It
446
450
contains the merged information of GET, POST and COOKIE data.
447
451
</para>
448
-
<para>
449
-
You can also deal with XForms input in PHP, although you will find yourself
450
-
comfortable with the well supported HTML forms for quite some time.
451
-
While working with XForms is not for beginners, you might be interested
452
-
in them. We also have a <link linkend="features.xforms">short introduction
453
-
to handling data received from XForms</link> in our features section.
454
-
</para>
455
-
</section>
456
-
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
452
</section>
502
453
503
454
<section xml:id="tutorial.whatsnext">
504
455