Learning to program with PHP for beginners

Introduction to PHP

PHP is a powerful language that allows you to create dynamic pages which means pages that can change depending on how a user interacts with your page or events like the day of the week. PHP is a hypertext pre-processor. When a page that has PHP code is requested, the server first checks to see if you've put any special PHP instructions and then processes them before sending the page to a client. Instead of everyone that comes to your site getting the same page, they can each get a customized experience.

Parsing

When a php page is requested, the server looks for special opening <?php and closing ?> tags. Once it finds these tags, all the code in between them gets parsed (a fancy word for translated) by the server. The client receives the page, plus the results of whatever you programmed.

Extensions

Before you can put PHP code in your pages, you'll need to make sure that any pages that you use php with are saved with a .php file extension instead of the usual .html ext ension. Otherwise your PHP code won't be parsed (translated) and will appear on your pages unprocessed. I usually save all of my pages with the .php extension because I know most of them will have php code.

Let's get started

Most of the time, you can simply place the php code within your html. Let's examine a simple, but useful php command:

 
<p>Today is <?php print date("l"); ?></p>

If you put that on a page with a .php extension and typed things correctly, then you'll see whatever the day of the week appear. Think of how powerful that is. You could use this information to display different information to your users depending on the day of the week.

The first line of code establishes the time zone for the current website. Since you probably have no idea where your server is, then it's important to set a default timezone for your site so that when you calculate time, it's done in relationship to the server's timezone.

Functions

The date() command is called a function. A function has a name like date followed by open and close parenthesis (). If the function needs additional information to do it's job, we can pass that by including words or numbers within the parenthesis; these are called parameters.

If there was more than one parameter to pass, we would separate them by commas. In the case of the date() function, we need to pass it how we want the date formatted. If you pass information that is not a number, but words or letters (as in the letter l above), you need to include that information within quotes. We call that type of information a string.

Conditionals

Once you've got the ability to tell what day of the week it is, you can use that to show someone a special message if it's a certain day of the week. The most used conditional is called an if-then-else statement. Let's see how it works.

 
<?php
if (date("l")=='Monday') {
	print "<p>It's Monday.</p>";
} 
?>

If Then Structure

The if then statement goes is formatted something like this if () then {}. This looks at first like a function, but it isn't. The stuff between the parenthesis () is where we can test wether somthing is TRUE or FALSE. This is called a conditional statement. A conditional statement asks a questions about what's within the parenthesis.

What's with the double equal sign?

PHP understands a single equal sign as an assignment operator, so you can't ask a true/false question with an = sign. If I were to say 'a=b', then I would be making the value of a equal to the value of whatever b was. If I'm asking a question "is a the same as b?", that is written like this 'a==b' and in that case, the double equal sign '==' is called a conditional operator. A very common mistake is to use a=b instead of a==b when you're asking a question. You won't get an error because a=b is always true, and you'll go nuts trying to figure out why it's not working.

The stuff between the curly brackets are called statement blocks. Statement blocks tell php to perform a series of steps. Every step you want to have PHP do should be separated by a semicolon (;).

This works great as long as it is Monday. if it isn't we can add some additional code to make it work everyday.

 
<?php
if (date("l")=='Monday') {
	print "<p>It's Monday.</p>";
} else {
	print "<p>It's not Monday.</p>";
}
?>

Now it will work everyday. If it is a Monday, then it will give you the first message, if it isn't, it will give you the second message. So let's talk about the new notation here.

Else

The else statement is optional but allows you to do something if the condition is false. Again we see the statement blocks here that allow you to do more than one thing.

Variables

That was a good example, but, I want to change the program somewhat. I want to say what day of the week it is and then have a few different special messages for some of the days.

 
<?php
print "<p>It's ".date("l");
 
if (date("l")=='Monday') {
	print "...It's going to be a long week.</p>";
} 
 
if (date("l")=='Friday') {
	print "...Hurray!!! Let the games begin.</p>";
}
?>

We're using the function date("l") an awful lot. We can use variables to make this program a little easier to read and update in the future.

 
<?php
	$day=date("l");
	print "<p>It's ".$day; 
 
	if ($day=='Monday') {
		print "...It's going to be a long week.</p>";
	} 
 
	if ($day=='Friday') {
		print "...Hurray!!! Let the games begin.</p>";
	}
?>

Variables in PHP always begin with a dollar sign ($). They're a great way to simplify your code as well as create a shorcut to a complicated formula that you can use over and over in your pages. As the name implies, their value can also change over time.

Concatenation

You may have noticed something peculiar about the code above. We created a sentence from the word It's and the day of the week by using a period to combine it with the variable $day. The period (.) is a special character to PHP called the concatenation operator. It glues together two pieces of information into a single piece. It's a very common PHP command.

Single vs Double Quotes

You may have noticed that sometimes in our code, we use single quotes and sometimes we use double quotes. You can use either when you use strings in PHP, but the double quotes have one superpower. If you put a variable inside double quotes, PHP replaces the variable with it's value. Therefore you could replace:

 
print "<p>It's ".$day;

with:

 
print "<p>It's $day";

And both will work in the same way.

Escaping

You might be asking yourself...what if I want to use a single or a double quote as part of what I'm printing. There are two ways to deal with this. One, you've already been using. When we say:

 
print "<p>It's $day";

We've got a single quote inside a double quote statement; so one way to deal with this is to alternate the quotes. But what if we wanted to write something like"

 
print "<p>It's $day and there'll be 12" of snow";
If you write something like that, you'll get an error because the double quote means something to PHP and it thinks that the end of the print command is right after the number 12. In order to include an inch symbol in our print statements, we have to escape or tell PHP that we really don't mean to end the print statement.
 
print "<p>It's $day and there'll be 12\" of snow";

Includes

Another real good reason to use php is to simplify the development of your pages. Let's say that your website used a list of your favorite links (also referred to as a blogroll) on every page. If you manually write this into every page, when you're ready to add a link to your blogroll, you'd have to go through all pages on your site and make sure the information was updated.

Say your blogroll looked like this:

 
<div id="blogroll">
<h2>Favorite Sites</h2>
<ul>
	<li><a href="http://planetoftheweb.com">planetoftheweb.com</a></li>
	<li><a href="http://mega981.com">mega981.com</a></li>
	<li><a href="http://neimultimedia.com">neimultimedia.com</a></li>
</ul>
</div>

If you want you can save your blogroll as a separate file and call the include function to include that file on any page you want. Now when you want to update your blogroll, you simply open the blogroll.php file and any pages including this document will be automatically updated. You would do this with this code:

blog comments powered by Disqus