Contents
The PHP is often referred to as “loosely typed” or “dynaically typed”. What this means is that variable types are detirmined by context. Lets jump straight into some code to see it in action.
<?php
/*** create a number as a string ***/
$var = "6";
/*** display the variable type and value ***/
var_dump($var);
echo '<br />';
/*** increment the variable ***/
$var++;
/*** display the variable type and value ***/
var_dump($var);
?>
The above code will produce the following output:
int(7)
We began with a string value of 6 and then incremented the variable using the ++ operand. PHP has noted the context of the operation and internally converted the type to INT.
Types
PHP supports many different types, these include
- int
- string
- binary
- unicode
- array
- boolean
- bool (same as boolean)
- object
- float
- double (same as float)
- real (same as float)
Casting
The ability of PHP to be able to dynamically type variables makes for quick and lean code. But for those times where we absolutely need the variable to be of a particular type, the variable must be cast. This is done in much the same way as in C.
Cast To String
<?php
/*** create an int ***/
$num = 6;
/*** dump the type ***/
var_dump($num);
echo '<br />';
/*** cast to string ***/
$num = (string) $num;
/*** dump again ***/
var_dump($num);
?>
The above script shows us the variable begins as an integer and then is cast to a string.
string(1) “6”
Cast To Int
Just as PHP allows casting to string, the same applies when casting to an integer
<?php
/*** create an string ***/
$string = "26";
/*** dump the type ***/
var_dump($string);
echo '<br />';
/*** cast to int ***/
$string = (int) $string;
/*** dump again ***/
var_dump($string);
?>
The above script tells that the string is created and then converted to an int.
int(26)
Try changing the value of the value of the sting to a non-numerical value such as “this is a string”. The result, after casting, would then be int(0).
Cast To Binary
Cast To Unicode
Cast To Boolean
<?php
/*** create an int ***/
$num = 1;
/*** dump the type ***/
var_dump($num);
echo '<br />';
/*** cast to bool ***/
$bool = (bool) $num;
/*** dump again ***/
var_dump($bool);
?>
The above script will output the following:
bool(true)
The script ouputs bool(true) because “1” is considered to be a true boolean value. Other values that evaluate to boolean true are:
- -1
- “string”
- 1.6e5
So, basically, any non zero value will evalute to boolean true. Values that evaluate to boolean false are:
- 0
- “0”
- “”
- FALSE
- array()
- NULL
Cast To Object
This is quite groovy. Casting an array to an object allows the use of the array as an object, using the corresponding keys and values.
<?php
/*** create an array ***/
$array = array('animal'=>'koala', 'name'=>'bruce', 'type'=>'marsupial');
/*** cast to an object ***/
$object = (object)$array;
/*** use the array as an object ***/
echo $object->name;
?>
If any other type, such as a sting or integer is cast to an object, PHP creates an instance of stdClass and the value is contained within the class member scalar as shown here:
<?php
/*** create an array ***/
$string = 'this is a string';
/*** cast to an object ***/
$object = (object)$string;
/*** output the value ***/
echo $object->scalar;
?>
The above string outputs “this is a string” as the string value has been assigned internally by PHP to the special class member “scalar”.
Cast To Float
Floats are also known as double or real values and casting is generally considered to be a bad idea with PHP as the precision needed with floats is not readily available as PHP has a finite amount of numbers. Better results can be had with the bc functions.
Check for Type
Up to this point we have dealt with converting or casting types. But there will be times when we need to check what type of variable a script is using. PHP comes with several ways to achieve this with the is_* functions and the charater type, or ctype_* functions.
Check String
In PHP most variables are strings to begin with, so this is rather straight forward.
<?php
/*** a simple string ***/
$string = 'This is a string';
/*** check if value is a string ***/
if(is_string($string))
{
echo $string;
}
?>
Check INT
PHP has several ways of checking if the value of a variable is an integer, using the is_int() function is the preferred method. The is_interger() and is_long functions are both an alias of is_int().
<?php
/*** a simple int ***/
$int = 1234;
/*** check if value is a int ***/
if(is_int($int))
{
echo $int;
}
?>
Of course, we could use the ctype_digit() function to check also.
<?php
/*** a simple int ***/
$int = 1234;
/*** check if value is a int ***/
if(ctype_digit($int))
{
echo $int;
}
?>
Note:
While the function is_numeric() also checks the value of a string for numbers, it should not be used as it also returns try for floats and scientific notification.
Check Array
To check if a value is or type array, simply use is_array() as we have seen earlier with other types, you should have the swing of this by now..
<?php
/*** a simple array ***/
$array = array('animal'=>'koala', 'name'=>'bruce', 'type'=>'marsupial');
/*** check if value is an array ***/
if(is_array($array))
{
/*** cast array to object ***/
$obj = (object)$array;
/*** echo the name value ***/
echo $obj->name;
}
?>
The above code outputs “bruce”. As we saw in the Cast To Object section we can cast the array to an object. In this instance we have first checked that the value is indeed an array with the is_array() function.
Check Object
No surprises here as to what function is used to check if a variable is an object.. is_object().
<?php
/*** a simple object ***/
$obj = new stdClass;
/*** check if value is an object ***/
if(is_object($obj))
{
echo 'Object found';
}
?>
Check Float
To check if a variable is a float or double or real type, the is_float() function is used. It should be noted that all values from super globals such as $_GET or $_POST are strings and to check the values of these the is_numeric() function should be used. Here we show how to check for a floating point number.
<?php
/*** use pi as a floating point number ***/
$num = pi();
/*** check if value is an float ***/
if(is_float($num))
{
echo $num;
}
?>
The above script checks the value of pi() and finds it has a value of 3.14159265359 and so, echoes the value.
Check Double
To check if a number is a double the is_double() function can be used. This function is an alias of is_float() and so the same procedure applies as seen in the section Check Float.
Check Real
To check if a number is a real type the is_real() function can be used. This function is an alias of is_float() and so the
same procedure applies as seen in the section Check Float.
Check Boolean
Checking a boolean value is quite simple, just as we have seen through-out this document the is_bool() function is used.
<?php
/*** create a boolean value ***/
$bool = is_int(0);
/*** check if value is boolean ***/
if(is_bool($bool))
{
var_dump($bool);
}
?>
In the above code we have checked if the number zero is an integer with the is_int() function. This returns boolean true on success so when checked with the is_bool() function we see that it is boolean and dumping the resulting variable shows bool(true).
Check Buffer
This bad boy checks if a variable is a native unicode or binary string. The is_buffer() function works the same as the other is_* functions
<?php
/*** create a unicode string ***/
$unicode = "û?Ìcöde";
/*** check if value is unicode ***/
if(is_buffer($unicode))
{
echo 'Unicode string';
}
?>
Check Binary
To check if variable is a native binary, use the is_binary() function.
<?php
/*** no code for this function yet ***/
?>
Check Unicode
To check if a string is a unicode string, the is_unicode() function is used as below.
<?php
/*** create a unicode string ***/
$unicode = "û?Ìcöde";
/*** check if value is unicode ***/
if(is_unicode($unicode))
{
echo 'Unicode string';
}
?>
Check NULL
To find if a variable value is NULL the is_null() function is used.
<?php
/*** create a null value ***/
$var = null;
/*** check if value is null ***/
if(is_null($var))
{
echo 'Value is null';
}
?>
Check Resource
A resource is a type returned by PHP from opened files and database connections. Using the is_resource() function a variable can be checked reliably.
<?php
/*** a database connection resource ***/
$conn = @mysql_connect('localhost', 'username', 'password');
/*** check if connection is a valid resource ***/
if (is_resource($conn))
{
/*** show the resource type ***/
echo get_resource_type($conn);
}
else
{
/*** show an error on failure ***/
echo "Connection Failed: ".mysql_error();
}
?>
Check Scalar
A scalar value is a variable value of type boolean, string, integer or float. All other types are considered non-scalar values. To check for a scalar value use the is_scalar() function.
<?php
/*** a string ***/
$string ="5.2";
/*** check if value is scalar ***/
if (is_scalar($string))
{
echo $string.' is scalar';
}
?>