| Introduction to PHP |
|
|
|
| PHP - Basics | ||||
| Written by Stuart Duncan | ||||
| Saturday, 03 October 2009 18:33 | ||||
|
Most PHP tutorials start out by telling you it's origins, what year it was created, who was around for it and how it's grown over the years. But none of that will help you build the next great web application and so either you can go find another tutorial which will explain that stuff, or you can read on and start learning some PHP basics right away. First of all, HTML is not a scripting language.. PHP is. I will give you an example of exactly what that means in the most simplest terms. In HTML, if you want to write out "truck" on your page, and reference your "truck" in 14 different places, you have to write out the word "truck" 14 times. 3 months down the road, you trade in your truck for a more fuel efficient car and now you have to refer to your "car" on that page. This means having to go through your page and finding all 14 instances and changing each and every one of them to "car". Not a huge deal, but multiply this by 7 or a thousand times.. you get where I'm going. In PHP, if you want to write out "truck" on your page 14 times, what you would do is set a variable (a place to store data) at the top of the page and have it store "truck". It would look like this $vehicle = 'truck';. Now on your page, in all of those 14 places, instead of putting "truck", you'd put $vehicle and 3 months down the road when you trade in your truck for a "car"... you just change that one line to $vehicle = 'car' and presto, all 14 instances on the page now instantly become "car" and all you had to do was change one word. Make sense? Now that we have that cleared up (I hope), let's start with the basics and get you roling.
VariablesAs in our example above, a variable is a programmer's way of storing information somewhere. The information can be a name, place, number, email address.... it could be an entire page if you wanted, the point is.. you get one word that stores info. In PHP, a variable is recognized by the dollar sign in front of it. This is what tells PHP that it's not just a word, it's a variable. It looks like this: The word part of it can be anything you want so long as it has no spaces, special characters and doesn't start with a number. A lot of popular ones are: The way you assign something to it is with an equal sign. This essentially tells PHP to make variable equal information. Think of it in terms of math and it's pretty straight forward, however it may bring back some bad memories from highschool. We've all seen this one: That's right. While the teacher was trying to shove algebra down your throat, you were learning the basics for programming! There are various types of variable information including string, integers, boolean, float, etc... but those are for another tutorial. Right now you need the basics so start with this.... If you want to assign words/letters to a variable, you have to put them in quotes, as if you were writing it to say "I said 'be this'"... an example would be: This tells the variable where the information strarts and where it ends. Now, if you have a word in there such as "can't"... it will break because there will now be 3 quotes instead of 2. You can fix this by telling PHP to use the quote as a quote with a slash, for example: This is a method called "escaping" where you can tell PHP that the quote is escaping the other quotes and belongs in the sentence. Assigning numbers to a variable is much easier as it requires no quotes and as big as they can be, there are never any spaces or quotes or special characters. A number is just a number. Going back to highschool (again, sorry about this) you might remember (if you stayed awake long enough) that algebra consisted of adding numbers together but they weren't numbers, they were letters and made very little sense. Well, hopefully this clears it up for you.... here it is in terms of PHP. Can you guess what information $answer is now storing? If you said anything but 4, please turn off the computer and go outside. Either you need a break or programming simply isn't for you. If you're still here, we can make things more interesting. This is a very simple statement which tells the variable "number1" to equal 2, and tells "number2" to equal 2 and then tells the variable "answer" to equal "number1" plus "number2".... which again, is 4. Congratulations you just did your first programmatic equation. Pretty simple right? I'm sure you want to start testing this out right away, if you have a website on a server with PHP already, you can begin playing right away. If you are unsure or don't even know what I just said... it's best to ask whom ever is hosting your website for you.
Hello worldEvery programmers first page or program is called "hello world" even if it's not really what you make it say... it's just... tradition. It doesn't have to make sense but in your case, your first PHP script will say "hello world" (if you follow my instructions). Start with a blank page on your website and call it "index.php". You'll notice that it's a .php and not a .html file, this tells the server to run it through a special program which will do the work of changing all those variables to the information they're supposed to be. Once you have a blank page in front of you, I want you to type in <?php. This is what tells the PHP program on the server that it's time to go to work. Everything that comes after it will be put through the PHP parser (program that makes variables into info). Once you have that on the page, go a line down and make a variable with the information "hello world". It should look something like this: It's important to note, if you haven't already noticed, that there is a pattern that needs to be followed. A variable followed by an equal sign followed by the information it stores followed by a semi colon. The semi colon is required to be there. It's what tells PHP that it is the end of the statement. Under that line, you are going to tell PHP to put the information in that variable on the screen. There are 2 different words that PHP can use to do that, there really is no difference and either will work. The words are "echo" and "print". They've both been used over the years and so PHP has just included both for your preference. This means that you will have a line similar to this: Doesn't look too scary does it? The "echo" tells PHP to put what ever comes next to the screen... the "$greeting" is just a place to store information so that can't go to the screen, so it takes the information that is stored there and puts that on the screen which will be "hello world". And again, the semi colon tells PHP that the statement is done. If you recall, you had to put <?php to start the php, well now you have to close off the page (tell PHP to stop working) with a similar piece of text that looks like this ?> This tells PHP that <?php turns on the programming stuff and ?> turns off the programming stuff. And it will do the rest. Your end result, if you did it all right, should look like this:
And there you have your very first PHP page. Save that (as index.php) and then load it up in your browser. Make sure it's on your website and that your website can do PHP (ask your host if you have to)... because this will not work if you just try to save it and load it on your own computer. Variables are the foundation for programming... it's what makes programming possible. You can do almost anything with a variable once you grow your imagination from there. In PHP there are functions, classes, typecasting, namespaces and a bunch of other stuff that is all scary and intimidating but at the root of them all, are variables. The only reason there are those other things is because someone wanted to organize and use their variables better. And that's it. Now go back and look through some HTML pages you've done and try to find someone on them that you may have written more than... say... 3 times. I bet every single page you've done has something on it that where a bunch of things are all the same and could have been done using a variable. Imagine the posibilities with the adding thing... you could add up 10 items, figure out the taxes, add in shipping and produce a total... all with adding variables together. And on that note, I'm going to leave you with a few more examples that you can copy and paste into your new "index.php" file... be sure to change them up and see what happens. Some math:
Name game:
A whole new website:
Have patience, there is a lot more you can do with this but the title is called an "introduction to PHP"... if you can master these very simple concepts, you'll learn more specifics easily and how to do other cool things very quickly. |
||||
| Last Updated on Friday, 09 October 2009 11:10 |




