What do I need to know?
In order to complete the tutorials I write on PHP and have any idea what happened, you will need to have a basic understanding of what HTML/XHTML is and a little previous experience using it. If you don’t have the experience required you are still welcome to try, but I can’t be held responsible for any hair loss that results from such an action ~_^.
What is PHP?
Chances are, if you are reading this document you already have a general idea of what PHP is, but please allow me to contribute a brief explanation. PHP, ironically, stands for PHP: Hypertext Processor (See the Manual). It is a recursive acronym which means that the acronym is restated inside of itself.
PHP was originally created in 1995 by Rasmus Lerdorf as a quick way to create and run his online resume. He called it PHP/FI which stood for Personal Home Page / Forms Interpreter. He borrowed much of his syntax (how the code it written) from PERL and C, two other programming languages. Within three years it attracted several thousand users and the rest is history! (See the Manual).
The purpose of PHP in today’s machines is about the same as it was back in 1995. It was, and still is, to create dynamic webpages and process forms. PHP is a server-side language, which means that it runs all of its code before anyone viewing the webpage sees anything. The results of PHP’s code is then sent to the browser which is what the viewer of the webpage will eventually see. All this, of course, is done in the blink of an eye. PHP is incredibly fast at what it does. PHP runs the code given to it (including content from forms, cookies, and more) and then dynamically generate HTML and sends that to the browser.
What is PHP good for?
PHP is good for hundreds of uses, from photo galleries, to e-commerce. Since it runs on the server, it is and excellent choice for running high security applications because it can easily hide information from the end-user (the person viewing the webpage). Because of its integration with databases (specifically MySQL) PHP is excellent for creating large, information driven, websites. People use PHP to make hundreds, even thousands of websites and pieces of websites on a yearly basis.
Lets meet the meat!
Ok, so you want to see PHP in action? Try this on for size!
<?
// Assign the title variable
$thetitle = $_GET[�title�];
// Nearly every line in PHP ends with a semi-colon. This allows PHP to run multiple commands on one line (though running more than one command on one line is bad practice and is just generally a pain to read later on).
// Assign the head variable - notice it is enclosed in single quote marks.
$thehead = �<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>�.$thetitle.�</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="generator" content="HAPedit 3.1">
</head>
<body bgcolor="#FFFFFF">�;
// Now we assign what is going in the body - notice it is enclosed in double quotes
$thebody = "Well, this website seems to be running a few dynamic features. For instance, the title of the website (AKA $thetitle) changes at will.
Don�t believe me? Look up top! Whatever you stick at the end of the URL changes the head of the website!";
// Assign whats in the footer of the page - again, single quotes.
$thefooter = �</body>
</html>�;
// Now we use the print command to send these variables (in order) from the PHP page to the browser
print $thehead;
print $thebody;
print $thefooter;
?>
Seems complicated, but here it is in action!
Allow me to explain this example. These two tags "<?" and "?>" are placed in your document surrounding the PHP code. This allows you to place non-PHP specific code into the webpage as well by putting it outside the PHP tags. You could also have code on your website twice, by opening it inserting your code, closing the PHP then doing the same later on. The first line (after the comment) "$thetitle = $_GET[’title’];" tells PHP that the variable (a container, if you will) $thetitle will now hold the value (or contents) of the $_GET[’title’] array. I will explain more about where the $_GET array comes from later, as it is a very special part of PHP. Those who have used the <form> element before might recognize the "GET" action which PHP dumps all into one ‘array’, $_GET.
Next I assign $thehead some contents. This is the head to the page. You may notice one strange thing about this section code, "<title>’.$thetitle.’</title>". I break the quotes surrounding the <head> HTML on the <title> tag to put in the variable $thetitle.
Next I assign $thebody some text. This time the quotes do not break when I insert the $thetitle tag into the content. This variable I will use as the body of my webpage. And finally I assign $thefooter variable. This is simple enough, it is just the end to my webpage.
Unfortunately just assigning them to variables will not do anything. If I don’t use a command to tell PHP to send the variables to the browser, then the page would appear blank! Therefore, we use the print command to send all of the variables, in order, to our browser. Now the person viewing the page will see the end result of the work we did!
Try it for yourself!
I can’t encourage you enough to try messing around with assigning a few variables in a PHP script. Don’t get crazy if they do not work. I will be back with a tutorial discussing how to create your own PHP pages and the intricacies involved in getting them to run.
Because of the server-side nature of PHP, you will need to run them on a server that supports PHP. My first recommendation is to go out and find a webhost that supports PHP. I assume that you already have a web host, if you are looking into mastering PHP. If you do, then you will know how to upload your files to the server and use that to test your scripts. If you do not know, then you will need to do some research on hosting and PHP. If you are really ambitious then you can try to install PHP on your home computer. This takes some moderate to advanced computer skills to accomplish. For more information on installing PHP on your own computer, consult the manual of PHP.
Just who does this manual think it is?
You have seen me make several references to "the manual." The manual is THE official reference for anything PHP. The site that runs the manual is the same site that is responsible for PHP. They have compiled a manual for programmers to use as they work through the complicated list of functions, features, special variables, and everything else that PHP has to offer. If you are already an experienced programmer (I doubt your still reading =P) then this site will offer you all the tools you need to add PHP to your list of programming languages learned. If you are not experienced at programming, then I suggest continuing to work with tutorials and books before you start using the manual on a regular basis. This is because the manual is written by geeks, for geeks ~_^. You need to gain some understanding of what sometimes seems like a completely different language that geeks speak. Pay close attention to the tutorials you read as they will try to explain many of the words you will find on sites like the PHP manual. You’ll get there soon enough!
Be the first to comment on this post!
Leave a Reply