PHP Tutorial: The Very Basics
Learning PHP is actually not as difficult as you may think. Many webmasters shy away from trying to learn PHP and other web languages because they simply believe that they “don’t understand that stuff”. The fact is, with just a little time, effort, and the willingness to learn, learning PHP is not difficult at all.
This article will lay a foundation so that webmasters can learn the very basics of PHP. Hopefully in future articles we will be able to advance to more advanced topics and provide the tools necessary to build basic applications in PHP.
PHP – What Is It?
PHP is a programming language intended solely for the internet. Unlike Perl, C++, and other programming languages, PHP applies primarily to internet programs. The beauty of PHP is that it is a server side technology that can be run directly among HTML. Confused by that statement? Let us explain.
Server side technology, or a server side program, is a program that is run by the computer that serves up the website (called the server). The opposite of server side is client side. The difference here is this: where server side programs are executed by the server, client side programs are executed by the user’s computer.
To understand this more, go to any page that ends in ‘.php’. View the source of that page. Can you see anything that does not look like HTML code? You may see some JavaScript code, but most everything will look like HTML. This is because before the server that hosts the website spits out the web page to your screen it executes all the PHP code and only spits out the results of the PHP code. This is called a server side technology.
The opposite of server side technology is client side technology. The most well known client side technology out there is JavaScript. If you view the source code of a page, you can see the original JavaScript code. When your computer receives a web page with JavaScript, it is your computer that executes the code and displays it on your screen.
So, PHP is a server side technology that can be run directly among HTML. This means you can make a website dynamic without straying too far from the HTML that you know and are probably somewhat comfortable with.
What You Will Need
If you are still confused as to what PHP is, do not worry just yet. As you start using PHP a little bit you will grow more comfortable with it and understand it just a little bit more. However, the only way you can really learn about PHP is to start using it. To do that you will need to have a web host that supports PHP. This should not be too hard to find as nearly every web hosting company out there supports PHP. In fact, chances are your current web host supports PHP.
You will also need a basic HTML editor that allows you to view and edit the code of your pages. I recommend Macromedia Dreamweaver, but you can use any program you like. That is all you really need to get started.
Your Very First PHP Script
Now you get to see just how easy PHP really is. With just a regular HTML document we will program a very simple PHP script. Start out with a blank HTML page. The source on the page should look something like this:
<html>
<head>
<title>A very Simple PHP Script</title>
</head>
<body>
</body>
</html>
As this stands right now it is a blank HTML page. Remember how we said that PHP can be run within HTML? Well, that is exactly what we are about to do right now.
Whenever you want to use PHP code, you have to tell the server what you are doing. To do this, you need to “interrupt” your HTML with a tag that says you are now using PHP. That tag is generally “<?php” (without the quotes). When you are done with your PHP code, end your code with “?>”. This tells the server to execute anything between those tags as PHP. So let’s put that part in place:
<html>
<head>
<title>A very Simple PHP Script</title>
</head>
<body>
<?php
// Some PHP Code Here
?>
</body>
</html>
There you have your PHP tags in place, now its time to write a little code. The most basic command in PHP is the “echo” command. Echo spits out whatever you want to the screen. PHP also has a similar command called “print”, although echo is the faster of the two commands. Whenever you end a line or a command in PHP, you need to end it with a semi-colon. This may be difficult to remember to do at first, but as you get used to it this will become habit. If you start getting errors when you run more advanced scripts, one of the first things you should do is check for a left out semi-colon.
<html>
<head>
<title>A very Simple PHP Script</title>
</head>
<body>
<?php
echo “Does this really work?”;
?>
</body>
</html>
Now save this HTML and upload it to your server. When you save the file, it is important that you save the file as .php, not .html or .htm. The .php at the end of the file will tell the server to look for PHP code on the web page.
Need More Help? Consult our forums.
Check Out the Source Code of the Page
Remember all that talk about server side and client side technology? Well, now you can see it in action. You know what the code of your website actually looks like, take a look at the code when you view the source of your web page. It should look like this:
<html>
<head>
<title>A very Simple PHP Script</title>
</head>
<body>
Does this really work?
</body>
</html>
Notice how you cannot see any of the <?php or echo stuff. The server took this information, executed it, and only returned the results of the PHP code. This becomes extremely important as you start programming much more dynamic content. You will be able to construct an HTML page that search engines love that changes frequently without having to physically change your website at all.
Do Not Be Discouraged
You may be thinking that the script we just wrote is a very small accomplishment. Do not be discouraged at its simplicity. Understanding the basic concepts of PHP is important. Pretty soon we’ll be talking about more complicated concepts such as scalars, arrays, functions, classes, and others. Once you get your mind around these concepts you will really be able to start programming in PHP.
About this author
Mark Daoust is the owner of http://www.Site-Reference.com
Discuss this article further at http://forums.site-reference.com/f14/s.html.
Related articles
The Best and Easiest Google-Friendly Change to Your Web Site
No matter who you are or how much you pay for web site advertising, free search engine traffic is probably responsible for a big part of your business. So why make your web site so hard for search engines to figure out?
Syndication- Another Viral Marketing Tool
Viral marketing has been a major advertising method and syndicating your content is another form of this highly effective concept.
