PHP Tutorials

Tutorial 2 - Basic PHP

This tutorial will help you to start writing basic PHP scripts. Before we begin properly, you must ensure that your web host has PHP set-up. This is needed if you want to create your own PHP scripts or if you want to follow these tutorials by testing out the code yourself. To make sure your script runs properly, you must save the file as filename.php - this ".php" ending is important, and needed, to run PHP (although obviously replace filename with the name of the file). If your host doesn't support PHP, you can set-up your own personal hosting environment on your computer using a program like Xampp. Xampp comes with PHP support already built in.


Quick Reference:
Starting a PHP Script
Comments
"echo" - i.e. Outputting Content

Starting a PHP Script Top

In order to start a PHP script, you will always need to open and close it with specific tags - "" to close the script. These tags signify to the server that what's inside the PHP tags needs to be parsed (i.e. deal with/process) before the output is sent (in HTML) to the user.


<?php

// This is a comment. Everything inside the PHP tags are always processed, unless they are in a comment like this.
// A new line, hence another "//" is needed to signify the start of a comment.

/* This is another comment. Notice, though:

This comment uses "/*" to start the comment, instead of "//"

This comment also won't automatically finish after a new line (as with the "//" comment). It only finishes using the following closing tag:
*/

?>

Comments Top

As can be seen above, there are orange parts of the script - these are called comments. A comment is completely ignored when the PHP engine comes to parse (or process) the PHP code. What's the point of these, then? Mainly it's to leave notes to yourself whilst writing the script. These notes may be to remind you that you need to do somthing later on in the script, or they may be to explain how a particular bit of code works (especially useful when the bit of code is quite complex). They are also, as a result, helpful if you are showing your script to another PHP coder - that other person will be able to follow the code more easily.

As shown above, there are two ways to put a comment into a PHP script:

"echo" - i.e. Outputting Content Top

The most common way in PHP to output content to the client is using "echo". echo is a language construct, meaning that it is an integral part of PHP itself. In other words, they are like the building blocks of PHP. As such, it works in a different way to many other parts of PHP. There are other language constructs, which will be brought in at later stages. Despite this (i.e. echo being a language construct), I mostly always call it the echo command or echo function, although be wary that this isn't strictly correct. Below are examples of how the PHP echo command can be used:


<?php

echo 'Hello World!';
// This would output Hello World! to the client. The quotation marks signal the start and the end of the echo command (hence everything between the quotation marks are output).
// The semi-colan (;) at the end of the echo command is used to signify the end of the echo statement. If the semi-colan wasn't there, PHP would try to output everything after the echo statement as well, which would lead to PHP breaking and an error being shown.

echo "Hello World!";
// This would also output Hello World! to the client, despite using double quotation marks instead of single quotation marks. For basic strings it doesn't matter whether you use double quotation marks or single quotation marks for an echo.

echo 20+17;
// This would output 37. Yep, the echo command (and PHP in general) can be used for arithmetic! Note that there aren't any quotation marks used here.

echo 20*3;
// This would output 60 (* means multiplied).

echo '20+17';
// This would literally output 20+17. Because quotation marks were used, this was processed as a string instead of a mathematical operation. This would happen whether double or single quotation marks are used.

echo 'PHP Mission is great, it's my favourite website!';
// Oh no. The pretty colours have broken... but nothing'
s changed, right? Wrong! See below:

?>

As can be seen, the echo command is fairly flexible. It can even do maths (Wink). However it's not infallible. In fact PHP is very delicate! As you can see above, the final echo command broken. Can you figure out why? If not, look at where the red PHP code (signalling a echo command) stopped and the blue PHP code (signalling normal PHP code) started. Noticed it?

The echo command in the final example didn't work because of the apostrophe (the single quotation mark) in the middle of the sentence. This would actually break PHP, and cause an error (called a parse error) to be shown. Obviously this doesn't mean that you can't use quotation marks in an echo command, though. To bypass this error, you have to "escape" the apostrophe. This is done by putting a backlash (\) before the quotation mark. This then tells PHP (i.e. the PHP engine) to deal with the apostrophe literally, and not to interpret it as the end of the echo statement.


<?php

echo 'PHP Mission is great, it\'s my favourite website!';
// This would output: PHP Mission is great, it's my favourite website!
// Notice that the apostrophe has been "escaped", meaning that the code won't break!

echo "And then I said, \"PHP Mission is great!\"";
// This would output: And then I said, "PHP Mission is great!"
// Notice that this time, because double quotation marks were used to open and close the echo command, any double quotation marks needed to be escaped.

echo 'He then asked, \"So does anyone have any questions?\"';
// This would literally output: He then asked, \"So does anyone have any questions?\"
// Obviously the backlashes aren't meant to be output, so why have they been?
/* Confusing I know, although simply put, you don't need to "escape" quotation marks of the opposite type to what you used to open and close the echo command.

This means that if you used single quotation marks for an echo query (e.g. echo 'Hello World';) you wouldn't need to escape any double quotation marks within this echo command. The reverse is also true: */

echo "His car's are amazing!";
// This would output: His car's are amazing!
// Notice that the apostrophe didn't need to be escaped. This is because the echo statement was opened and closed with double quotation marks.

?>

Hopefully this basic PHP tutorial has helped you to understand PHP a bit more. If possible, try and write a few basic PHP scripts yourself. Go exploring the great landscape that is PHP!