New tutorials View all
Encapsulation in PHP
Abstract classes in PHP
An introduction to MySQL databases in PHP
Creating a user login and registration system.
First Lesson of HTML
New forum replies
Jim in errorsPaul in errorsJim in Im Stuck On The Member System Tut...scott33 in Im Stuck On The Member System...Jim in Test topicCodefreek in My IRC Bot,Jim in 2.1, likes and dislikes
New frontpage replies
tutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHPtutorials: Creating an IRC bot in PHP
New articles
New news
Learning PHP from scratch PART 1 (89 views)
In this series of tutorials you will start learning PHP. Each month I will add a new chapter to the website, so keep the website in your bookmarks.
In this first chapter I will explain you what PHP is, and how to start with it. At the end of this tutorial you will know how to:
- Create PHP files
- Load PHP files
- Starting PHP
- Create variables
- Use the IF function
Actually this is quite much for a first chapter, but I know that learning a language isn't fun if you can't do nice things fairly quickly. So after some basics will use the IF function for some fun stuff.
How to create PHP files?
Creating a PHP file isn't that hard; just create a text file with .php as extension. What you do with PHP is actually creating a HTML file, but you just post some parts of PHP between the HTML.
What's more interesting to know, is how to edit a PHP file. Well, you can use many editors. I even started coding in plain notepad! But there are editors who are nicer to work with, the best free editor is notepad++, you can get it here.
With Notepad++ you will get syntax highlighting, this basically means the editors will use colors to highlight the PHP code. So a function will get a different color than some plain text, this makes it a lot more easy to read.
How do you load a PHP file?
You need a server with PHP support for this. Most paid host support it, but you won't find PHP on your simple geocities website.
There are two things you can do.
One is installing Apache plus PHP on your home computer, but that can be quite a hassle.
Two is just get a webhost with PHP support, there are plenty of them online. So there is no need to install it on your own computer. I recommend you to get your own domain-name, so you can test all you want.
If you have a host with PHP support you just need to upload the PHP file the server.
Starting PHP
To start telling the server you are going to use PHP isn't that hard. You use the following tags to indicate PHP:
The stuff with the "//" is comment, so no PHP code.
As you can see you only need to write a beginning PHP tag, and a ending tag. You can just put these in your HTML like this:
This makes it quite easy to use PHP!
And check out that part about "echo". This is a function, this tells PHP to put the text between the quotes into the HTML.
So the result of that code would be:
This is what your browser would receive from the server. Quite easy to understand right?
Create variables
Now lets move on to real PHP programming.
PHP uses much temporary information, lets say some tekst you've written in a form is also some temporary information PHP must be able to save. To save this information you need to create a variable. A variable basically is a piece of the computers RAM memory.
Let's see how you create a variable shall we?
First you should notice the dollar sign ($). This is used to let PHP know you are using a variable. Next is the name of the variable, so we've created a variable called "$bike". After the name you write the content of the variable, in this example the variable "$bike" contains the text "Kawasaki";
The difference between text and numbers.
In PHP (and all programming languages) there is quite a difference between numbers and text. First of all in PHP you call a number an "Integer", and you call some text a "string".
Of course this is not the only difference, the main difference is how you use them in PHP. Like you've seen in the example above, to store some text into a variable you need to put quotes around them ("). This is where the difference kicks in, when working with numbers you do not add quotes around them.
So let's say if I want to store my length in a variable I will wont use quotes: (in centimeters of course)
Now what if I want to combine a number with text? Well that's simple, it turns into text. You only omit the quotes when it's a pure number without some text.
Now let's echo the value of the variable into the HTML code.
This looks weird doesn't it?
What I did here is placing the $length variable in the echo. You can do this by closing the text with a quote (") and placing a point followed by the variable.
This is not so hard right? Let's move on to some real programming.
The if function
The most used function within every programming language is the If function, it's a function that compares values.
It looks overwhelming, but believe me it's very easy.
A lot of new stuff, but don't worry you will understand it soon
The first part is if($bike == "Kawasaki"). This part compares "$bike" with the plain text "Kawasaki". So basically it's checks if the value of $bike is "Kawasaki".
Our variable $bike contains the text "Kawasaki", so this check is true. Whit this function, if it's true the code between the accolades ({ .. }) is executed.
So the output of this code is:
The bike is a Kawasaki!
But what if the variable does not contain "Kawasaki"?
Now the if function isn't true anymore. This means that the code between the accolades ({…}) won't be executed.
This piece of code won't return any output, that's kind of ugly. I want to know it when the $bike does not contain "Kawasaki".
To realize this we'll need to add some extra code.
I've added else to the code. The code between the accolades of else is executed when the if is not true.
So in this example the part between else's accolades are executed. Returning the following output:
The bike is not a Kawasaki!
This example should be pretty clear, but I highly recommend you to write your own pieces of code to test all of your new knowledge.
The end
This is the end of part one. In a short time I will write the second chapter of this series. In the next series we'll talk about form input. So all the input that's send via a simple HTML form.
If you have any questions, register an account and post it on the forum. You will have your answer within 24 hours.
- Create PHP files
- Load PHP files
- Starting PHP
- Create variables
- Use the IF function
Actually this is quite much for a first chapter, but I know that learning a language isn't fun if you can't do nice things fairly quickly. So after some basics will use the IF function for some fun stuff.
How to create PHP files?
Creating a PHP file isn't that hard; just create a text file with .php as extension. What you do with PHP is actually creating a HTML file, but you just post some parts of PHP between the HTML.
What's more interesting to know, is how to edit a PHP file. Well, you can use many editors. I even started coding in plain notepad! But there are editors who are nicer to work with, the best free editor is notepad++, you can get it here.
With Notepad++ you will get syntax highlighting, this basically means the editors will use colors to highlight the PHP code. So a function will get a different color than some plain text, this makes it a lot more easy to read.
How do you load a PHP file?
You need a server with PHP support for this. Most paid host support it, but you won't find PHP on your simple geocities website.

There are two things you can do.
One is installing Apache plus PHP on your home computer, but that can be quite a hassle.
Two is just get a webhost with PHP support, there are plenty of them online. So there is no need to install it on your own computer. I recommend you to get your own domain-name, so you can test all you want.
If you have a host with PHP support you just need to upload the PHP file the server.
Starting PHP
To start telling the server you are going to use PHP isn't that hard. You use the following tags to indicate PHP:
| php | |
|
1 2 3 4 5 |
<?php // And here some PHP code ?> |
The stuff with the "//" is comment, so no PHP code.
As you can see you only need to write a beginning PHP tag, and a ending tag. You can just put these in your HTML like this:
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<html> <head> <title>Combined Minds PHP tutorial!</title> </head> <body> <?php echo "Hello world!"; ?> </body> </html> |
This makes it quite easy to use PHP!

And check out that part about "echo". This is a function, this tells PHP to put the text between the quotes into the HTML.
So the result of that code would be:
| php | |
|
1 2 3 4 5 6 7 8 9 10 |
<html> <head> <title>Combined Minds PHP tutorial!</title> </head> <body> Hello world! </body> </html> |
This is what your browser would receive from the server. Quite easy to understand right?
Create variables
Now lets move on to real PHP programming.
PHP uses much temporary information, lets say some tekst you've written in a form is also some temporary information PHP must be able to save. To save this information you need to create a variable. A variable basically is a piece of the computers RAM memory.
Let's see how you create a variable shall we?
| php | |
|
1 2 3 4 5 |
<?php $bike = "Kawasaki"; ?> |
First you should notice the dollar sign ($). This is used to let PHP know you are using a variable. Next is the name of the variable, so we've created a variable called "$bike". After the name you write the content of the variable, in this example the variable "$bike" contains the text "Kawasaki";
The difference between text and numbers.
In PHP (and all programming languages) there is quite a difference between numbers and text. First of all in PHP you call a number an "Integer", and you call some text a "string".
Of course this is not the only difference, the main difference is how you use them in PHP. Like you've seen in the example above, to store some text into a variable you need to put quotes around them ("). This is where the difference kicks in, when working with numbers you do not add quotes around them.
So let's say if I want to store my length in a variable I will wont use quotes: (in centimeters of course)
| php | |
|
1 2 3 4 5 |
<?php $length = 172; ?> |
Now what if I want to combine a number with text? Well that's simple, it turns into text. You only omit the quotes when it's a pure number without some text.
| php | |
|
1 2 3 4 5 6 7 |
<?php $length = 172 cm; // Wrong $length = "172 cm"; // Right ?> |
Now let's echo the value of the variable into the HTML code.
| php | |
|
1 2 3 4 5 6 7 |
<?php $length = "172 cm"; echo "My length is: " . $length; ?> |
This looks weird doesn't it?
What I did here is placing the $length variable in the echo. You can do this by closing the text with a quote (") and placing a point followed by the variable.
This is not so hard right? Let's move on to some real programming.
The if function
The most used function within every programming language is the If function, it's a function that compares values.
It looks overwhelming, but believe me it's very easy.
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $bike = "Kawasaki"; if($bike == "Kawsaki") { echo "The bike is a Kawasaki!"; } ?> |
A lot of new stuff, but don't worry you will understand it soon

The first part is if($bike == "Kawasaki"). This part compares "$bike" with the plain text "Kawasaki". So basically it's checks if the value of $bike is "Kawasaki".
Our variable $bike contains the text "Kawasaki", so this check is true. Whit this function, if it's true the code between the accolades ({ .. }) is executed.
So the output of this code is:
The bike is a Kawasaki!
But what if the variable does not contain "Kawasaki"?
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $bike = "Honda"; if($bike == "Kawsaki") { echo "The bike is a Kawasaki!"; } ?> |
Now the if function isn't true anymore. This means that the code between the accolades ({…}) won't be executed.
This piece of code won't return any output, that's kind of ugly. I want to know it when the $bike does not contain "Kawasaki".
To realize this we'll need to add some extra code.
| php | |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
I've added else to the code. The code between the accolades of else is executed when the if is not true.
So in this example the part between else's accolades are executed. Returning the following output:
The bike is not a Kawasaki!
This example should be pretty clear, but I highly recommend you to write your own pieces of code to test all of your new knowledge.
The end
This is the end of part one. In a short time I will write the second chapter of this series. In the next series we'll talk about form input. So all the input that's send via a simple HTML form.

If you have any questions, register an account and post it on the forum. You will have your answer within 24 hours.

Replies on Learning PHP from scratch PART 1:
Jump to comment page: 1
There are no replies for this tutorial. Are you going to be the first?
Jump to comment page: 1
You are not logged in. Please login or register an account, it just takes 30 seconds.
