PHP: A Quick Tutorial
If you're designing a web site, you'll likely have run across the numerous technologies available. From Perl to HTML to CSS, there are almost more languages than sites to design. However, despite all the esoteric platforms out there, PHP is very useful when designing a site on the Linux platform. In fact, PHP enables web applications to read from databases, handle multiple users, and generate Captcha codes.
Many web developers who are new tend to have difficulty ascertaining when a certain technology should be used. Remember that PHP and other scripting languages were built to address the limitations of HTML. By nature, HTML can only deliver static content. It cannot work with databases, nor can it manage users. PHP was developed to provide web developers with the ability to accomplish tasks on the server end. For example, with PHP, one could create a web site that could record the number of hits each page received. Or one could write a web page that returned the date and time. Such tasks were previously impossible with strict HTML.
Learning PHP is not hard considering it's a reasonably straightforward language that doesn't contain many subtleties. For example, many other languages require variables to have a specific type (integer, float, and so on). PHP doesn't have this for reasons of simplicity. As well, other languages have complex concepts like classes, polymorphism, and so on. While PHP does have these, it is rarely necessary that you use them. Most simple PHP applications, such as a page that reads from a database, never use anything above simple logical sequences.
If you're looking to learn PHP, the best way is to see a simple application. Consider the following snippet of code:
$add = 1 + 2;
echo $add." should be three.";
?>
Let's break this application down.
- The first line <?php tells the server that it should treat whatever is between the <?php and ?> as PHP code. That is, don't serve it as regular HTML, but instead invoke PHP to interpret it.
- The second line declares a variable named $add. In programming, a variable is a space in memory that holds some value. The equals sign tells PHP to assign the value of the right-hand side to the variable $add. Here, $add will have the value of 3, which is equal to 1 + 2.
- The third line introduces a command called echo, which prints the contents after the command to the web page. Quotation marks mean the inner text is to be taken literally. The period between $add and " should be three." acts as a join. Consequently, the text printed will be the value of $add (which is 3) followed immediately by should be three.
- The last line tells the server that the PHP code is finished.
So the final output of this program is simply:
That wasn't so hard was it? The lines of code were meticulously broken down for learning purposes. However, coding in PHP will just become second nature with time. Try modifying the program so that it prints the text 5 should be five. Making little changes to small tutorial applications will help you greatly understand the workings of PHP.
If you're interested in learning more about this language, a simple search for "PHP tutorial" in your favorite search engine will reveal many tutorials that will help you explore the language more in-depth. PHP is very versatile and chances are you'll find that learning it will improve your development opportunities.