Home of all things tech!
PHP – The Basics

PHP Logo
PHP has grown from nothing to become one of the most popular languages on the web. This is mainly because of it’s simple syntax, easy usage, and seamless integration with MySQL and other databases. It’s also free, which makes it a great tool for developers when designing interactive and
dynamic content. In fact, my blog is powered by the open-source software WordPress.org, which is written in PHP.
What is this ‘PHP’ thing?
You may be thinking “wow, it sounds great, but what is it?”. So here are a few points about PHP:
- PHP stands for PHP: Hypertext Preprocessor (It’s a bit of a paradoxical name if you ask me!)
- PHP is a server-side scripting language, which means the scripts are executed on the server – before the user sees any output. (This is similar to ASP.net)
- The opposite of server-side is client-side. JavaScript is a client-side scripting language.
- Although PHP is free and open source, you need a server to run PHP, you can’t just access it in your browser. Common servers include Apache and Microsoft’s IIS.
- PHP files have the extension .php / .php3 / .phtml
What do I need?
To be honest, you don’t really need much at all! A simple text editor such as NotePad or TextEdit will suffice. I use NotePad++ for various reasons, outlined in my review, but mainly because of it’s syntax highlighting. You could also try NotePad2 for a more lightweight option.
Also, as I mentioned earlier, you need a webserver to parse the script. This means you will need to host your files somewhere. This could be locally, using a package such as XAMPP, or you could buy hosting, but make sure they offer PHP support. (Most hosts will, as PHP is free and open source.)
Got all that? Good, let’s start writing some code!
Writing your PHP
PHP has a very simple syntax, and is written directly into your HTML, in blocks of code. A block starts with…
<?php
…and ends with…
?>
Inside the PHP block, you put your code. Here is a simple snippet which writes the phrase “Hello World” into the HTML page.
<html> <body> <?php echo "Hello World"; ?> </body> </html>
Notice that the “echo” statement ends with a semi-colon, if I had forgotten this, the script would have triggered a syntax error. Every statement must end with a semi-colon.
I could also add a comment into this script, to help people understand what the script is doing. Comments can be written on one line by using //, or on multiple lines using /* and */, like so…
<html> <body> <?php /* This script writes 'Hello World' to the document. This was written by MattyBatty. This is written in PHP. This is a comment. */ echo "Hello World"; //Writes 'Hello World' ?> </body> </html>
Variables, Strings and Operators!
Variables is the general name for Text Strings, Numbers and Arrays. All variables in PHP are accessed with the $ symbol. For example, to assign the string “Hello” to the variable $greeting, I would do this:
<?php $greeting = "Hello"; ?>
Make sure you remember the quotation marks, this is what tells PHP it is a string. Once it’s set, you can then access this variable and echo it like this:
<?php echo $greeting; ?>
See, it’s simple really! Just like the “Hello World” example, but we replace the text with a variable name. It’s almost identical for numbers too, just one tiny difference. Numbers don’t need quotation marks when assigning them. So if I wanted to assign the number ’10′ to $number, this is what I would write:
<?php $number = 10; ?>
Got it? You’re almost there, there are just a few rules you should be aware of:
- Variable names can only contain letters, numbers, and underscores.
- Variable names must start with a letter or an underscore, not a number!
- No spaces allowed in the name, use underscores or capitalisation instead. (eg. $variable_name or $variableName.)
Well done, you now know how to assign and echo variables, but what’s the fun in that, you want to do something with your variable! There are several hundred functions and tricks you could do with them. I’ll just teach you the most important ones.
You can join strings together using the – wait for it – concatenation operator (full stop) like so…
<?php $string1 = "Hello"; $string2 = "World"; $string3 = $string1 . " " . $string2; //$string3 is now "Hello World" ?>
Another important function is strlen(). This returns the length of the string. For example, strlen(“MattyBatty”) would return ’10′.
There are also lots of operators for numbers. These are the main ones…
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
For example, to divide 10 by 2, I would do the following:
<?php $result = 10/2; echo $result; //This would echo '5' ?>
So… that’s it, you’ve reached the end of this brief introduction to PHP. Come back next week for another PHP tutorial in which we will be discovering arrays, loops, if… else… statements and more. Thanks for reading!
P.S. To make sure you don’t miss the next one, why not subscribe to my RSS feed!
| Print article | This entry was posted by MattyBatty on July 24, 2009 at 7:52 pm, and is filed under Tutorials. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 2 years ago
Cool! I think I will be folowing this and learning PHP.
about 2 years ago
Phahh.. you won’t learn PHP, you can just get me to do it
about 2 years ago
Remember to check the *new* upcoming posts widget to see when the next tutorial is out!
about 2 years ago
Not my thing MattyBatty but great advice nevertheless!
about 2 years ago
Nice. I can understand all this, its easy!
about 2 years ago
Thanks Mr. Blobby
about 2 years ago
Do you teach HTML too because i need to link some of my website customers to where they can learn HTML and i’m thinking of linking to you.
about 1 year ago
Yeah, I can definitely teach HTML, and CSS too if you need.