Thursday, September 2, 2010

Php Variables Tutorial.

Written by raver, owner of the Romanian Php Portal
A) What exactly is a variable?
To better understand what exactly is a variable let's use a real world
example. Let's assume that on a Friday evening your girlfriend calls you
to set a date at a certain hour, let's say 19:00. You agree, but after
half an hour she calls again telling you that she cannot make it because she
has to talk about you with her friends and it would be better to meet at 20:00.
After you hang up another friend calls and ask you when you will meet your girlfriend.
You say: "At 20:00". Let's see how this can be interpreted
in PHP code:

//first call
$hour = "19:00";
//second call
$hour = "20.00";
//when are you going to meet your girlfriend?
echo "Ill meet my girlfriend at ".$hour;

This is a more practical (although weird) way of representing a variable. You
can think of a variable like a piece of memory in which you can store data.
Any kind of data (almost).

Here are a few things worth remembering when using variables:
a) Every variable must start with the dollar ( $ ) sign. This indicates to the
PHP engine that what goes after it will be a variable.
b) When naming variables you must always use alpha-numerical characters. Here
are a few examples:
$ variable 1;

c)variables are case-sensitive. So $variable is different than $Variable or

B)Variable Types
I`ve mentioned earlier that variables can hold all kinds of data. Let`s see
exactly what types it can hold.

a) String
String variables can hold words and sentences. Let`s see a quick example in
which you will also learn about the concatenate operator ( . ).

$string = "Hi there, I am a string";
$string2 = " inside a string";
Echo $string.$string2;

This will display:
Hi there, I am a string inside a string. Using the dot ( . ) operator you can
merge in one sentence 2 or more variables.

c) Integer
If you finished grade school than you must know that integer means whole numbers.
So we have:
$integer = 5;
$integer2 = 25;
//You can also perform basic mathematic operations with them:
$sum = $integer + $integer2; //will hold 30
$withdraw = $integer2 - $integer; //will hold 20
$multiply = $integer * $integer2; //will hold 125
$divide = $integer2 / $integer; //will hold 5

d) Double
A double refers to numbers with a decimal point.
$double = 25.4;

e) Boolean
Variables of this type can only hold two types of data, true and false, best
used in conditional sentences .

f) Array
Variables of type Array can of course, hold arrays.

Incrementing and decrementing
If you are new to programming the terms may sound a little weird, but trust
me, it is very simple. Many times you will have to increase or decrease a variable
by 1. Incrementing means adding 1 to a variable, and decrementing decreasing
by one. Ex:

$variable = 1;
echo $variable; //will print 2
echo $variable; //will print 1;

This is very useful in loops and searching through an array. Here is another

$number = 1;
For $number <10
echo $number;


Running this will print the numbers 1 – 10.

Well this is all for the moment, I hope you liked this tutorial. For more tutorials
and scripts visit (Romanian php portal)



Post a Comment


Complete Online Solution | Make the internet world into your hands Copyright © 2009 Community is Designed by CO5 | Web designing | Web hosting