cvs: phpdoc-ru /chapters tutorial.xml

From: Date: Tue, 21 Jan 2003 09:52:52 +0000
Subject: cvs: phpdoc-ru /chapters tutorial.xml
Groups: php.doc.ru 
Request: Send a blank email to doc-ru+get-74@lists.php.net to get a copy of this message
alexws Tue Jan 21 04:52:52 2003 EDT Added files: /phpdoc-ru/chapters tutorial.xml Log: Translated part of it... in progress.

Index: phpdoc-ru/chapters/tutorial.xml +++ phpdoc-ru/chapters/tutorial.xml <?xml version="1.0" encoding="windows-1251"?> <!-- EN-Revision: 1.10 Maintainer: alexws Status: in-progress --> <!-- $Revision: 1.1 $ --> <chapter id="tutorial"> <title>Êðàòêîå ðóêîâîäñòâî</title> <para>  äàííîì êðàòêîì ðóêîâîäñòâå íà ïðèìåðàõ ïîêàçûâàþòñÿ îñíîâû PHP. Ýòî ðóêîâîäñòâî âêëþ÷àåò â ñåáÿ òîëüêî ñîçäàíèå äèíàìè÷åñêèõ Web-ñòðàíèö ñ ïîìîùüþ PHP, îäíàêî ðåàëüíàÿ îáëàñòü ïðèìåíåíèÿ PHP ãîðàçäî øèðå.  ðàçäåëå "<link linkend="intro-whatcando">×òî ìîæåò PHP</link>" ïðèâåäåíà äîïîëíèòåëüíàÿ èíôîðìàöèÿ. </para> <para> Ñîçäàííûå ñ èñïîëüçîâàíèåì PHP Web-ñòðàíèöû îáðàáàòûâàþòñÿ, êàê îáû÷íûå HTML-ñòðàíèöàìè. Èõ ìîæíî ñîçäàâàòü è èçìåíÿòü òî÷íî òàêèì æå îáðàçîì, êàê è îáû÷íûå ñòðàíèöû íà HTML. </para> <sect1 id="tutorial.requirements"> <title>×òî ïîòðåáóåòñÿ?</title> <para>  äàííîì ðóêîâîäñòâå ìû ïðåäïîëàãàåì, ÷òî âàø ñåðâåð èìååò ïîääåðæêó PHP è ÷òî âñå ôàéëû, çàêàí÷èâàþùèåñÿ íà <filename>.php</filename>, îáðàáàòûâàþòñÿ PHP.  áîëüøèíñòâå ñåðâåðîâ ýòî ðàñøèðåíèå èñïîëüçóåòñÿ äëÿ PHP ïî óìîë÷àíèþ, íî âñå-òàêè íå ëèøíèì áóäåò óòî÷íèòü ýòî ó âàøåãî àäìèíèñòðàòîðà ñåðâåðà. Åñëè âàø ñåðâåð ïîääåðæèâàåò PHP, òî ó âàñ åñòü âñå, ÷òî òðåáóåòñÿ. Ïðîñòî ñîçäàâàéòå âàøè ôàéëû <filename>.php</filename> è ðàçìåùàéòå èõ â âàøåì êàòàëîãå Web-ñåðâåðà - îíè áóäóò îáðàáàòûâàòüñÿ àâòîìàòè÷åñêè. Íå íóæíî íè÷åãî êîìïèëèðîâàòü, íå íóæíî íèêàêèõ äîïîëíèòåëüíûõ ïðîãðàìì. Ñ÷èòàéòå ôàéëû PHP îáû÷íûìè ôàéëàìè HTML ñ íàáîðîì íîâûõ "âîëøåáíûõ" òåãîâ, êîòîðûå ïîçâîëÿþò âàì äåëàòü âñå, ÷òî óãîäíî. </para> </sect1> <sect1 id="tutorial.firstpage"> <title>Ïåðâàÿ ñòðàíèöà íà PHP</title> <para> Ñîçäàéòå ôàéë ñ èìåíåì <filename>hello.php</filename> â êîðíåâîì êàòàëîãå âàøèõ äîêóìåíòîâ Web-ñåðâåðà è çàïèøèòå â íåãî ñëåäóþùåå: </para> <para> <example> <title>Ïåðâûé ñêðèïò íà PHP: <filename>hello.php</filename></title> <programlisting role="php"> <![CDATA[ <html> <head> <title>Òåñòèðóåì PHP</title> </head> <body> <?php echo "Ïðèâåò!<p>"; ?> </body> </html> ]]> </programlisting> <para> Ýòà ïðîãðàììà âûâåäåò ñëåäóþùåå: <screen role="html"> <![CDATA[ <html> <head> <title>Òåñòèðóåì PHP</title> </head> <body> Ïðèâåò!<p> </body> </html> ]]> </screen> </para> </example> </para> <para> Çàìåòèì, ÷òî ñõîäñòâà ñî ñêðèïòàìè íà CGI íåò. Ôàéë íå îáÿçàí áûòü âûïîëíèìûì èëè îòìå÷åííûì ëþáûì äðóãèì îáðàçîì. Ýòî ïðîñòî îáû÷íûé ôàéë HTML, â êîòîðîì åñòü íàáîð ñïåöèàëüíûõ òåãîâ, äåëàþùèõ ìíîãî èíòåðåñíîãî. </para> <para> Ýòà ïðîãðàììà ÷ðåçâû÷àéíî ïðîñòà, è äëÿ ñîçäàíèÿ íàñòîëüêî ïðîñòîé ñòðàíè÷êè äàæå íåîáÿçàòåëüíî èñïîëüçîâàòü PHP. Âñå ÷òî îíà äåëàåò - ýòî âûâîäèò "<literal>Ïðèâåò!</literal> ñ èñïîëüçîâàíèåì ôóíêöèè PHP <function>echo</function>. </para> <para> Åñëè ó âàñ ýòîò ïðèìåð íå îòîáðàæàåò íè÷åãî èëè âûâîäèò îêíî çàãðóçêè, èëè åñëè âû âèäèòå âåñü ýòîò ôàéë â òåêñòîâîì âèäå, òî âåñüìà âåðîÿòíî, ÷òî âàø Web-ñåðâåð íå èìååò ïîääåðæêè PHP. Ïîïðîñèòå âàøåãî àäìèíèñòðàòîðà ñåðâåðà âêëþ÷èòü òàêóþ ïîääåðæêó. Ïðåäëîæèòå åìó èíñòðóêöèþ ïî óñòàíîâêå - ðàçäåë "<link linkend="installation">Óñòàíîâêà</link>" äàííîé äîêóìåíòàöèè. Åñëè æå âû õîòèòå ðàçðàáàòûâàòü ñêðèïòû íà PHP äîìà, òî âàì â ðàçäåë <ulink url="&url.php.downloads;">íåîáõîäèìûå ôàéëû</ulink>. Äîìà ìîæíî ðàçðàáàòûâàòü ñêðèïòû ñ èñïîëüçîâàíèåì ëþáîé îïåðàöèîííîé ñèñòåìû, íî âàì ïîíàäîáèòñÿ óñòàíîâèòü ñîîòâåòñòâóþùèé Web-ñåðâåð. </para> <para> The point of the example is to show the special PHP tag format. In this example we used <literal>&lt;?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>?&gt;</literal>. You may jump in and out of PHP mode in an HTML file like this all you want. </para> <note> <title>A Note on Text Editors</title> <para> There are many text editors and Integrated Development Environments (IDEs) that you can use to create, edit and manage PHP files. A partial list of these tools is maintained at <ulink url="&url.phpeditorlist;">PHP Editor's List</ulink>. If you wish to recommend an editor, please visit the above page and ask the page maintainer to add the editor to the list. </para> </note> <note> <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. </para> <para> 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> </note> <note> <title>A Note on Windows Notepad</title> <para> If you are writing your PHP scripts using Windows Notepad, you will need to ensure that your files are saved with the .php extension. (Notepad adds a .txt extension to files automatically unless you take one of the following steps to prevent it.) </para> <para> When you save the file and are prompted to provide a name for the file, place the filename in quotes (i.e. "hello.php"). </para> <para> Alternately, you can click on the 'Text Documents' drop-down menu in the save dialog box and change the setting to "All Files". You can then enter your filename without quotes. </para> </note> </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 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>. </para> <note> <title>PHP Autoglobals Note</title> <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> 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, such as <varname>$HTTP_SERVER_VARS</varname>. Although deprecated, these older variables still exist. (See also the note on <link linkend="tutorial.oldcode">old code</link>.) </para> </note> <para> To display this variable, we can simply do: </para> <para> <example> <title>Printing a variable (Array element)</title> <programlisting role="php"> <![CDATA[ <?php echo $_SERVER["HTTP_USER_AGENT"]; ?> ]]> </programlisting> <para> A sample output of this script may be: <screen role="html"> Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) </screen> </para> </example> </para> <para> There are many <link linkend="language.types">types</link> of variables available in PHP. In the above example we printed an <link linkend="language.types.array">Array</link> element. Arrays can be very useful. </para> <para> <varname>$_SERVER</varname> is just one variable that's 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 a file that looks like this: </para> <para> <example> <title>Show all predefined variables with <function>phpinfo</function></title> <programlisting role="php"> <![CDATA[ <?php phpinfo(); ?> ]]> </programlisting> </example> </para> <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> <para> <example> <title>Example using <link linkend="control-structures">control structures</link> and <link linkend="functions">functions</link></title> <programlisting role="php"> <![CDATA[ <?php if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { echo "You are using Internet Explorer<br />"; } ?> ]]> </programlisting> <para> A sample output of this script may be: <screen role="html"> <![CDATA[ You are using Internet Explorer<br /> ]]> </screen> </para> </example> </para> <para> 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 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 <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, 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. </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> <para> <example> <title>Mixing both HTML and PHP modes</title> <programlisting role="php"> <![CDATA[ <?php if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { ?> <h3>strstr must have returned true</h3> <center><b>You are using Internet Explorer</b></center> <?php } else { ?> <h3>strstr must have returned false</h3> <center><b>You are not using Internet Explorer</b></center> <?php } ?> ]]> </programlisting> <para> A sample output of this script may be: <screen role="html"> <![CDATA[ <h3>strstr must have returned true</h3> <center><b>You are using Internet Explorer</b></center> ]]> </screen> </para> </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. </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 be available to your PHP 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: </para> <para> <example> <title>A simple HTML form</title> <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> </example> </para> <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> <para> <example> <title>Printing data from our form</title> <programlisting role="php"> <![CDATA[ Hi <?php echo $_POST["name"]; ?>. You are <?php echo $_POST["age"]; ?> years old. ]]> </programlisting> <para> A sample output of this script may be: <screen role="html"> <![CDATA[ Hi Joe. You are 22 years old. ]]> </screen> </para> </example> </para> <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. Earlier we used the <varname>$_SERVER</varname> autoglobal, now above we just introduced the <link linkend="reserved.variables.post">$_POST</link> autoglobal which contains all POST data. Notice how the <emphasis>method</emphasis> of our form is POST. If we used the 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, COOKIE and FILE data. See also the <function>import_request_variables</function> function. </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). The following <link linkend="language.variables.superglobals">autoglobal arrays</link> were introduced in PHP <ulink url="&url.php.release4.1.0;">4.1.0</ulink>. They are: <varname>$_GET</varname>, <varname>$_POST</varname>, <varname>$_COOKIE</varname>, <varname>$_SERVER</varname>, <varname>$_ENV</varname>, <varname>$_REQUEST</varname>, and <varname>$_SESSION</varname>. The older <varname>$HTTP_*_VARS</varname> arrays, such as $HTTP_POST_VARS, still exist and have since PHP 3. </simpara> </listitem> <listitem> <simpara> External variables are no longer registered in the global scope by default. In other words, as of PHP <ulink url="&url.php.release4.2.0;">4.2.0</ulink> the PHP directive <link linkend="ini.register-globals">register_globals</link> is <emphasis>off</emphasis> by default in &php.ini;. The preferred method of accessing these values is via the autoglobal arrays mentioned above. Older scripts, books, and tutorials may rely on this directive being on. If on, for example, one could use <varname>$id</varname> from the URL <literal>http://www.example.com/foo.php?id=42</literal>. Whether on or off, <varname>$_GET['id']</varname> is available. </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 -->
« previous php.doc.ru (#74) next »