Working with PHP Datatypes
By Steve Adcock2005-05-14
If this, If that
Let's make some decisions here. PHP's if statements can be used to check the validity of data, integrity or even the presence of data within a variable. Let's use the gettype() function in our first example to check the datatype of a variable.
<?Since 10 is an integer, and our if statement checks to see if the variable is an integer, the "Alright, the variable i is an integer" string will be displayed. Try changing 10 to 10.1. Since 10.1 is a double instead of an integer, the second statement, "No, sorry, variable i is not an integer" will be displayed.
$i = 10;
if(gettype($i) == "integer") // Checks if variable i is an integer
{
echo("Alright, the variable i is an integer"); // If integer, then display
} else {
echo("No, sorry, variable i is not an integer"); // If not integer, then display
}
?>
Two more functions, isset() and unset() are also useful PHP utilities for working with datatypes. isset() checks to see if a variable has been given a value. unset() will destroy the variable, even from memory.
<?Since variable i does have a value, "Variable i has a value" will be printed. If you delete the $i = 10; line, the second echo statement, "Sorry, variable i has no value" will execute. Let's move on to unset, to remove the variable for memory.
$i = 10;
if(isset($i)) // Checks if variable i has a value
{
echo("Variable i has a value"); // If defined, then display
} else {
echo("Sorry, variable i has no value"); // If not integer, then display
}
?>
<?In the first part of the coding, we defined a variable, i. Then, we used the unset() function and passed $i into it as an argument, which therefore destroyed the variable. Just to make sure that we did, in fact, destroy the variable, I used the isset() function we just looked at above to check for a value. Since there should not be a value, no echo statement should be printed and you should be left with a blank page, if nothing else is within your document, of course.
$i = 10;
unset($i); // Destroys variable i
if(isset($i)) // Checks if variable i has a value
{
echo("This should NOT display"); // If not destroyed, then display
}
?>
We can also perform an action if, and only if, a particular variable is a specific datatype, by using the is_double(), or is_string(), or is_array(), or is_integer, etc functions relative to the respective datatype you are checking for. Let's look at an example.
<?Since 10 is an integer, the first if statement will be true and "This is an integer" will be displayed. Try adding quotes around the 10, then the string echo value would display, or try adding a .1 after the 10, then it's a double.
$i = 10;
if (is_integer($i))
{
echo("This is an integer");
if (is_string($i))
{
echo("This is a string");
if (is_double($i))
{
echo("This is a double");
}
?>
Here you have a basic introduction into datatypes and how to manipulate them using the PHP server-side language. A complete mastery of PHP ultimately begins at datatypes, so congratulations on passing the first step. A continuing study of this language can render you an expert in no time. Good luck.
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
