Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Working with PHP Datatypes

By Steve Adcock
2005-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:

<?


$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

?>
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.

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.

<?


$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

?>
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.

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


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.