PHP Tutorial For Beginners – Part 1
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?
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:
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.
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.
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.
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.
Browse all articles on the Web Tools category
12 Responses to “PHP Tutorial For Beginners – Part 1”
Just to clarify, you should tell your readers that using ‘apt-get install’ will only work on Debian based systems, like Ubuntu for example. On other server-side systems like CentOS, Fedora and all RedHat based distributions, you can use ‘yum install’ to install stuff.
There are two packages which set up an Apache server with MySQL, PHP already installed.
Effectively you had a server on your desktop and can run PHP pages or play around with HTML, customising a WordPress theme, learn how to work with databases etc. (Though you do have to manually install WordPress – not to difficult)
The advantage is that you can make changes to a script and then get the result almost instantly.
The two are WAMP and XAMPP, (Just google the words). WAMP is slightly easier to use.
I particularly recommend these if you want to play around with themes or alter php based software.
Daniel, I’ve been blogging for five years and I couldn’t wait for something like this to come out! Thank you so much! Everyone seems to forget about programming newbies and even though I have a blog I always have to search for people to help me out with some technical things. I hope to solve at least some portion of the problem with your tutorial.
Yes, as Raj says, don’t get blown away, keep it simple! 😉
Keep up the great work!
good…you have started this tutorial easy way. i will follow it
one request: plz go slow and short on posting so that i can absorb things better.
It is great to see you have started PHP series. I have always wanted to learn this. The great thing is that you have explained everything in an easy way.
One thing I would like to see is PHP tutorials geared more towards building web applications. Most of these are pure basics. I think it would be great to see some tutorials about building applications once basics are laid out.
Leif G.S. Notae
Oh man, I so need to php well and yet, I can’t seem to wrap my head around it. Thanks for the tips, I’ll keep my eye out for the next installment and thanks for sharing!
Wondeful begginers oriented tutorial. Looking forward for Part 2. Make sure you don’t miss ‘Regular Expressions’
I wonder you haven’t specified the full form of PHP (Hypertext Pre Processor) here!!
// Look I have started using your tutorial 😉
Daniel, really your first part on PHP is lot simpler (even non-techie people also can understand it easily!!) Looking forward for your next part on PHP 🙂
I believe We can all benefit from learning PHP.
Part 2 is going to be more fun!
I am looking forward to the rest of this series. I know a bit of php, but I am all self-taught, and have never read a book or anything on it. What I have learned, I have learned by looking at WordPress code.
So I know I have LOTS of holes in my knowledge of how PHP works. Thanks for writing this series!
Mike From Maine
I know just enough php to make some minor changes on my wordpress themes….I leave that stuff to my outsourcers.
Comments are closed.