Working with PHP Datatypes
By Steve Adcock2005-05-14
Let's play "What's that datatype?" !
PHP includes a real easy function to display the type of a variable, not the actual value, called gettype(). You can use gettype() like this:
<?Notice our use of the period to separate the gettype function by the HTML code <br>. The convenience of functions is the fact any variable can be passed into the function, from within the parenthesis, to be processed. So, by simply changing the variable we call into gettype(), we can check the datatype of that specific variable.
$i = 10;
$j = 10.3;
$k = "Broncos";
echo(gettype($i) . "<br>"); // Displays integer
echo(gettype($j) . "<br>"); // Displays double
echo(gettype($k) . "<br>"); // Displays string
?>
You can also set the datatype of a variable after it has been created. The following example first defines a double, and I will use PHP's settype() function to redefine it as an integer.
<?As you can see, we are actually passing in two different types of information into the settype() function, the variable and the variable type we want to change it to. Keep in mind that if you change a string to an integer, the value of that variable will equal 0, instead of the string of characters it initially was defined to hold.
$k = 12.1;
echo("k = " . gettype($k) . "<br>"); // Displays k = double
settype($k, "integer"); // Changes the datatype of variable k
echo("k now = " . gettype($k)); // Displays k now = integer
?>
Tutorial Pages:
» Working with PHP Datatypes
» Finding your PHP info
» Defining variables and constants
» Let's play "What's that datatype?" !
» If this, If that
