Tuesday, 12 March 2013
Convert Data Type By Type Casting In Php
What is Data Type Conversion And Type Casting In Php ?
If we want to change a data type of a variable then there is a way that just write the data type in round brackets and write it before the variable. In this way the data type of the variable will be changed to the data type written in brackets.
Difference Between Type Casting And Using Settype Function :
The main difference between Type Casting and Settype function is that in type casting the original data type of the variable does not changes but a new variable copy with a new data type will be created. If you are getting confused then observer the program below to understand type casting
<?php
$undecided = 3.14 ;/* this variable is only declared and it has been assigned a data type*/
$holder ; /* this variable is only declared and it has not been assigned a data type yet */
$holder = (double) $ undecided;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage
*/
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable*/
$holder = ( string ) $undecided ;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage */
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable */
$holder = ( integer ) $undecided ;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage */
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable */
$holder = ( boolean ) $undecided ;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage */
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable */
$holder = ( double ) $undecided ;
print gettype( $holder ) ; /* This will print the data type of holder variable at this stage */
print " -- $ holder <br/> " ; /* This will print the value asigned to holder variable */
?>
Output :
double -- 3.14
string -- 3.14
integer -- 3
boolean -- 1
double -- 3.14
In the above script we have made a variable $undecided and the given a floating point value to it. Then with type casting we have made of copy of $undecided variable with different data type and then placed the copy in $holder variable . In this way the data type of $undecided variable remains same and we can just use this variable to make its copies with different data types and then assigning it to others. This is helpful when we want some specific data types like string or integer.
For example If you ask user to enter his/her age in text box and users enters 25 years in the textbox. User has entered a string But Age is usually in integer. So now you have to type cast the string entered by the user. This will be done as follows :
For example If you ask user to enter his/her age in text box and users enters 25 years in the textbox. User has entered a string But Age is usually in integer. So now you have to type cast the string entered by the user. This will be done as follows :
$age = (integer) $ user_age ;
print $age;
The output of above statement will be
Output :27
Other Ways For Data Type Conversion :
Ofcourse There are always many other ways to perdorm a single task. In the same way there is another way to change the data type of given variable without using Type casting. We will discuss those ways in our next Tutorial.
Stay tuned and subscribed for learning Complete Web development. Share It with others. Ask Questions And Queries Via Comments. You feedback will be welcomed. Thanks
" Share It Please . Thanks!!!"
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 Programming Tricks and Tutorials Web Development
Subscribe to:
Post Comments (Atom)
0 comments: