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 #1b: Introduction to PHP Programming
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
can go here and can
be on multiple spaces
and when you are
finished writing notes
don't forget the closing
*/
// 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. You can find documentation for any PHP function by using the following format: php.net/FUNCTION_NAME. So if you wanted to find the documentation for the print function you would copy/paste to your browser: http://www.php.net/print Try that now. The PHP function documentation for the print function looks like this:
print
Description
print -- output a string
print(string arg);
outputs arg
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';
$my_website = 'tdscripts';
$myfavoritenumber = 7;
String assignments must end with a semi-colon. Valid string names begin with a 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
$tdscripts
$Script_School
$Visit_my_tech_radio_show_every_friday_at_2pm
INvalid String Name
$2pm_PST <---- starts with a number
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';
Now that you understand what strings are let's go back to a more friendlier explanation of what the print function does. It prints the contents of a string to the browser.
<?php
print('Yippie! My host is running php!');
?>
Could also be written by assigning the text to a string like this:
<?php
$comments = 'My host is running php!';
print("Yippie! $comments");
?>
Notice that double quotes need to be used when mixing $strings and text because the contents are then interpreted instead of literal. Notice that when you print a string by itself you do not need the quotes surrounding the string, and for this I usually use echo($string) which does almost the same thing like this:
<?php
$comments = 'Yippie! My host is running php!';
echo($comments);
?>
A technical aside for the purists in the audience: in terms of being useful, why would you ever want to use the second example above? Let's say you are building a script that will be translated into multiple languages. Rather than having to go through all your code and changing the English text strings inside the print functions to say Spanish instead, you could go to one section where a bunch of text strings are being assigned and easily make the translation in one section of code.
Well that covers some very basic terrain with PHP. With these basics you should now know the tools necessary to create a PHP script (and where to obtain them), and how to store information in strings and print strings. Next week we'll dig in and I'll show you how to create a picture of the day script in PHP that you can freely use on your websites. But first ... let's give you your very first coding assignment:
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.
This concludes lesson #1 of 16 in the PHP 101 scripting course. Next lesson - 2A | Home
Copyright 2000-2008 KMR Enterprises All Rights Reserved