Sunday, 9 June 2013

TYPE CASTING AND SOME RELATED TYPE SPECIFYING FUNCTIONS IN PHP

As php is a loosely typed language, and so we don't need to explicitly specify the type of variable. Most of the times when we receive variables using the get and post methods, they are treated as strings, while we wanted them to be something else. Suppose in a form you have to fill up your age and this age is treated as string rather than a numerical value. So it becomes indispensable to forcefully cast the string type into numeric data type.
The following source code does a similar type of casting.

Source Code # 1
/*******************************************************************************/
<html>
                <head>
                                <title>Type Casting</title>
                </head>
                <body>
                                <?php
                                                $var1="212345";
                                                $var2=(int)$var1;
                                                echo "var2 = ".$var2."<br />";
                                               
                                                $var3="95 Hello World";
                                                $var4=(int)$var3;
                                                echo "var4 = ".$var4."<br />";
                                ?>
                </body>

</html>
/*******************************************************************************/
Output:-
var2 = 212345
var4 = 95

Note that in the above example the second string is not completely composed of pure numeric value, rather it also has some string component, but type casting ignores that string part and just assigns the  numerical part after casting to the specified variable.


Now let's have a look at some of the basic functions that return the type of variable and an another set of function that returns boolean value depending whether the specified argument is of a particular type or not.

Source code # 2
/********************************************************************************/
<html>
                <head>
                                <title>Type specifying functions</title>
                </head>
                <body>
                                <?php
                                                $var1=25;
                                                $var2="Hello";
                                                $var3=true;
                                                $var4=25.36;
                                               
                                                echo "type of var1 = ".gettype($var1)."<br />";
                                                echo "type of var2 = ".gettype($var2)."<br />";
                                                echo "type of var3 = ".gettype($var3)."<br />";
                                                echo "type of var4 = ".gettype($var4)."<br />";
                                               
                                                echo "is_array var1 = ".is_array($var1)."<br />";
                                                echo "is_bool var1 = ".is_bool($var1)."<br />";
                                                echo "is_float var1 = ".is_float($var1)."<br />";
                                                echo "is_int var1 = ".is_int($var1)."<br />";
                                                echo "is_null var1 = ".is_null($var1)."<br />";
                                                echo "is_numeric var1 = ".is_numeric($var1)."<br />";
                                                echo "is_string var1 = ".is_string($var1)."<br />";
                                ?>
                </body>

</html>
/*****************************************************************************/
Output:-
type of var1 = integer
type of var2 = string
type of var3 = boolean
type of var4 = double
is_array var1 =
is_bool var1 =
is_float var1 =
is_int var1 = 1
is_null var1 =
is_numeric var1 = 1
is_string var1 = 

No comments:

Post a Comment