Introduction to PHP
Jun 5
Well, I’ve decided that my first actual tutorial should be a little beginner’s introduction to PHP: Hypertext Preprocessor, one of the most popular open source programming languages around. So, here we go!
OK, PHP is basically a programming language designed primarily for web applications and databases. That’s why PHP has complete capability of being embedded into HTML documents.
I’m assuming here and for the rest of my PHP tutorials that you have a server and your server has PHP installed and working properly. When I feel like it I’ll write a separate tutorial on actually getting a server (Apache) installed on your home PC, and setting up PHP to work with it.
Anyway, let’s begin!
First, consider a normal HTML document:
Example 1: a simple HTML document
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Hello world!</title> </head> <body> <p>Hello world!</p> </body> </html>
You can see here it’s a really simple HTML page: with a page title and a little paragraph, both shouting out loud our beloved “Hello World!”. No PHP. Where do we put it then? Well, anywhere in our HTML!
Example 2: PHP in HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Hello world!</title> </head> <body> <p>This paragraph was written in plain HTML.</p> <p><?php echo 'This paragraph was written with PHP!'; ?></p> </body> </html>
See the magic? We’ve changed the first paragraph slightly, and added a new paragraph. What’s this? <?php and ?> tags?
Well, these are what you call delimiters. Delimiters basically tell the server which code to call PHP code, which to call HTML code. All PHP code will be handed to PHP to be processed. <?php (or, commonly, just <? like XML’s processing instructions) tells the server whatever code that follows it is PHP. ?> tells the server to stop treating code as PHP until the next opening delimiter.
The above example, when viewed in a browser, will look like this:
Example 3: after PHP is done with the PHP code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Hello world!</title> </head> <body> <p>This paragraph was written in plain HTML.</p> <p>This paragraph was written with PHP!</p> </body> </html>
When the server sees <?php, it passes the code to PHP. PHP will then attempt to parse, which means to run through and handle the code, until it sees ?>, when it returns the result to the server. When the page is done, the server passes the script back to your browser for display.
Now, I hear you ask: what’s that word ‘echo’ doing there? echo() is a basic PHP command that tells PHP to output the text, which means to display or write it, to return to the server then the browser. The text enclosed in ‘quotes’ is called a string. A string is just a bunch of text together.
See that semicolon (;) there? That semicolon is fundamental in PHP coding. If you recall your English grammar, you’ll understand that the semicolon is used to end statements, besides the period (.). It’s the same in PHP: a semicolon ends a statement so PHP can proceed to parse the next statement of code.
OK, let’s go slightly further.
Example 4.1: a variable with its value
<?php $variable = 'Hello world!'; echo $variable; ?>
That word preceded by a dollar sign ($) is a variable. If you recall your algebra lessons, you’ll know that variables are just things that can hold any certain numbers:
Example 4.2: elementary school math
x + y = 10 2y = 6
x should be 7 and y should be 3. If we don’t know what the value is, we just place a variable there, telling ourselves that we don’t know. PHP variables are also things that hold certain values (strings, numbers or arrays which we will talk about some other time).
In the above example (4.1), PHP should create a variable called variable and give it the value of the string Hello World!. The next thing it should do is to echo that variable, which — I’ve just explained — means display.
You can do whatever you want to a variable, and you can give variables any names, except for a few limitations:
- Only letters, numbers and underscores (_) are allowed: $My_Var_Of_Doom12345
- The name must not start with a number: not $666Lolz but $AnOKVarName1
Don’t ask me why the second factor because I’m not exactly sure either. But for the first rule, we can’t even use dashes because PHP treats those as minus signs. And PHP doesn’t quite well support Unicode (UTF-8 and UTF-16) yet, though the majestic PHP 6.0 will.
I forgot to mention that the current versions of PHP that are widely used are PHP 4 and PHP 5. The latest stable versions at the time of writing this are PHP 4.4.7 and PHP 5.2.3 respectively. PHP 6 is still under development but should be out in a couple years or so.
If you’re giving a variable a numerical value, don’t give the number any quotes!
Example 5: numerical value
<?php // These two variables are numbers. $foo = 1; $bar = 2; /* By enclosing a number in quotes, you're turning it into a string! */ $baz = '3'; ?>
See the slashes and asterisks all around? Let me introduce PHP’s comments to you!
If you haven’t done programming before, comments are just what they are: comments. Comments about the code, comments about yourself, comments about PHP, comments about other comments, whatever, but they all get completely ignored by PHP. That means you can say what you want: PHP doesn’t care.
Comments are extremely useful in helping your friends and even yourself by explaining what the hell’s going on.
Comments are also pretty useful to tell PHP not to run a certain command. For example, you could do this to tell PHP not to echo $hello:
Example 6: don’t say anything!
<?php $hello = 'world'; # Don't echo. Keep quiet. Stay still. # echo $hello; ?>
That pound (#) sign is an example of PHP’s notoriety of taking inspiration off other programming languages. This one’s from shell.
Where did PHP get // and /* */ comments then?
C.
// and # are [b]single-line[/b] comments. That means everything after either of those two will get ignored by PHP until the line ends.
Example 7: a multi-line comment
<?php /* This is a multi-line comment! It can span as many lines as you like; all will get completely ignored by PHP. They invented multi-line comments because they didn't want anybody to... */ // Abuse // Single-line # comments # like // --------------- this! --------------- // // ------------------------------------- // /* and to provide some extra convenience too :) */ ?>
I’m getting tired. I think that’s all for the basics of PHP.
I’ll continue writing tutorials like these. I’m beginning to enjoy them, actually!
I’ll see you again. This is my first PHP tutorial so please leave comments
Enjoy PHP!