Mini PHP Crash Course

A graphical despiction of a very simple html d...

Image via Wikipedia

If you’re a Web coding geek like me, you’ve probably mastered (X)HTML, used some CSS and JavaScript — and you’re looking for the next step up. You may have realized that PHP programming is an important language to learn. (Note: If this alphabet soup is Greek to you, go back to (X)HTML 101 before continuing.) Instead of being a rather simple markup or scripting language, PHP can be the foundation for some amazing Web applications.

So you wanna’ try it yourself, eh? Good news: It’s not too hard to learn the basics. Here’s a super-quick tutorial to get you rolling. Fire up a simple text editor like Notepad, or a code editor such as  SciTE (free), and get ready for your first taste of PHP-coding goodness!

PHP stands for “PHP: Hypertext Preprocessor.” (Yeah, codeheads can be odd.) It’s written inside  <?php and  ?> tags, as you’ll see below. One way to keep the code easy-to-read is to put these tags on their own line. Paste the following code into the body of a regular HTML document:
<?php
echo “Spiffiness Ensues”;
?>

The code can also be written in a single line, e.g.,
<?php  echo “Spiffiness Ensues”; ?>

Echo basically means “print this”. Don’t forget the semicolon(;) at the end of the line, outside the quotes. PHP is very picky about this kind of thing.

PHP statements play nice with HTML code on the same line. For example, if you want to print the title of your webpage using PHP, you can write:
<title><?php echo “Spiffiness Ensues”; ?></title>

… and the browser will happily oblige you.

Now, save your document as a .PHP file — that is, a text file with a .PHP extension, e.g., mypage.php . Then open the file in your local Web browser. You should see… nothing. Nope, sorry, it won’t work that way. PHP needs to be installed on your computer or server for it to work right, which is a whole new can of worms.

Webpage, PHP, and source

Basic PHP page, with the original PHP (bottom) and the HTML the browser created from it.

What’s a poor code monkey to do? One option is simply to upload it onto a server that has PHP installed, as most do. (If it doesn’t, you need to find one that does before you can publish your PHP files, anyway… A couple good choices are Hostgator or Startlogic.) Just upload it to the root directory, or wherever you fancy. When you type in the address, e.g., http://www._____.com/mypage.php , it should show a page with “Spiffiness Ensues” written near the top. Check out the example on the right. Ta-da!

The beauty of PHP is that anything you write gets processed into straight HTML before it reaches the browser. For the example above, for, uh, example, the HTML+PHP line becomes:
<title>Spiffiness Ensues</title>

That means that both search engines and users see a single, integrated webpage, in spite of the fact that the page itself may be composed of two or more PHP, HTML, and other files. (On the downside, it’s tricker to learn techniques of other PHP coders from studying their webpages’ source code, but that only affects true propheads.)

For some more basic PHP guidance, check out the comprehensive resource at PHP.net. Some good items to start with are echo, if statements, for loops, and the very versatile include/require. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *