Introduction to PHP Programming
By PHP Catalyst2007-11-19
In PHP a variable can hold data of any type. These data types are as following:
- Integer - An integer is a non-fractional, whole number such as 1, 2, 3, 4, 88, 1000, 1374747. The range of integer is operating system specific.
- Real Number - It is also known as a floating number or floating point number. It is not a whole number and has fractions such as 1.22, 2.45, 100.765 etc.
- String - Also called character string, it consists of a series of characters such as "php".
- Boolean - A boolean is a true or a false value.
Assigning data types
Unlike other programming languages, PHP does not require formal declaration of the data type of a variable. When you assign certain value to a variable, PHP will automatically try to guess the type of data being stored and assign the data type to the variable. When needed PHP will automatically convert the data type.
<?
$a = 5; //Storing an integer
$b = 5.5 //Storing a floating number
$c = $a + $b; // $a is integer and $b is floating number
?>
Here data type of $a is automatically converted to a real number and then added with the value of $b which is a real number too.
Type Casting
Sometimes, there might be a need to change the data type of a certain variable from string to an integer and then back to a string. Such manual over riding of data types is called Type Casting. Since PHP will automatically change the data type, chances are rare that you will need to use type casting at all. To specify a different of a particular data type, use the following:
<?
$a = (int) $b; //changing to a variable
$a = (string) $b; //changing to a string
$a = (float) $b; //changing to a real number
?>
Determining Data Type
To find out the data type of a variable, use a statement like the following:
<?
$a = 100; // storing an integer
var_dump($a);
//This prints the value stored in the variable $a ?>
This will output the data type and value of the variable. In this case, the output will be int(100)
|
|||||||||
You might also want to check these out:
|
Link to This Tutorial Page!

