yet more PHP

Status
Not open for further replies.

b.dave

Baseband Member
Messages
79
Introductory Tutorial
PHP is a tool that lets you create dynamic web pages. 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.
What do I need?
In this tutorial we assume that your server has support for PHP activated and that all files ending in .php3 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 .php3 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.
Your first PHP-enabled page
Create a file named hello.php3 and in it put the following lines:

<html><head><title>PHP Test</title></head>
<body>
<?php echo "Hello World

"; ?>
</body></html>


The colours you see are just a visual aid to make it easier to see the PHP tags and the different parts of a PHP expression. Note also 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.
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: Hello World

If you tried this example and it didn't output anything, chances are that the server you are on does not have PHP enabled. Ask your administrator to enable it for you.

The point of the example is to show the special PHP tag format. In this example we used <?php to indicate the start of a PHP tag. Then we put the PHP statement and left PHP mode by adding the closing tag, ?>. You may jump in and out of PHP mode in an HTML file like this all you want.

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 request. This information is stored in a variable. Variables always start with a dollar-sign in PHP. The variable we are interested in is $HTTP_USER_AGENT. To display this variable we can simply do:

<?php echo $HTTP_USER_AGENT; ?>

For the browser that you are using right now to view this page, this displays:
Mozilla/4.0 (compatible; MSIE 4.01; MSN 2.5; MSN 2.5; Windows 98)
There are many other variables that are automatically set by your web server. You can get a complete list of them by creating a file that looks like this:
<?php phpinfo()?>

Then load up this file in your browser and 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:

<?php
if(strstr($HTTP_USER_AGENT,"MSIE")) {
echo "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 should probably pick up any introductory C book and read the first couple of chapters. All the tricky string and memory manipulation issues you have to deal with in C have been eliminated in PHP, but the basic syntax remains.
The second concept we introduced was the strstr() function call. strstr() is a function built into PHP which searches a string for another string. In this case we are looking for "MSIE" inside $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 following statement is executed.

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:

<?php
if(strstr($HTTP_USER_AGENT,"MSIE")) {
?>
<center>You are using Internet Explorer</center>
<?
} else {
?>
<center>You are not using Internet Explorer</center>
<?
}
?>

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. Running this script right now results in:

You are using Internet Explorer
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 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:
<form action="action.php3" method="POST">
Your name: <input type=text name=name>
You age: <input type=text name=age>
<input type=submit>
</form>

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.php3 page is called. In this file you would have something like this:
Hi <?php echo $name?>. You are <?php echo $age?> years old.

It should be obvious what this does. There is nothing more to it. The $name and $age variables are automatically set for you by PHP.
More?
This tutorial will get expanded, but at this time this is all there is. With what you know now you should be able to understand the online manual and also the various example scripts available in the example archives.
 
Ok,
I'm running Apche 1.3 with php 4 on a Win2k server. How would I go about doing the Window's counterpart of CHMOD??
Becuase I keep getting the message
Method Not Allowed
The requested method POST is not allowed for the URL /action.php3.
and if I just use the *.php* extension the only thing that i get is...
Hi . You are years old.

:confused:
 
Status
Not open for further replies.
Back
Top Bottom