PHP 101-Learn the Very Basics of PHP

The Big Prologue

The whole purpose of this little tutorial is simply to get you started with PHP. I am assuming a lot of things. For one, I assume you are familiar with building web pages in HTML and using some kind of FTP client. These things are pretty much necessary if you want to tackle a serverside scripting language.

FatCow $88 Plan for $44 only
If you do know HTML and can wield an FTP editor and even notepad you will do alright. PHP basics really are not that incredibly hard to pick up. As with just about every other programming or scripting tutorial in the world, I will follow tradition by opening things with a "Hello World!" example. Now doesn't that sound like fun?!

My Hello World Example

I did mention that I'm assuming you know HTML already right? If you don't, feel free to read along anyway if you wish. The following sentence is my official disclaimer: The HTML you see in these examples may or may not be the right way to use HTML as I'm a bit too lazy in some cases to make sure my example markup is totally valid. I will try to control myself where valid markup is concerned so I don't somehow distort somebody's knowledge of HTML(or XHTML if you want to get technical).

This is html<br />
<?php
//This is PHP
echo "Hello Weirdo!<br />";
//This is the end of PHP
?>
this is html again<p>

Well then...you've read three paragraphs and learned nothing. Let's start with the code on the right. It's a bizarre mix of PHP and HTML. Cool huh?

A file with the extension .PHP or something of the like is nothing more than a glorified HTML file. By glorified I mean that a server with PHP will almost always be set up to look for PHP code in files with these extensions. PHP code can mix within HTML markup. Special tags mark the beginning and ending of PHP code in the HTML. The most common tags are <?php /*This is a PHP comment*/ ?>. These are my personal favorite and what I will use throughout this tutorial. You may also see <? /*This is a PHP comment*/ ?> or <% /*This is a PHP comment*/ %> sometimes.

You may have noticed my last second deviation from the standard "Hello World!" script. What can I say? I'm an individual after all. Back to business...OK so you can mix PHP up in HTML as long as it's in a file that your server looks in for PHP code. You could do something like the following if you want...

This <?php echo "is"; ?> a <?php echo "sentence"; ?>.
Man <?php echo "I'm"; ?> brilliant!


If you do the above, however, don't blame me when people start flinging random objects at your head. Take special notice of the word echo in the above code. This is an easy way for you to output HTML from PHP. Take note of the quotation marks around the the words we want to output also. These tell PHP that everything within the quotation marks is part of what you want PHP to output. Letters, numbers and other characters grouped together like this are called strings.

You should also take note of the ";" at the end of each line. The PHP parser needs this to tell it where statements end or it will try to read them as one statement which really makes for some nifty errors. This can be a very common mistake for the beginner.

You can also encase the words in single quotes (or apostrophies if you want to get technical) but there is a difference in the way that PHP handles the string if you do this. We will get to that in the next chunk of this tutorial when we go over variables.

Continue on to PHP Variables Primer.

Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Leave a Reply:

Name (required):
Mail (will not be published) (required):
Website:
Comment (required):