Star Hoster
Photoshop Tutorials
Anonymous Web Surfing
Funny Jokes
Myspace Friends
Open Treasure Chest
Building in Paradise


November 30, 2005

PHP Data Types

Filed under: Intro PHP Tutorials — phpdeveloper @ 4:52 am

Data Types in PHP

In PHP the data type of a variable is not set by the programmer. PHP decides the data type of variables after interpreting the web page. Data Types in PHP include:

Numeric data type
String data type
Array data type
Numeric data type

Numeric data type is used for number.

There are two different numeric data types:

Integer
Double

Integer Data Type

Integer data types contain whole number values.

Example:

$mark = 50;

Double

Double contains floating point numbers.

Example:

$price = 12.75;

String Data Type

String data type is used to contain textual information or letters. The value is assigned within quotes as shown below.

Example:

$strname = “Uday”;
$strId = “phpprogrammer”;
$StrInt = “2.6″;

If you declare an integer as a string, string operations will be preformed and not integer operations. Trying to do integer operations on a variable declared as a string will cause some unexpected results.

String Concatenation

String Concatenation is a process of adding two strings. This is done by attaching one string to the end of another string. This is done by using ‘.’ (period) operator as shown below:

Example:

$strname = “Uday”;

$strspace = “-”;

$strlast = “Parekh”;

$strname = $strname.$strspace.$strlast;

echo $strname;

Output:

Uday Parekh

Array Data Type

Array is used to store same type of data at continuous memory locations.

Array data type is used to contain many values in a single variable. Each array element can be retrieved by using the array variable name and its key/index value. We can declare an array variable in different ways as shown below:

Example:

$mark[0] = 10;

$mark[1] = 14;

$mark[3] = 18;

(Or)

$mark = array(10,14,18);

In the above example, the variable ‘mark’ contains three different values. In the first method the array elements are stored using the key values (0, 1, 2), but in the second example the keyword ‘array’ is used to assign values to the array variable. Remember that array key element starts with 0.

The array elements can be accessed by using their key values. The different marks stored in the array elements can be added together to obtain the total marks as shown below:

Example:

$total = $mark[0] + $mark[1] + $mark[2];

echo $total;

Output: 42

In the above two examples we have used the key values as numbers, but it not necessary that the key value must be a number, it can also be a string as shown below:

Example:

$mark[’physics’] = 10;

$mark[’chemistry’] = 14;

$mark[’biology’] = 18;

$total = $mark[’physics’] + $mark[’chemistry’]+$mark[’biology’];

echo $total;

Output: 42

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.