Why you should be using PHP

Note: This article has been marked for a quality review and will soon be updated.

I would like to introduce you to PHP. If you aren’t already using PHP on your site, you should be! PHP is an easy way of generating dynamic content and interacting with all sorts of things, from the server, to the user, right back to databases. In past tutorials I’ve used PHP without first explaining why, in this article I’ll explain why it’s really great. It’s important to note that you need PHP installed before you start using it, you can do so by either using a server (the easiest, but more expensive way), or by searching Google for “install PHP on OSX/Windows”.

With CSS we can use a small bit of code to link our HTML pages to our style sheets – the meta tag. Well what if you’re building a site and you want the same header on every page, without PHP you would just have to copy and paste the same code onto every page, and imagine if you had 100 pages when you decided you wanted to change a tiny detail. That would be a problem. But we can solve this problem with PHP using a simple require() function. For example…

<?php 
  require("header.php");
?>

The above code simply goes and grabs the file in between the quotations and replaces itself with it’s contents. This can be really useful, and means that you only have to edit the one file to see your changes reflected across your site. But PHP is so much more than just something you can use to insert files, you can use it to do (nearly) anything!

PHP was originally created to produce dynamic content, the PHP code tells the server to do something and returns a HTML value. So say for instance, you may have once visited a site that was displaying the current time or/and date. There are many ways to do this, but one of the easiest, is with PHP. Let me show you.

<?php 
  echo gmdate('d-m-y / H:i'); 
?> 

Would output something like this: 31-01-10 / 19:23. The “echo” command basically produces text and sends it to the browser for rendering, while the gmdate() function gets the current GMT time. The code it encased with the two <?php ?> markers, which tell the server that it should look out for PHP to render. PHP is a powerful language which you can use for a plethora of solutions. My favourite part of PHP is the community. If you want to do something in PHP you will be able to find the solution easily. Furthermore it seems to me like the is a function for everything – want to know when a file was created? There’s mktime() for that. Want to capitalise the first letter of a string? There’s ucwords() for that! Want to make a cup of coffee? There’s makeMeCoffee()! Well that last one may have been a bit of wishful thinking, but there really are a huge amount of functions that you can utilise off the shelf.

PHP is also really useful for security. If you’ve got a password that you want to encrypt, you can use the md5() function, which creates an incredibly secure hash which is nearly impossible to decrypt. Moreover, if you want to ensure only certain visitors can access certain pages, you can use $_SESSION[] variables to store bits of information accessible by any page of your site. Lastly, PHP can be used for databases, there is a detailed explanation of this over in my tutorial on building a CMS, and I believe it is one of the most useful features of PHP.

PHP is a really powerful programming language, and I recommend you learn it. A site I found very useful when I was starting off was Tizag, which will walk you through the basics. And when you’re trying to solve a problem, sooner or later you’ll end up over at PHP.net which is a fantastic resource, despite the slightly 90s look and feel to the site :).

I use PHP all the time, as do solutions such as WordPress and Facebook. It makes my life so much easier and opens up doors to things that I could never achieve without it. I urge you to learn it, and as always the best way to learn is through using the language, so go ahead and create something cool using PHP!


Posted

in

by

Tags:

Comments

2 responses to “Why you should be using PHP”

  1. Some guy avatar
    Some guy

    “If you’ve got a password that you want to encrypt, you can use the md5() function, which creates an incredibly secure hash which is nearly impossible to decrypt.” Actually MD5 is the weakest of the hashing algorithms. It is incredibly insecure. one Google search for md5 reverse look up table can decrypt most hashes. A much better way to secure passwords is to use something like SHA512 with a salt.

    1. Tom avatar

      Actually look up tables don’t dycrypt md5 hash keys, they simply match them against already collected information, for example I can encrypt a string and then conclude the resulting hash is that string in an encrypted state, but I can’t physically dycrypt that hash without a vast array of computers and time. Having said that I agree that sha512 is an excellent alternative, and using a salt is also a good idea.