PHP Primer

So you want to finally make life easier on yourself and learn PHP eh? Congratulations, not only will PHP help your website become something more than the sum of it’s parts, but it will make your life a lot easier! But before we begin, lets get a couple things out of the way.

  1. You need a good understanding of the English Language - I’m likely to use some bigger words, so you’ll need to understand them
  2. You need to know HTML - Yes, this is a requirement of this tutorial
  3. You need a server with PHP installed - There are plenty of help sites out there that will help you with this, so I’m not going to cover it
  4. You NEED to bookmark http://www.php.net - This site is essential! It’s got a seachable database of all of the functions in PHP, and you’ll want to leave it open while your coding, as it is an excellent resource.
  5. It helps if you know C - C and PHP are very similar, heck, C++ and PHP are too

Those are pretty much the requirements for this tutorial, and those come with one other disclaimer. I (Jamie Harrell) am a programmer by nature, so I might take some assumptions down the road in this tutorial as to what you might already know, or what might seem “easily understood”. If you don’t understand something, please contact me in the forums and tell me what it was you didn’t understand, I’ll be more than happy to help you out and perhaps revise these tutorials in a way that makes more sense to non-programmers. So, with that out of the way, lets begin.

Lets start off with the question “What is PHP?” Well, PHP stands for PHP Hypertext Processor, which in of itself is recursive, which later becomes somewhat of an irony. PHP is a scripting language much its programming language cousins of C and C++, but this time specifically designed for web-based applications. And it should be noted that unlike Javascript, PHP is a server-side scripting language, which means that all of the information is processed BEFORE it is sent to the visitor. Javascript and VBScript send the code to the visitor, and their browser does the dirty work of figuring out what to do and doing it. What’s the advantage to server side? Well simply put, if it works for you, it’ll work for everyone, there is no guess work involved, no figuring out what browser they’re using (other than the typical html worries), and no wondering if the clients browser even supports Javascript or VBScript, because as far as the visitors browser is concerned, they’re viewing an HTML page. Tricky eh? Well, you’ll see how that works later on in this tutorial.

So, lets start off by figureing out how we setup a PHP document so that it will be processed. In HTML, we’re all familiar with the <> set that encompasses all of our variables, well, in PHP we have a new tag, which looks like this:

<?php
?>

And all of our PHP code goes in between these to statements. This is telling our server (with PHP installed) that “hey! this portion of my site is PHP! Decode it first before you send it to the visitor!” The only other real change we have to make to the way we’ve been doing things is that a PHP document MUST have the extension .php, otherwise it won’t work properly. There are sometimes other alternatives like .php3 or the like, but truth be told, it’s often just easier to stick with .php and forget about the rest.

So, now we’ve got a PHP document, and we know where to put the code for our website, but we don’t know what to do inbetween our new friendly tags of <?php and ?>, now do we? Well, lets start off with something simple, like a variable. And so we create first.php which looks something like this:

$endstring = "";
$endstring .= " $endstring .= " \$myVariable = \"Hello World\";\n";
$endstring .= " echo \"\$myVariable\";\n";
$endstring .= "?>\n”;
echo highlight_string($endstring,true);;
?>
What does this do? Well, it’s the infamous “Hello World” program, only this time, in PHP. Lets go thru this line by line. First thing we do is tell the parser “hey! we’re using php here, decode this” by doing:

<?php

Now that that is out of the way, we move on to setting a variable to our string, which in this case should hold the value “Hello World”. In PHP, you use the $ character to say “Ok, this is a variable” so that it will seperate it from a regular string. So by doing:

$myVariable = "Hello World";

It’s like saying the following in plain English: Take the string of Hello World and insert it into the variable $myVariable. Also notice that the line ends in a ;, which is required to be at the end of EVERY line of PHP code, because it tells the interpreter that we’re done doing that statement, so now we’re going to move on to something else. So how that we have it in a variable, we can display it! So we now do:

echo "$myVariable";

And here we’re saying: print out the value of $myVariable, which will display “Hello World” to the screen. But be very careful here, PHP is a Case Sensitive language, which is to say that $myVariable is not the same variable as $myvariable or $MyVaRiable, so you must use the same case in every instance of the variable, otherwise it will not work. And then finally we do the following:

?>

Which tells the interpreter that we’re done dealing with PHP right now. And what would the output be? Well, it’d look like this:

Hello World

Pretty slick eh? So now you know how to assign a variable and display it, so lets look at a more practical purpose with second.php:

$endstring = "";
$endstring .= "\n”;
$endstring .= ” \n”;
$endstring .= ” \n”;
$endstring .= ” \n”;
$endstring .= ” \n”;
$endstring .= ” \n”;
$endstring .= ” The title of this page is !\n”;
$endstring .= ” \n”;
$endstring .= “\n”;
echo highlight_string($endstring, true);
?>
So in this example, we’ve mixed our PHP and HTML all into one nice file. This is most likely the method you are going to use the most, as it offers the most functionality with the least amount of work. Nothing really new here, we’ve assigned our file a .php extension, we assign our variable of $myTitle to the value of “Peoii’s Place” and again we’re echoing it in certain locations. This time however, we’ve mixed it into our HTML. So in this example, the value of $myTitle will be used in 2 locations, both as the title of the page, and in the text of the page. Again, pretty powerful stuff if you start to think about other things you might do even with this simple variable assignment system.

Well, by now you should have a pretty good idea of how to use a variable in PHP, and the basics of how to get PHP working within your website. So I’m going to end this part of the primer here. In the next section (coming soon), we’ll be covering the Math Functions, messing with strings, and getting to understand an if-statement structure. But for now, this tutorial should get you some idea of what we’re going to be dealing with in these tutorials. Cheers for now.

2 Comments

  1. Paul Higginbotham:

    Thanks for a good overview stressing the differences between php and C, JavaScript, or Visual Basic. Suggest you include a date in the tutorial, e.g., — was it done in 1996 or in March, 2008 — the date of this note.

    Thanks and best regards,
    Paul

  2. Eric:

    Good start. I’m not a programmer, but I have begun to bite the bullet and learn php. Looking forward to following along. So far it’s understandable to a non programmer.

Leave a comment