Get Latest Tricks Alert Daily
Follow Readers

PHP Tutorial For Beginners!


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.

What’s PHP, and why should I care?

PHP is a programming language that was developed to to produce dynamic web pages. Technically speaking it’s a server-side scripting language. Server-side means that it works on the server, as opposed to stuff that runs on the client (i.e., the browser), like JavaScript or Java applets. Scripting means that PHP is an interpreted language used mostly to create dynamic web pages, as opposed to a compiled language like C or C++ which are used to create complete applications. Other scripting languages popular in web development are Perl, Python and Ruby
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 Programs

Before 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 php5
And 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.php
Windows users can check how to run PHP scripts here.

Your First Program: Hello World

The 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:

echo "Hello World";

Every 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.

PHP Strings

A 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:

echo 'This is Peter's car';

You’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';

The code above works fine. An alternative is to alternate double with single quotation marks. So:

echo "This is Peter's car";

would also work, as would this:

echo 'The "Lord of the Rings" book is very long.';

Other 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.
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 Variables

A variable can be seen as a container for some data. Variables in PHP start with the $ sign. Check the following script:

$x = 10;

$y = 20;

echo $x + $y;

We basically created two variables, assigned a value to each of them, and then printed the sum on the screen.
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 Strings

As I mentioned before, double quoted strings expand variables inside them. Take a look at the code below:

$var = "World";

echo "Hello $var\n";

The output is “Hello World”, because the variable gets expanded inside the string. Even variables holding integers will be expanded on the string.

String Concatenation

Another 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";


$x = 10;

$y = 20;

echo $x + $y . "\n";

The 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.
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.


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to our regular Email Updates!

Subscribe To Get Latest Tricks On Email!



  1. I really appreciate you that you shared these tutorials for beginners which is very useful for them to learn basic things of PHP Development Services.

  2. Great post indeed and thanks for all the information, it was very helpful i really like that you are providing information on PHP and MYSQL with basic JAVASCRIPT,being enrolled in i was looking for such information online to assist me on php and mysql and your information helped me a lot. Thanks.

  3. very informative articular, Thanks for sharing this