diff --git a/chapters/tutorial.xml b/chapters/tutorial.xml index 557c23f969..119e907075 100644 --- a/chapters/tutorial.xml +++ b/chapters/tutorial.xml @@ -1,10 +1,10 @@ - + A simple tutorial - 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 What can PHP @@ -19,33 +19,33 @@ What do I need? - In this tutorial we assume that your server has support for PHP - activated and that all files ending in .php - 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 .php + 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 .php 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 .php 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 + sorts of things. Most web hosts offer PHP support, but if your + host does not consider reading the PHP Links section for resources on finding PHP enabled web hosts. - 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 Apache, and of course - PHP. You'll most likely + PHP. You will most likely want to install a database as well, such as - MySQL. You can install - these individually or a simpler way is to locate a pre-configured package - 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, + MySQL. You can either install + these individually or choose a simpler way. Locate a pre-configured package + 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 rpmfind and PBone helpful for locating RPMs. @@ -81,10 +81,10 @@ or http://127.0.0.1/hello.php but this depends on the web servers configuration. Although this is outside the scope of this tutorial, see also the DocumentRoot and - ServerName 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: + ServerName 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: - 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: Hello World using the PHP echo statement. - 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 Installation 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 PHP support options. @@ -148,7 +148,7 @@ A Note on Word Processors 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. @@ -169,11 +169,11 @@ - 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 - phpinfo function and you'll see a lot of useful + phpinfo function and you will see a lot of useful information about your system and setup such as available - Predefined Variables, + predefined variables, loaded PHP modules, and configuration settings. Take some time and review this important information. @@ -183,10 +183,10 @@ Something Useful - 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 variable. Variables always start with a dollar-sign in PHP. The variable we are interested in right now is $_SERVER["HTTP_USER_AGENT"]. @@ -195,8 +195,8 @@ $_SERVER 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 - Autoglobals + It is known as an autoglobal (or superglobal). See the related manual page on + autoglobals for more information. These special variables were introduced in PHP 4.1.0. Before this time, we used the older $HTTP_*_VARS arrays instead, @@ -206,7 +206,7 @@ - To display this variable, we can simply do: + To display this variable, you can simply do: @@ -231,7 +231,7 @@ Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) Arrays can be very useful. - $_SERVER is just one variable that's automatically + $_SERVER is just one variable that is automatically made available to you by PHP. A list can be seen in the Reserved Variables 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) - 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. 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: @@ -265,7 +265,7 @@ Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) "; } ?> @@ -285,28 +285,29 @@ You are using Internet Explorer
Here we introduce a couple of new concepts. We have an 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 + 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 Language Reference part of the manual. You can find a list of PHP books at &url.php.books;.
- The second concept we introduced was the strstr - function call. strstr is a function built into + The second concept we introduced was the strpos + function call. strpos is a function built into PHP which searches a string for another string. In this case we are - looking for "MSIE" inside - $_SERVER["HTTP_USER_AGENT"]. If the string is found, - the function returns &true; and if it isn't, it returns &false;. If - it returns &true;, the if - statement evaluates to &true; and the code within its {braces} is - executed. Otherwise, it's not. Feel free to create similar examples, + looking for "MSIE" (so-called needle) inside + $_SERVER["HTTP_USER_AGENT"] (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 if 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 if, else, and other functions such as strtoupper and strlen. 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 how to read a function definition and the section about PHP functions. @@ -321,14 +322,14 @@ You are using Internet Explorer
-

strstr must have returned true

+

strpos must have returned non-false

You are using Internet Explorer
-

strstr must have returned false

+

strpos must have returned false

You are not using Internet Explorer
strstr must have returned true +

strpos must have returned non-false

You are using Internet Explorer
]]>
- 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 - strstr returned &true; or &false; In other words, - if the string MSIE 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 strpos. In other words, it depends on + whether the string MSIE was found or not.
@@ -365,7 +366,7 @@ if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { scripts. Please read the manual section on Variables from outside of PHP for more information and examples on using forms - with PHP. Here's an example HTML form: + with PHP. Here is an example HTML form:
@@ -418,8 +419,8 @@ You are 22 years old. method GET then our form information would live in the $_GET autoglobal instead. You may also use the $_REQUEST - 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 import_request_variables function.
@@ -428,11 +429,10 @@ You are 22 years old. Using old code with new versions of PHP 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. Two of the most important recent changes that affect old code are: @@ -479,7 +479,7 @@ You are 22 years old. What's next? - 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: