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 becoming more powerful as a language and version 4.0.2 has
just been released. Programmers met in early 2000 in Tel Aviv, Israel to
discuss the organizational direction of PHP as well as creating even better
functionality with the increasingly popular mySQL database. 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/.
I. What tools do I need for the project?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:
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): <?
Example of output: Your host is running PHP VERSION 3.0.7
Now save the above file in your text editor as phpversion.php3 or phpversion.phtml.
Note: you can often use php3 or phtml or php synonomously as an extension
to identify a PHP script.
<? 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)
?>
<--- this indicates the ending of php code (group 1)
Therefore we could have also written the code above using the more common group 3 php tags as: <script
language="php">
You do not have to separate the lines of code, as I did above, you can put everything on one line like this: <? PHP CODE GOES HERE ?> Adding Comments To Your Code While you may understand the code you are writing it is a good idea and considered good practice to comment your code. Although it is entirely optional, I will adopt this practice throughout our tutorials so you can better understand what the code is doing. I did so in the above example. There are two different ways you can add comments. C type method: /*
your comments
// comments can only go on one line, no closing is necessary Refer to my example above and you'll see I used the // method of adding comments to the code, since my comment only took up one line. It doesn't matter which method you use for comments so long as you remember that if you use the C method you must close the comment tag ( by using */ ) and if you use the // method that you keep the comments on the same line. Remember that comments are optional yet a good idea for producing readable code. While you write the code it is fresh in your mind, but often times several months later when you go back to review it will not be. Using built-in PHP functions PHP has a lot of useful functions built-in to assist you in coding. Things like printing to the browser are taken care of by using the function print. In our example above we used the slightly more complicated printf function to print the version of PHP to the browser in a certain format. Most every programming language starts you off by printing the rather useless: "hello world" example. I would now recommend that you go download the PHP function library. You can do this by visiting http://www.php.net/docs.php3 and downloading the big zip file (1500kb). The PHP function documentation for the print function looks like this: print
This is commonplace for programming reference manuals that they speak in technobabble that irritates even programmers. The only reason I suggested you download this is that you will need to have this as a reference material down the road. I will give you a much friendlier explanation than this for the functions we use, but I wanted to show you where the built-in functions are explained. You should further note that not all functions listed may be available in your version of PHP. For the purpose of our courses I will try to only use functions that are in lower versions whenever possible so the examples will work on your version of PHP. Now before I can give you my friendlier explanation of the print function, it requires understanding what a string is. What are $strings? A string is a container that holds text which can be made up of letters, numbers, words or entire sentences. Strings can contain integers (whole numbers like 1,2,3,4,5) or floating-point numbers like (1.5, 2.67, 3.9993, 9.0), or text. A string name is preceeded by a dollar sign $ to show it is a string. Numbers are not required to be enclosed in quotes, but alphanumeric characters are (IE. "7 lessons"). You can assign a value to a string by using the equal sign = $myname
= "TDavid";
String assignments must end with a semi-colon. Valid string names begin with an letter and can contain numbers or the underscore symbol _ Case sensitivity matters in the Unix world, so $tdscripts and $TDscripts would actually be two different strings. Valid
String Names
INvalid
String Name
Tip: Use string names that identify what you are storing. For example it
may take a bit less space to use strings like $a = "123 Any Street"; but
it is much easier to understand code when you have problems if you use
something like $address = "123 Any Street";
<?
Could also be written by assigning the text to a string like this: <?
Notice that when you print a string you do not need the quotes surrounding
the string, and that in PHP you enclose the string you want to print in
parenthesis. Now if you look back at the php manual definition for the
function print you will see that arg stands for argument. An argument
is what is passed inside the parenthesis to a function. In this case the
contents of the string is its argument. Is the technobabble a littler clearer
now?
TO-DO ASSIGNMENT #1: Write a script to store and print your name (nick) and at least one favorite url to the browser using variables to assign them. Make the variable names conform to the rules explained above and use comments in the code explaining each line of code. Cut and paste your code to the Week 1 forum to share with others in the appropriate "To-do Assignment #1" folder. Need help with your assignment or have questions about the first week's course?
LIVE CHAT: Script School Live Java WORKSHOP: Adult NetSurprise Technical Chat Tuesdays 3:00 PM Eastern / 12:00PM Pacific - we will review this course here in depth AUDIO: Script School Live Audio Friday 8, 2000 5:00 PM Eastern / 2:00PM Pacific - we will review the week's code posted in depth on this show
|