Thursday, September 2, 2010

Php Variables Tutorial.


Written by raver, owner of the Romanian Php Portal http://phpzone.frih.net
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:

<?php
//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;
$_a;
$ variable 1;
$_1984;
$o_longer_name_of_a_variable;

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

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 ( . ).

<?php
$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:

<?php
$variable = 1;
$variable++;
echo $variable; //will print 2
$variable--;
echo $variable; //will print 1;
?>

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

<?php
$number = 1;
For $number <10
{
echo $number;
$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 http://phpzone.frih.net (Romanian php portal)

--
http://www.co5.in/

0 comments:

Post a Comment

 

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