PHP - The Basics

Help with websites, php, html and databases like MySQL

Moderator: Emmz

Post Reply
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

PHP - The Basics

Post by red_ned »

Example#1 Our first PHP script: hello.php
Create a file named hello.php and put it in your web server's root directory (DOCUMENT_ROOT) with the following content:

Image

Use your browser to access the file with your web server's URL, ending with the "/hello.php" file reference. When developing locally this URL will be something like http://localhost/hello.php or http://127.0.0.1/hello.php but this depends on the web server's configuration. If everything is configured correctly, this file will be parsed by PHP and the output will be sent to your browser:

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. Note that the file does not need to be executable or special in any way. The server finds out that this file needs to be interpreted by PHP because you used the ".php" extension, which the server is configured to pass on to PHP. Think of this as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things.

If you tried this example and it did not output anything, 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, or is not configured properly. Ask your administrator to enable it for you using the Installation chapter of the manual. If you are developing locally, also read the installation chapter to make sure everything is configured properly. Make sure that you access the file via http with the server providing you the output. If you just call up the file from your file system, then it will not be parsed by PHP. If the problems persist anyway, do not hesitate to use one of the many PHP support websites.

The point of the example is to show the special PHP tag format. In this example we used <php>. You may jump in and out of PHP mode in an HTML file like this anywhere you want.

A Note on Line Feeds Line feeds have little meaning in HTML, however it is still a good idea to make your HTML look nice and clean by putting line feeds in. A linefeed that follows immediately after a closing ?> will be removed by PHP. This can be extremely useful when you are putting in many blocks of PHP or include files containing PHP that aren't supposed to output anything. At the same time it can be a bit confusing. You can put a space after the closing ?> to force a space and a line feed to be output, or you can put an explicit line feed in the last echo/print from within your PHP block.

A Note on Windows Notepad 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.) When you save the file and are prompted to provide a name for the file, place the filename in quotes (i.e. "hello.php"). Alternatively, 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.

Example#2 Get system information from PHP
Create a file named info.php and put it in your web server's root directory (DOCUMENT_ROOT) with the following content:
Image


Content taken from http://us3.php.net

ned
Last edited by red_ned on Tue Feb 19, 2008 7:32 pm, edited 6 times in total.
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Post by red_ned »

Dealing with Forms
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 will automatically be available to your PHP scripts. Please read the manual section on Variables from outside of PHP for more information and examples on using forms with PHP. Here is an example HTML form:


Example#1 A simple HTML form

Image

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 action.php page is called. In this file you would write something like this:


Example#2 Printing data from our form

Image

A sample output of this script may be:

Image


Apart from the htmlspecialchars() and (int) parts, it should be obvious what this does. htmlspecialchars() makes sure any characters that are special in html are properly encoded so people can't inject HTML tags or Javascript into your page. For the age field, since we know it is a number, we can just convert it to an integer which will automatically get rid of any stray characters. You can also have PHP do this for you automatically by using the filter extension. The $_POST['name'] and $_POST['age'] variables are automatically set for you by PHP. Earlier we used the $_SERVER superglobal; above we just introduced the $_POST superglobal which contains all POST data. Notice how the method of our form is POST. If we used the method GET then our form information would live in the $_GET superglobal instead. You may also use the $_REQUEST superglobal, 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.

You can also deal with XForms input in PHP, although you will find yourself comfortable with the well supported HTML forms for quite some time. While working with XForms is not for beginners, you might be interested in them.
Last edited by red_ned on Tue Feb 19, 2008 7:37 pm, edited 1 time in total.
Image
May we all fight to live another day.
User avatar
red_ned
CiC-GoD
CiC-GoD
Posts: 4757
Joined: Sat Nov 24, 2007 2:23 am
DayZ Name: Hobn0b
Contact:

Post by red_ned »

Description

int print ( string $arg )

Outputs arg .

print() is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.

Example#1 print() examples

Image
Image
May we all fight to live another day.
Post Reply