October 2, 2011 learn_php

Learn basics for PHP

Learn basics for PHP.


I remember when I was a kid, and used to surf the internet for games, awesome graphics, videos(back then with 24-56k modems, only 10 second videos were available) and even chat online. I started having interest on HTML and started learning how to make websites. I would see how some websites would have dynamic data, and always wondered, “How in the world can a website do that?” Of course, with the HTML mentality, it would be difficult to understand. It wasn’t till my third year in college, in which I finally saw the light and was exposed to what we all know as PHP.

PHP stands for: PHP Hypertext Preprocessor. Some people have a debate in what it stands for, but its original name was: Personal Home Page. This name came from its creator Rasmus Lerdorf, and the purpose for this creation was to record his web traffic.

Now days, this is the most commonly used scripting language for a lot of websites out there on the net, even Facebook uses it.

Let’s get on with it.

So you want to learn the powerful language of PHP, well on my opinion, you are on the right track.

First things first.

In order to run .php files, you have 2 options.

The first option is to make an investment, and rent a webhosting service. Here are a few recommendations, in which I think are the best web hostings. Check the post here. The web hosting you choose should have PHP installed in it.

The second option is to download an apache server that comes with PHP and MySQL. The one that I use is XAMPP. You can find it at Apache Friends. Follow the instructions on how to install this into your computer. Once this is installed, you will need to run the control panel, and start the services for apache server, and MySQL.

Now let’s get to the fun part.

You are here because you want to learn some basics of PHP. Assuming you already know basic HTML, then let’s get on with it.

PHP is a scripting language that is interpreted within the web server. When you write PHP code, the server will see what you are requesting, translate it to HTML, and the ending result is displayed as HTML for the users to see. Open up your favorite PHP text editor and create a new file. All PHP files will have an extension of .php. PHP code is written within the following blocks:

Let’s create your first PHP page

If you notice the above code, there is a semicolon at the end of the statement. This semicolon indicates the termination of the current statement being interpreted. Each statement should be closed by this, or you will just see an error coming back from the server.


You as a developer can create your own variables within PHP. One of the things I like from PHP is that, there is no need to declare a type for a variable before using them. Variables are created when they are assigned a value. How do you assing a value to a variable, take a look at the following example :

The variable $quantity was assigned 0, thus making it a data type – int. As for $name, it was assigned “John” to it, making this variable, of type string. Check out the Data Types for PHP

Integer – Used for whole numbers

Float – used for real numbers

String – used far a set of characters

Boolean – indicates true or false

Array – Used to store multiple items

Object – used for storing instances of classes (This is a bit more advanced)

You can declare a PHP variable with the types mentioned above.


As we have seen previously, variables may and will vary their value. Sometimes we will need to define what we call a constant. Constants are defined as follows:

You would use a default function from PHP – define, sending 2 parameters. The first being the name of the constant, and the second being the value of it. Usually constants are defined in CAPITAL letters, so that way, when reading the code you know which a constant is.

Let’s take a look at arithmetic Operators

These operators you can do basic math operations. These are:

Addition +

Subtraction –

Multiply *

Division /

Modulus %

To use them would be:

$total will be the sum of $price1 and $price2. When using modulus (%) the result will be the remainder of a division. Usually, these arithmetic operators are a used with numbers (integer or float data types). If in any case it is used with a string, PHP will try to convert from string to a number. If the “+” is used between 2 strings, and the string can’t be converted to number, then PHP will just concatenate them forming one full string of both.

Comparison Operators

These operators are used to compare values within the scripts. These are basically used to determine what path the process will take. Let’s take a look:

    • Equals ==
    • Not Equal !=
    • Less than <
    • Greater than >
    • Less than or equal to <=
    • Greater than or equal to >=

Control Structure (How to use the above operators)

The above operators are here for a reason. In some cases you need to execute a certain set of code, and in other cases, you do not. In order to control this process, you would need to make a decision. These decisions are taken in a few possible ways. Let’s take a look at the most commonly used decision maker in PHP.

The IF – ELSE statements

I f you wanted to let a user know, that you have a certain product in stock, you would first check the quantity for this same one. Quick example:

Note that after the conditions statements (if – else), there are curly braces. These braces indicate a block of code. In the example above, we have quantity set to 10. Since it is grater than 0, then the code to process will be the one after the “if”, before the “else”, stating that there are items available in stock. Otherwise, if we were to set quantity to 0, then the block of code to be executed would be after the “else” statement.

Another conditional statement would be the famous switch. This statement works just like an if-else statement, in that it executes only the block of you that you specify. Let’s ask the user, what category do they want to look in to:

The form:

For the php script, we can name it: process.php

Not that, if a category was not picked, then there is a default response. In this case, it just tells the user that no category was chosen. Also, as you might have noticed, you are able to achieve this same result by using if-elseif, just like this:

There has been a debate between people of which method to use. I would say you could use either of them, as tests have shown for these two processes to be almost as equally effective. In speed factors, well some tests that I have seen around indicate that the “IF-ELSEIF” seems to be a bit faster than the “SWITCH”. Other tests demonstrate the opposite. It is also said that, “SWITCH” is better off when there are a lot of conditions to be compared. I guess it’s all a matter of preference.

Let’s put a close for this for now. I will have more simple tutorials for PHP in the near future.

Hope you all enjoy.

8 thoughts on “Learn basics for PHP

  1. Awesome explanation, neat and presice. I am looking foward to know how to actually print a form and have also the option to email it. I have looked into some sites but they are confuising. I am sure you can clarify this doubt with ease.

  2. @Dude: Thanks for stopping by, really appreciate your support. I’ll create a new post continuing on this basic tutorial, that way, you will get a better understanding on how to display the information to the user. So stay tuned.

  3. Nice work Jorge, this is very helpful for those beginners who are interested in learning PHP. Keep up the good work!!!

  4. Thank you for this site, i will browse this everyday, and bookmark it. I got an intern on this company, and they use PHP and MYSQL, im coming from a GUI Design with java, do i code there, it different here. even with just a login logout form with mysqli im having problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.