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 =
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