some editing for brevity and correct grammar

the strstr-based tutorial was less than optimal: strstr never returns true.
if successful, it returns a substring of the haystack.  I've used strpos
now, because the semantics are easier to explain (although the current
description lacks a discussion of the "!== false" part. maybe someone
wants to add that.).


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@139648 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Sascha Schumann 2003-09-05 06:11:36 +00:00
parent 1cb1e6157b
commit 0014ed5eaa

View file

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.24 $ -->
<!-- $Revision: 1.25 $ -->
<chapter id="tutorial">
<title>A simple tutorial</title>
<para>
Here we would like to show the very basics of PHP in a short simple
Here we would like to show the very basics of PHP in a short, simple
tutorial. This text only deals with dynamic webpage creation with
PHP, though PHP is not only capable of creating webpages. See
the section titled <link linkend="intro-whatcando">What can PHP
@ -19,33 +19,33 @@
<sect1 id="tutorial.requirements">
<title>What do I need?</title>
<para>
In this tutorial we assume that your server has support for PHP
activated and that all files ending in <filename>.php</filename>
are handled by PHP. On most servers this is the default extension
In this tutorial we assume that your server has activated support
for PHP and that all files ending in <filename>.php</filename>
are handled by PHP. On most servers, this is the default extension
for PHP files, but ask your server administrator to be sure. If
your server supports PHP then you don't need to do anything. Just
create your <filename>.php</filename> files and put them in your
web directory and the server will magically parse them for you.
your server supports PHP, then you do not need to do anything. Just
create your <filename>.php</filename> files, put them in your
web directory and the server will automatically parse them for you.
There is no need to compile anything nor do you need to install
any extra tools. Think of these PHP-enabled files as simple HTML
files with a whole new family of magical tags that let you do all
sorts of things. Most web hosts offer PHP support but if your
host doesn't consider reading the <ulink url="&url.php.links;">
sorts of things. Most web hosts offer PHP support, but if your
host does not consider reading the <ulink url="&url.php.links;">
PHP Links</ulink> section for resources on finding PHP enabled
web hosts.
</para>
<para>
Let's say you want to save precious bandwidth and develop locally.
In this case, you'll want to install a web server, such as
Let us say you want to save precious bandwidth and develop locally.
In this case, you will want to install a web server, such as
<link linkend="install.apache">Apache</link>, and of course
<ulink url="&url.php.downloads;">PHP</ulink>. You'll most likely
<ulink url="&url.php.downloads;">PHP</ulink>. You will most likely
want to install a database as well, such as
<ulink url="&url.mysql.docs;">MySQL</ulink>. You can install
these individually or a simpler way is to <ulink
url="&url.installkits;">locate a pre-configured package</ulink>
that automatically installs all of these with just a few mouse
clicks. It's easy to setup a web server with PHP support on
any operating system, including Linux and Windows. In Linux,
<ulink url="&url.mysql.docs;">MySQL</ulink>. You can either install
these individually or choose a simpler way. <ulink
url="&url.installkits;">Locate a pre-configured package</ulink>
which automatically installs all of these with just a few mouse
clicks. It is easy to setup a web server with PHP support on
any operating system, including Linux and Windows. On Linux,
you may find <ulink url="&url.rpmfind;">rpmfind</ulink> and
<ulink url="&url.rpmfind.pbone;">PBone</ulink> helpful for
locating RPMs.
@ -81,10 +81,10 @@
or <literal>http://127.0.0.1/hello.php</literal> but this depends on the
web servers configuration. Although this is outside the scope of this
tutorial, see also the <varname>DocumentRoot</varname> and
<varname>ServerName</varname> directives in your web servers
configuration file. (on Apache this is &httpd.conf;).
If everything is setup correctly, this file will be parsed by PHP and
the following output will make it to your browser:
<varname>ServerName</varname> directives in your web server's
configuration file (for Apache, this is &httpd.conf;).
If everything is configured correctly, this file will be parsed by PHP
and the following output will be sent to your browser:
</simpara>
<screen role="html">
<![CDATA[
@ -107,20 +107,20 @@
that do a lot of interesting things.
</para>
<para>
This program is extremely simple and you really didn't need to use
This program is extremely simple and you really did not need to use
PHP to create a page like this. All it does is display:
<literal>Hello World</literal> using the PHP <function>echo</function>
statement.
</para>
<para>
If you tried this example and it didn't output anything, or it prompted
If you tried this example and it did not output anything, or it prompted
for download, or you see the whole file as text, chances are that the
server you are on does not have PHP enabled. Ask your administrator
to enable it for you using the
<link linkend="installation">Installation</link> chapter
of the manual. If you're developing locally, also read the
of the manual. If you are developing locally, also read the
installation chapter to make sure everything is configured
properly. If problems continue to persist, don't hesitate to use one of
properly. If problems continue to persist, do not hesitate to use one of
the many <ulink url="&url.php.support;">PHP support</ulink> options.
</para>
<para>
@ -148,7 +148,7 @@
<title>A Note on Word Processors</title>
<para>
Word processors such as StarOffice Writer, Microsoft Word and Abiword are
not good choices for editing PHP files. If you wish to use one for this
not optimal for editing PHP files. If you wish to use one for this
test script, you must ensure that you save the file as PLAIN TEXT or PHP
will not be able to read and execute the script.
</para>
@ -169,11 +169,11 @@
</note>
<para>
Now that you've successfully created a simple PHP script that works, it's
Now that you have successfully created a working PHP script, it is
time to create the most famous PHP script! Make a call to the
<function>phpinfo</function> function and you'll see a lot of useful
<function>phpinfo</function> function and you will see a lot of useful
information about your system and setup such as available
<link linkend="language.variables.predefined">Predefined Variables</link>,
<link linkend="language.variables.predefined">predefined variables</link>,
loaded PHP modules, and <link linkend="configuration">configuration</link>
settings. Take some time and review this important information.
</para>
@ -183,10 +183,10 @@
<sect1 id="tutorial.useful">
<title>Something Useful</title>
<para>
Let's do something a bit more useful now. We are going to check
what sort of browser the person viewing the page is using.
In order to do that we check the user agent string that the browser
sends as part of its HTTP request. This information is stored in a <link
Let us do something more useful now. We are going to check
what sort of browser the visitor is using.
For that, we check the user agent string the browser
sends as part of the HTTP request. This information is stored in a <link
linkend="language.variables">variable</link>. Variables always start
with a dollar-sign in PHP. The variable we are interested in right now
is <varname>$_SERVER["HTTP_USER_AGENT"]</varname>.
@ -195,8 +195,8 @@
<para>
<link linkend="reserved.variables.server">$_SERVER</link> is a
special reserved PHP variable that contains all web server information.
It's known as an Autoglobal (or Superglobal). See the related manual page on
<link linkend="language.variables.superglobals">Autoglobals</link>
It is known as an autoglobal (or superglobal). See the related manual page on
<link linkend="language.variables.superglobals">autoglobals</link>
for more information. These special variables were introduced in PHP
<ulink url="&url.php.release4.1.0;">4.1.0</ulink>. Before this time, we used
the older <varname>$HTTP_*_VARS</varname> arrays instead,
@ -206,7 +206,7 @@
</para>
</note>
<para>
To display this variable, we can simply do:
To display this variable, you can simply do:
</para>
<para>
<example>
@ -231,7 +231,7 @@ Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Arrays can be very useful.
</para>
<para>
<varname>$_SERVER</varname> is just one variable that's automatically
<varname>$_SERVER</varname> is just one variable that is automatically
made available to you by PHP. A list can be seen in the
<link linkend="reserved.variables">Reserved Variables</link> section
of the manual or you can get a complete list of them by creating
@ -248,15 +248,15 @@ Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
</example>
</para>
<para>
If you load up this file in your browser you will see a page
When you load up this file in your browser, you will see a page
full of information about PHP along with a list of all the
variables available to you.
</para>
<para>
You can put multiple PHP statements inside a PHP tag and create
little blocks of code that do more than just a single echo.
For example, if we wanted to check for Internet Explorer we
could do something like this:
For example, if you want to check for Internet Explorer you
can do this:
</para>
<para>
<example>
@ -265,7 +265,7 @@ Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
<programlisting role="php">
<![CDATA[
<?php
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE") !== false) {
echo "You are using Internet Explorer<br />";
}
?>
@ -285,28 +285,29 @@ You are using Internet Explorer<br />
Here we introduce a couple of new concepts. We have an
<link linkend="control-structures.if">if</link> statement.
If you are familiar with the basic syntax used by the C
language this should look logical to you. If you don't know enough
C or some other language where the syntax used above is used, you
language, this should look logical to you. Otherwise, you
should probably pick up any introductory PHP book and read the first
couple of chapters, or read the <link linkend="langref">Language
Reference</link> part of the manual. You can find a list of PHP books
at <ulink url="&url.php.books;">&url.php.books;</ulink>.
</para>
<para>
The second concept we introduced was the <function>strstr</function>
function call. <function>strstr</function> is a function built into
The second concept we introduced was the <function>strpos</function>
function call. <function>strpos</function> is a function built into
PHP which searches a string for another string. In this case we are
looking for <literal>"MSIE"</literal> inside
<varname>$_SERVER["HTTP_USER_AGENT"]</varname>. If the string is found,
the function returns &true; and if it isn't, it returns &false;. If
it returns &true;, the <link linkend="control-structures.if">if</link>
statement evaluates to &true; and the code within its {braces} is
executed. Otherwise, it's not. Feel free to create similar examples,
looking for <literal>"MSIE"</literal> (so-called needle) inside
<varname>$_SERVER["HTTP_USER_AGENT"]</varname> (so-called haystack). If
the needle is found inside the haystack, the function returns the position
of the needle relative to the start of the haystack. Otherwise, it
returns &false;. If it does not return &false;, the <link
linkend="control-structures.if">if</link> expression evaluates to &true;
and the code within its {braces} is executed. Otherwise, the code is not
run. Feel free to create similar examples,
with <link linkend="control-structures.if">if</link>,
<link linkend="control-structures.else">else</link>, and other
functions such as <function>strtoupper</function> and
<function>strlen</function>. Each related manual page contains examples
too. If you're unsure how to use functions, you'll want to read both
too. If you are unsure how to use functions, you will want to read both
the manual page on <link linkend="about.prototypes">how to read a
function definition</link> and the section about
<link linkend="functions">PHP functions</link>.
@ -321,14 +322,14 @@ You are using Internet Explorer<br />
<programlisting role="php">
<![CDATA[
<?php
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE") !== false) {
?>
<h3>strstr must have returned true</h3>
<h3>strpos must have returned non-false</h3>
<center><b>You are using Internet Explorer</b></center>
<?php
} else {
?>
<h3>strstr must have returned false</h3>
<h3>strpos must have returned false</h3>
<center><b>You are not using Internet Explorer</b></center>
<?php
}
@ -340,19 +341,19 @@ if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
</para>
<screen role="html">
<![CDATA[
<h3>strstr must have returned true</h3>
<h3>strpos must have returned non-false</h3>
<center><b>You are using Internet Explorer</b></center>
]]>
</screen>
</example>
</para>
<para>
Instead of using a PHP echo statement to output something, we jumped out of PHP
mode and just sent straight HTML. The important and powerful point to note here
is that the logical flow of the script remains intact. Only one of the HTML blocks
will end up getting sent to the viewer depending on if
<function>strstr</function> returned &true; or &false; In other words,
if the string <literal>MSIE</literal> was found or not.
Instead of using a PHP echo statement to output something, we jumped out
of PHP mode and just sent straight HTML. The important and powerful point
to note here is that the logical flow of the script remains intact. Only
one of the HTML blocks will end up getting sent to the viewer depending on
the result of <function>strpos</function>. In other words, it depends on
whether the string <literal>MSIE</literal> was found or not.
</para>
</sect1>
@ -365,7 +366,7 @@ if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
scripts. Please read the manual section on
<link linkend="language.variables.external">Variables from outside
of PHP</link> for more information and examples on using forms
with PHP. Here's an example HTML form:
with PHP. Here is an example HTML form:
</para>
<para>
<example>
@ -418,8 +419,8 @@ You are 22 years old.
method <emphasis>GET</emphasis> then our form information would live in
the <link linkend="reserved.variables.get">$_GET</link> autoglobal instead.
You may also use the <link linkend="reserved.variables.request">$_REQUEST</link>
autoglobal if you don't care the source of your request data. It
contains a mix of GET, POST and COOKIE data. See also the
autoglobal, if you do not care about the source of your request data. It
contains the merged information of GET, POST and COOKIE data. Also see the
<function>import_request_variables</function> function.
</para>
</sect1>
@ -428,11 +429,10 @@ You are 22 years old.
<title>Using old code with new versions of PHP</title>
<para>
Now that PHP has grown to be a popular scripting language, there are
more resources out there that have listings of code you can reuse
in your own scripts. For the most part the developers of the PHP
language have tried to be backwards compatible, so a script written
for an older version should run (ideally) without changes in a newer
version of PHP, in practice some changes will usually be needed.
a lot of public repositories/libraries containing code you can reuse.
The PHP developers have largely tried to preserve backwards compatibility,
so a script written for an older version will run (ideally) without changes
in a newer version of PHP. In practice, some changes will usually be needed.
</para>
<para>
Two of the most important recent changes that affect old code are:
@ -479,7 +479,7 @@ You are 22 years old.
<sect1 id="tutorial.whatsnext">
<title>What's next?</title>
<para>
With what you know now you should be able to understand most of
With your new knowledge you should be able to understand most of
the manual and also the various example scripts available in the
example archives. You can also find other examples on the php.net
websites in the links section: