Friday, December 02, 2005

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)

5 Comments:

Blogger Austin said...

Thanks a lot!

When learning php this tutorial and Bright-Tutorials were helpful to me.

9:35 PM  
Blogger Learnphp said...

To learn PHP with advanced concepts, you can visit Advanced PHP tutorial

3:22 AM  
Blogger Unknown said...

Nice post to learn Programming in PHP, JAVA and others. if want to learn something advance about server technology for developers then visit Sharepoint tutorial For Developers.

12:01 AM  
Blogger basha said...

Excellent blog I visit this blog it's really awesome. The important thing is that in this blog content written clearly and understandable. The content of information is very informative.
Oracle Fusion HCM Online Training
Oracle Fusion SCM Online Training
Oracle Fusion Financials Online Training
Big Data and Hadoop Training In Hyderabad
oracle fusion financials classroom training
Workday HCM Online Training
Oracle Fusion HCM Classroom Training
Workday HCM Online Training

4:46 AM  
Blogger Sarika A said...

Very interesting blog Awesome post. Your article is really informative and helpful for me. We are also providing the best services click on below links to visit our website.

Oracle Fusion HCM Training
Workday Training
Okta Training
Palo Alto Training
Adobe Analytics Training

11:29 PM  

Post a Comment

<< Home