# Data Types, Constants, and Variables PHP

These functions check the status of a variable, change its type, or return a value as a particular data type.

boolean define(string name, value, boolean non_case_sensitive)
The define function creates a constant, which is essentially a variable that may be set only once. The value argument may be a string, integer, double, or boolean. It may not be an array or object. The non_case_sensitive argument is optional. By default, constants are case sensitive, which is the same as with variables. If the constant cannot be created for some reason, FALSE will be returned. If you wish to check that a constant is defined, use the defined function. It is customary to name constants using all uppercase letters, as is the practice in C. This makes them stand out among other identifiers.

Because PHP allows for unquoted string literals, it is possible to write code that uses constants that do not exist, yet produces no error. When you are using constants to hold strings to be displayed on the page, this is simply an annoyance, because you can see the error right away. When used for values not displayed, it can be a frustrating source of bugs. If you discover a constant mysteriously evaluating to zero, check that the constant has been defined.

<?
/*
** Database variables
*/
define("DATABASE_HOST", "localhost");
define("DATABASE_USER", "httpd");
print("Connecting to " . DATABASE_HOST . "<BR> ");
?>

boolean defined(string constantname)
The defined function returns TRUE if a constant exists, FALSE otherwise.

<?
define("THERMOSTAT","72 degrees");
if(defined("THERMOSTAT"))
{
print("THERMOSTAT is " . THERMOSTAT);
}
?>

double doubleval(expression)
The doubleval function returns its argument as a double. discusses converting between data types. Related functions are strval and intval. It is an error to pass an array or object to doubleval.

<?
$myNumber = "13.1cm"; print(doubleval($myNumber));
?>

empty
This function is an alias for isset.
string gettype(expression)
The gettype function returns a string that describes the type of the variable or expression. It will be one of the following values: array, class, double, integer,
object, resource, string, unknown type.

<?
//integer
printf("%s <BR> ", gettype(11));
//double
printf("%s <BR> ", gettype(7.3));
//string
printf("%s <BR> ", gettype("hello"));
?>

integer intval(expression, integer base)
The intval function returns its argument as an integer. The optional base argument instructs intval to use a numerical base other than ten.

<?
//drop extraneous stuff after decimal point
print(intval("13.5cm") . "<BR> ");
//convert from hex
print(intval("EE", 16));
?>

boolean is_array(expression)
The is_array function returns TRUE if the expression is an array, otherwise FALSE is
returned.

<?
$colors = array("red", "blue", "green"); if(is_array($colors))
{
print("colors is an array");
}
?>

boolean is_bool(expression)
Use is_bool to test whether an expression is a boolean.

boolean is_double(expression)
The is_double function returns TRUE if the expression is a double, FALSE otherwise.

<?
$Temperature = 15.23; if(is_double($Temperature))
{
print("Temperature is a double");
}
?>

is_float
The is_float function is an alias for the is_double function.

is_int
The is_int function is an alias for the is_integer function.

boolean is_integer(expression)
The is_integer function returns TRUE if the expression is an integer, FALSE otherwise.

<?
$PageCount = 2234; if(is_integer($PageCount))
{
print("$PageCount is an integer"); } ?> is_long The is_long function is an alias for the is_integer function. boolean is_object(expression) The is_object function returns TRUE if the expression is an object, FALSE otherwise. <? class widget { var$name;
var $length; }$thing = new widget;
if(is_object($thing)) { print("thing is an object"); } ?> boolean is_real(expression) The is_real function is an alias for the is_double function. boolean is_resource(variable) This function returns TRUE if the given variable is a resource. A resource is an integer used to identify a system resource. An example is the return value of fopen. boolean is_string(expression) The is_string function returns TRUE if the expression is a string, FALSE otherwise. <?$Greeting = "Hello";
if(is_string($Greeting)) { print("Greeting is a string"); } ?> boolean isset(variable) The isset function returns TRUE if the variable has been given a value, or FALSE if the variable has never been on the left side of a set operator. In other words, it tests that the variable has been set with a value. <? if(isset($Name))
{
print("Your Name is $Name"); } else { print("I don't know your name"); } ?> boolean settype(variable, string type) The settype function changes the type of a variable. The type is written as a string and may be one of the following: array, double, integer, object, string. If the type could not be set, FALSE is returned. <?$myValue = 123.45;
settype($myValue, "integer"); print($myValue);
?>

string strval(expression)
The strval function returns its argument as a string.

<?
$myNumber = 13; print(strval($myNumber));
?>

unset(variable)
The unset function destroys a variable, causing all memory associated with the variable to be freed.

<?
$list[0] = "milk";$list[1] = "eggs";
$list[2] = "sugar"; unset($list);
if(!isset($list)) { print("list has been cleared and has "); print(count($list));
print(" elements");
}
?>