Getting Started with PHP

Learn some basic php with this nice and simple article. I shall go through some basic things every now and then and go through some programming know how.

Why learn php?

Why should you learn php? well to be honest it could make you   of money i would say. Websites now a days (good ones anyway) use PHP. Along with MySQL. Which i will go through in basic form. a lot of the big websites you see on the net use PHP. For instance Facebook uses PHP and MySpace also. If you learn PHP you might be able to make some nice Facebook applications aswell.

Getting Started

First things first lets get the basics out the way. We need to know the open tags for PHP which are very simple of course.

< ? php

?>

There we go the opening tags for PHP. You may be wondering how we use them. Well lets get started by opening notepad or something like that and start a new file. Then type the code above which is the opening tag for PHP. You can also use for the open tags (Just remove the PHP from the start tag).

Now we can start programming in PHP and remember all your code will be in between these tags.

Displaying text

We can display text in PHP very easy by using the function “echo”. Echo does exactly what the name suggests it “echos” the text onto the page.

echo “Dale”;

The code above will display the word Dale. It will “echo ” anything within the quotation. You can use either ” ” or  ‘ ‘ quotes and it will work, but later on you will release they can make a difference.

Variables

We can use things called “Variables” to store data from information from on line forms submitted. They sometimes can be called differently depending on the language being learnt, but hey for now just think of them as a way of storing information very important to the script. (Maybe not very depends).

Variables are defined in PHP using $ signs. Then followed by the name of the variable.

$name = “Dale”;

We can store some information in the variable using the = sign. We have stored the String Dale in the variable called name.

How can we use variables

We can use variables in many ways. They can be used to store data from forms and then passed into a database. They can also be used to display information on users who maybe logged into a website (like facebook).

Storing information from forms tends to be a popular use for variables, but not always. Depends on how you program and how easy you like your code.

We can display the information stored inside variables using the “echo” function also.

echo $name;

The code above will then display the content of the variable called name. We are just assuming that the variable has already been defined somewhere.

$name = “Dale”;

echo $name;

We have now defined a variable called name and then displayed the content of the variable.

Maths

We can also do some maths in PHP using variables or without. Sometimes you will need to mix both methods of doing maths.

$num1 = 20;

$num2 = 30;

echo $num1 + $num2;

Now the code above will add the content of the variable num1 and the variable num2. Now because we have used to Int (Integers) that are stored in the variables. They will be added together like a maths sum. Now you can see that we have not used ” ” around the number, because this would mean that the variable would be storing a String not a Int. A String is a List of Characters. Int is a whole number. The good thing about PHP is that you don’t need to define any variable types. What i mean by this is when you declare variable in something like Java you must declare the variable.

String name = “Dale”;

With PHP you don’t need to worry about this. Please remember you will need to learn about type declaration when learning other scripting or programming languages.

Feel free to test all the maths signs out and see how they work. I will write down all the possible ones and explain what they do.

/  =  Divide
* =  Times
-  = Subtract
% = Modulus (This will devide two numbers and display the remainder e.g. 2 % 2 = 0, 3 % 2 = 1).
+  = Plus

Leave Your Response