Monday, 11 March 2013
Learn Php programming Data Types
What Are Data Types ?
There are different types of data :
- Numeric Data (0-9)
- Alpha-Numeric Data (0-9,A-Z, a-z)
So because of the different types of database, when we are declaring a variable then we also have to define the data type of the variable , Variable gets space in memory according to the data type defined with it. for example in C++ we make and define a variable like this :
int a = 5;
Here you can see in above example that first we have given a datatype to the variable . Here variable " a " is of integer data type and an integer can be stored in this variable. Now because of this integer declaration to the variable " a" we can not assign a character to variable " a " . If we assign a character to an integer variable , we will get an error.
Also Check :
Also Check :
How Php Deals With Undefined Variables ?
PHP is a simple and loosely typed language. It means that whenever you declare a variable , PHP will define a data type automatically for the variable and you will not have to face any data type mismatch error .
Data Types :
Integer
Description : Whole number
Double
Description : floating point number or numbers with decimal point
String
Description : Collection of characters
Description : special value that can be either true or false
Object
PHP data types |
Array
Description : collection of variables
Null
PHP Program To Explain Variables :
Also Check :
Source Code |
<?php
// declare without assigning
$test_var ;
echo "The data type of test_var_ variable is : ".gettype( $test_var );
echo" <br/> ";
$test_var = 5 ; // integer
echo "The data type of test_var_ variable is now : ".gettype( $test_var );
echo" <br/> ";
$test_var = "Pakistan"; // string
echo "and now the data type of test_var_ variable is : ".gettype( $test_var );
echo" <br/> ";
$test_var = 9.33; // double
echo "and now the data type of test_var_ variable is : ".gettype( $test_var );
echo" <br/> ";
$test_var = true; // Boolean
echo "and now the data type of test_var_ variable is : ".gettype( $test_var );
echo" <br/> ";
?>
Output :
The data type of test_var variable is : NULL
The data type of test_var_ variable is now : integer
and now the data type of test_var_ variable is : string
and now the data type of test_var_ variable is : double
and now the data type of test_var_ variable is : Boolean
You can see in the above script that we have declared a variable $test_var and used gettype function to get the data type of the variable. In the next lines of the code , we have assigned different data to the variable and each time we assign a data to the variable , data type of the data is find using the the get type function. The php engine checks the code from top lines to bottom and by processing each line it displays the output.
In the above program we have changed the data type of the single variable at run time and php engines decides the data type at run time. In the same way we can set the data type of variable at run time using settype function .
Understand and see the below program to find how settype function works.
<?php
$myvar = 100 ;
echo " Data type of myvar is : ".gettype ($myvar)." <br/>" ;
settype($myvar , "string" );
echo " The variable is now a : ".gettype($myvar);
?>
Source Code Program |
Here we have declared a variable named myvar and assigned a value of 100 to it . By assigning 100 to it , myvar becomes a integer variable . In the second line of the code we have printed the data type of he variable. By using settype function we have changed the data type of myvar variable from integer to string. At the end of the code we have printed again the data type of variable after changing the data type. Settype function takes two parameters. First parameter is the name of the variable and the second parameter is the new data type that we want to assign to the variable.
Settype(name of variable , " New Data Type ");
Php is a language full of functions We will explain another way to change the data type of the variable in the next tutorial.
" Sharing Is Caring "
Stay Blessed
Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →
Related Posts:
PHP PHP Data types Tricks and Tutorials Web Development
Subscribe to:
Post Comments (Atom)
0 comments: