When I asked in the past if our readers would be interested in learning some programming concepts the vast majority said yes, especially if the concepts were focused around the PHP language. Recently I started playing around with PHP myself, so I decided to create a series of tutorials sharing the stuff. If you like this first part please let me know so I keep more coming.
Speaking of which, why should you learn PHP and not one of those other scripting languages I mentioned? There are three main reasons:
1. PHP is the only language that was specifically designed to create dynamic web pages, as such you’ll find that it’s much easier to get started and to accomplish basic tasks (e.g., to parse data from web forms, to send emails, to manage cookies and so on) with PHP.
2. Most hosting plans these days already come with PHP installed, so you won’t need to worry about getting the PHP interpreter up and running. You just need to create your scripts and away you go.
3. There’s a huge community of PHP users as well as a huge number of important online projects running on PHP (e.g., WordPress, vBulletin). So you’ll be able to find help as well as examples.
Now don’t get the wrong idea. I am not saying PHP is better than the other languages. Ruby from what I heard is a fantastic language as well, and the Ruby on Rails framework is one of the most advanced these days. Same goes for Python, which I used when I was starting to code a couple of years ago and I still love it due to its simplicity and efficiency.
All I am saying is that PHP is the easier one to get started, and if you just want to implement some basic online projects it will probably be all you need to learn.
Running Your PHP ProgramsBefore you can run your PHP scripts you need install the PHP interpreter on your machine. If you are using Windows check the installation manual here. If you are running Linux all you have to do is to type the following on the terminal:
sudo apt-get install php5And they say it’s complicated to do things on Linux…
If you have a hosting plan it’s very likely that your server already has a PHP interpreter installed, so you can also run your scripts online. In this case you just need to upload a file with the .php extension via ftp and then browse to that file with your browser.
If you are running your scripts on your Linux machine then save your file as .php and then run it as:
php file.phpWindows users can check how to run PHP scripts here.
Your First Program: Hello WorldThe first program that you’ll run on probably all languages you learn is a simple one to display the “Hello World” message on the screen. In PHP it looks like this:
<?phpEvery PHP script starts with <?php and ends with ?>. Echo is a language construct used to output strings. Notice that every statement in PHP must end with a semicolon. If you forget it your scripts won’t run.
echo "Hello World";
PHP StringsA string is nothing more than a series of characters, and it’s one of PHP’s eight primitive types (I’ll cover the remaining ones later on).
Since web pages involve mostly text you’ll be using PHP strings a lot.
Strings in PHP can be represented with either single or double quotation marks. So “Hello World” is pretty much equivalent to ‘Hello World’. There are two main differences, though, explained later on.
Before explaining the differences we need to talk about escape sequences. A escape sequence is a combination of characters to produce another one in special circumstances). For example, let’s say you want to print the follow sentence with a PHP script: “This is Peter’s car.”
If you write your script like this:
<?phpYou’ll get an error, because the PHP interpreter will assume that the string finished right after Peter. If you want to make that apostrophe part of the string you need to use the escape sequence \’ (most escape sequences begin with a backslash).
echo 'This is Peter's car';
<?phpThe code above works fine. An alternative is to alternate double with single quotation marks. So:
echo 'This is Peter\'s car';
<?phpwould also work, as would this:
echo "This is Peter's car";
<?phpOther common escape sequences are \n to add a newline, \t to add a tab and \\ for the backslash itself. In fact from now on I’ll add the \n escape sequence to the end of all strings to make sure we print a newline after them, which makes reading the output easier.
echo 'The "Lord of the Rings" book is very long.';
Now this is the first difference between the types of strings: strings with single quotation marks only support the \’ escape sequence, while strings with double quotation marks support all of them.
The second difference is that strings with double quotation marks expand variables inside it. I’ll explain variables below and then return to this point.
PHP VariablesA variable can be seen as a container for some data. Variables in PHP start with the $ sign. Check the following script:
<?phpWe basically created two variables, assigned a value to each of them, and then printed the sum on the screen.
$x = 10;
$y = 20;
echo $x + $y;
PHP is a dynamically typed language, which means that it’s variables don’t have any specific type. This means that the same variable can start holding an integer, and later on you can make it hold a string, for example.
Mixing Variables and StringsAs I mentioned before, double quoted strings expand variables inside them. Take a look at the code below:
<?phpThe output is “Hello World”, because the variable gets expanded inside the string. Even variables holding integers will be expanded on the string.
$var = "World";
echo "Hello $var\n";
String ConcatenationAnother important point is string concatenation. Often times you’ll need to concatenate strings together, and the easiest way of doing it is with a dot. For instance:
echo "Hello" . "World\n";
<?phpThe above code first adds x with y, then converts the result into a string, and then concatenate it with the “\n” string, effectively printing out 30 with a newline after it.
$x = 10;
$y = 20;
echo $x + $y . "\n";
PHP comes with a huge amount of built-in string functions. You can check all them on the official PHP manual. For instance, there are functions to find the length of a string, to search for specific characters inside it, and pretty much anything else you might need.
On the next part of this tutorial I’ll talk about operators and control flow mechanisms.