The following course text material is Copyright KMR Enterprises All Rights Reserved. You are welcome to download and copy the course text material and use for your own personal study/use. You are not permitted to post, publish and/or redistribute the course text in any other format, online or offline without express written permission from KMR Enterprises. You are welcome to and encouraged to share your own course to-do assignments online (your blog, website, etc) and link back to the relevant course material here at Webmaster Cookbook. Thank you and happy coding to you!
Lesson #1: Introduction to PHP Programming
What exactly is PHP?
PHP is a server side scripting language bearing similarities in structure to C, Java and Perl. PHP started as one man's (Rasmus Lerdorf) personal project to make embedding more complex code in HTML a lot easier and has grown into being a full-fledged scripting language. It is growing in both popularity and has become one of the most powerful web scripting languages since this course material was first written in August 2000. Depending who you talk to the name PHP can stand for several different things, but today it is most commonly known as: Hypertext Preprocessor. The home web site is http://www.php.net/
zzzzzzzz ...
Ok have I put you to sleep yet?
Seriously, I would like to try and have you come away with something useful you can take back to your websites and use. So if you promise not to fall asleep, I'll promise to try and not make you sleepy and explain why you should be interested in PHP and show you over a series of 16 course lessons (split into 32 weeks of course instruction) on how to do some really cool and useful things that you'd normally have to pay a programmer several hundred to several thousand dollars to do for you. But first a little maintenance.
You don't have to be a genius!
Before you run clicking for the back or exit button, understand that programming isn't necessarily some huge insurmountable process. I like to think of any project in a much smaller sense and right now when I say that I'm going to teach you fundamental programming in PHP over a series of 16 course lessons that might instill panic in a few webmasters or seem like an insurmountable task to others. I have been doing programming for over 20 years and I can safely tell you that I am still very much learning, so the process of learning never stops, nor should it.
First, let's break this down into a series of logical issues and this is the same way you might go about dissecting a programming project.
I. What tools do I need for the project?
II. What programming language or languages will work best for the project? HTML, JavaScript, PHP, Perl, etc.
III. What knowledge do I need to possess?
IV. Where do I start?
Every time we look at a programming tutorial we'll start with this fundamental concept. There are some basic tools that you can't do anything without and the best part is that everything you need you can obtain for free, including the server to run PHP on your local PC! Items II-IV above we will deal with when we get into our first programming project in PHP. You will need the following tools for every project we discuss.
I:Tools needed for PHP scripting:
1. You need a text editor of some kind to do scripting. You could use the free one that comes with Windows: Wordpad or something fancier, but still free, like PSPad. You can use Notepad also, but Notepad limits the size of documents, so probably not a good idea to get used to using that. Feel free to post your comments/suggestions for alternate text editors in the show blog comments.
2. You need a UNIX or NT host (I will commonly refer to using a host as an external environment) or by using your local environment (your PC you are likely reading this from now) with the PHP source compiled. While these tutorials will be a great springboard for you to write code, if you don't have a place to actually execute the code you won't actually be able to do anything with the code you write. You can set up your own local webserver in Windows like Apache, but we will not be discussing how to do that in this course. If you'd like a list of hosts that currently support PHP, at the PHP website there is a cool search engine you can use to actually choose what features you want: http://hosts.php.net/search.php3 Below I give you a simple way to test if the host you have is running PHP. One final note on whether to use UNIX or NT hosts when programming PHP, I am but a newbie in the woods to the world of scripting PHP on an NT host, so most of my examples will focus on PHP for UNIX servers.
3. If you are using external environment you will need an FTP editor. You can use fancy HTML editors like Front Page, but I would not recommend this. These editors have a habit of introducing "helpful" code in irritating and in some cases invisible places like carriage returns, extraneous HTML, double quotes, etc, to your scripts that can cause immeasurable problems. There are many good FTP editors out there. One that used to be free, but now requires a license is http://www.smartftp.com/ which will allow you to drag and drop files similar to how you'd do in Windows Explorer. You need an FTP editor that will allow you to change permissions of files. If you have never set permissions on a CGI file or directory, you can usually accomplish this by right clicking on the file or directory in your FTP editor and a menu should pop up that says permissions or chmod or something like that. If the editor you are using doesn't seem to have this capability (most do) consider downloading one that does.
How do I know if my host has PHP?
The easiest way is to consult your host's website or call them directly and ask, but if you have one of those hosts where reaching them is difficult, then you can run a simple test using PHP code. TYPE the following code into your text editor (sometimes CUT AND PASTE adds troublesome formatting to the code when going from HTML to your text editor, so always type my examples directly into your text editor):
<?php
// Does my host have PHP, and if so, what version?
printf("Your host is running PHP version %s", phpversion());
?>
Example of output:
Your host is running PHP VERSION 5.0.5
Now save the above file in your text editor as phpversion.php or phpversion.phtml. Note: you can often use php3 or phtml or php synonomously as an extension to identify a PHP script. In the course we will use .phtml to designate php pages with HTML components and .php for script only files.
APACHE USERS: if your site is hosted on an apache webserver the default extension is .php in apache.
FTP this file in ASCII mode somewhere on your webserver. Some FTP editors will have a mode called "auto" to determine whether the mode is ASCII or BINARY. I would not recommend using "auto" mode, instead override the setting and make it ASCII. For those Perl people in the audience, the good news is this file does not have to be in a cgi bin, nor do you have to set any special permissions for it. Now goto your browser and call the script by typing it from the browser line. If you have PHP you will see the version number that is installed on your server.
If you'd like to see the modules and settings that your host has installed for PHP on your server in a neat table you can use the following code:
<?php phpinfo(); ?>
PHP code tags
As you could see in the above example we used <? and ?> tags. While these might seem foreign at first, this is how you tell the interpreter where your php code begins and ends. Similar to your <HTML> and </HTML> or <CENTER> and </CENTER> tags in a document, you cannot write working PHP code without enclosing your php code inside these every time. There are other valid formats you can use if you don't like the more simple <? and ?>, you just need to make sure you use the correct corresponding group. For example using <? to start the code and </script> to end it would produce undesirable results.
<?
<--- this
indicates the starting of php code (group 1)
<?php
<--- another way of starting php code
(group 2)
<script language="php"> <--- another way of starting php code
(group 3)
?>
<--- this
indicates the ending of php code (group 1)
php?>
<--- another way of ending php code
(group 2)
</script>
<---- another way of ending php code (group 3)
Therefore we could have also written the code above using the more common group 3 php tags as:
<script language="php">
/* Does my host have PHP, and if so, what version? */
printf("Your host is running PHP version %s", phpversion());
</script>
You do not have to separate the lines of code, as I did above, you can put everything on one line like this:
<?php PHP CODE GOES HERE ?>
This concludes part A of lesson #1 of the PHP101 scripting course. Next lesson - 1B | HomeCopyright 2000-2008 KMR Enterprises All Rights Reserved