goba Tue Jul 16 09:09:25 2002 EDT
Added files:
/phpdoc/en/chapters tutorial.xml
Log:
Adding tut.php as a DocBook document, as this should reside here.
This enables translation, and php.net/tut to jump to the translated
one. This way, this page also jumps up in searches, and manual people
can update it, as it should be. php.net/tut.php will be deleted as
this is generated to be online...
Index: phpdoc/en/chapters/tutorial.xml
+++ phpdoc/en/chapters/tutorial.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<chapter id="tutorial">
<title>A simple tutorial</title>
<para>
Here we would like to show the very basics of PHP in a short simple
tutorial. This text only deals with dinamic webpage creation with
PHP, though PHP is not only capable of creating webpages. See
the section titled <link linkend="intro-whatcando">What can PHP
do</lihnk> for more information.
</para>
<para>
PHP-enabled web pages are treated just like regular HTML pages and
you can create and edit them the same way you normally create
regular HTML pages.
</para>
<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
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.
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.
</para>
</sect1>
<sect1 id="tutorial.firstpage">
<title>Your first PHP-enabled page</title>
<para>
Create a file named <filename>hello.php</filename> under your
webserver root directory with the following content:
</para>
<para>
<example>
<title>hello.php</title>
<programlisting role="php">
<![CDATA[
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo "Hello World<p>"; ?>
</body>
</html>
]]>
</programlisting>
</example>
</para>
<para>
Note that this is not like a CGI script. The file does not need to be
executable or special in any way. Think of it as a normal HTML
file which happens to have a set of special tags available to you
that do a lot of interesting things.
</para>
<para>
This program is extremely simple and you really didn't need to use
PHP to create a page like this. All it does is display:
<literal>Hello World</literal> using the <function>echo</function>
statement.
</para>
<para>
If you tried this example and it didn't output anything 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.
</para>
<para>
The point of the example is to show the special PHP tag format.
In this example we used <literal><?php</literal> to indicate the
start of a PHP tag. Then we put the PHP statement and left PHP mode by
adding the closing tag, <literal>?></literal>. You may jump in
and out of PHP mode in an HTML file like this all you want.
</para>
</sect1>
<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 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 is
<varname>$_SERVER["HTTP_USER_AGENT"]</varname>. To display this
variable we can simply do:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php echo $_SERVER["HTTP_USER_AGENT"]; ?>
]]>
</programlisting>
</informalexample>
<para>
There are many other variables that are automatically set by
for your by PHP. You can get a complete list of them by creating
a file that looks like this:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php phpinfo(); ?>
]]>
</programlisting>
</informalexample>
<para>
If 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:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
echo "You are using Internet Explorer<br />";
}
?>
]]>
</programlisting>
</informalexample>
<para>
Here we introduce a couple of new concepts. We have an
<link linkend="control-structures.if">if</if> 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
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
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 following statement is executed.
</para>
<para>
We can take this a step further and show how you can jump in and out
of PHP mode even in the middle of a PHP block:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
?>
<center><b>You are using Internet Explorer</b></center>
<?php
} else {
?>
<center><b>You are not using Internet Explorer</b></center>
<?php
}
?>
]]>
</programlisting>
</informalexample>
<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 remain intact. Only one of the HTML blocks
will end up getting sent to the viewer.
</para>
</sect1>
<sect1 id="tutorial.forms">
<title>Dealing with Forms</title>
<para>
One of the most powerful features of PHP is the way it handles HTML
forms. The basic concept that is important to understand is that any
form element in a form will automatically result in a variable
with the same name as the element being created on the target page.
This probably sounds confusing, so here is a simple example.
Assume you have a page with a form like this on it:
</para>
<informalexample>
<programlisting role="html">
<![CDATA[
<form action="action.php" method="POST">
Your name: <input type="text" name="name" />
Your age: <input type="text" name="age" />
<input type="submit">
</form>
]]>
</programlisting>
</informalexample>
<para>
There is nothing special about this form. It is a straight HTML form
with no special tags of any kind. When the user fills in this form
and hits the submit button, the <filename>action.php</filename> page
is called. In this file you would have something like this:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
Hi <?php echo $_POST["name"]; ?>.
You are <?php echo $_POST["age"]; ?> years old.
]]>
</programlisting>
</informalexample>
<para>
It should be obvious what this does. There is nothing more to it.
The <varname>$_POST["name"]</varname> and <varname>$_POST["age"]</varname>
variables are automatically set for you by PHP.
</para>
<para>
<note>
<para>
On versions previous to PHP 4.1.0, one needed to use the
<varname>$HTTP_POST_VARS</varname> array instead of the
<varname>$_POST</varname> superglobal array. See the section
on <link linkend="language.variables.predefined">predefined
variables</link> for more information.
</para>
<note>
</para>
</sect1>
<sect1 id="tutorial.oldcode">
<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.
</para>
<para>
Two of the most important recent changes that affect old code are:
<itemizedlist>
<listitem>
<simpara>
The deprecation of the old <varname>$HTTP_*_VARS</varname> arrays
(which need to be indicated as global when used inside a function or
method), for the superglobal arrays <varname>$_GET</varname>,
<varname>$_POST</varname>, <varname>$_COOKIE</varname>,
<varname>$_SERVER</varname>, <varname>$_ENV</varname>,
<varname>$_REQUEST</varname>, and <varname>$_SESSION</varname>,
which are always accessible even from inside a function
scope. (PHP >= 4.1.0)
</simpara>
</listitem>
<listitem>
<simpara>
External variables are no longer registered in the global scope by
default (in other words, <literal>register_globals=off</literal>
by defaults in php.ini), which means that the preferred method of
accessing those values is via the superglobal arrays mentioned
above. (PHP >= 4.2.0)
</simpara>
</listitem>
</itemizedlist>
For more details on these changes see the section on
<link linkend="language.variables.predefined">predefined variables</link>
and links therein.
</para>
</sect1>
<sect1 id="tutorial.whatsnext">
<title>What's next?</title>
<para>
With what you know now 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:
<ulink url="&url.php.links;">&url.php.links;</ulink>.
</para>
</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
-->
--
PHP Documentation Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php