Variable Type and Type Conversion in PHP
When you declare a variable in PHP, you do not specify its type. Its type depends on the value it currently holds. For example, the following variable has been assigned a string value. So it's type is currently string
, as we verify by using the gettype
function:
$val = 'Hello there!'; // assign string
echo gettype($val); // string
You can change a variable's type by assigning a new value of a different type. We assign an integer to the variable declared above and use the gettype
function again to see the result:
$val = 21; // now assign integer
echo gettype($val); // integer
Now the variable $val
is of type integer
simply because it now holds an integer value.
Automatic Type Conversion
A variable's type may vary depending upon the context in which it is used. Some functions and operators may expect a value of a particular type and automatically convert to that type, or try to. For example, echo
and print
expect strings. A conditional expression expects a boolean.
An attempt to convert may result in an error message when a particular type cannot be successfully converted to the expected type. Let's see what happens when we attempt to use echo
to display a variable that currently holds an array value:
$val = array('one', 'two', 'three');
echo $val;
// Notice: Array to string conversion in [file] on line [line number]
// Array
The echo
function is not able to successfully convert an array to a string. All it can do is issue a notice and then display Array
. However, you can display the contents of arrays using the var_dump function.
Let's explore some other examples of automatic type conversion.
Number to String Conversion
The concatenation operator expects its arguments to be strings. Let's see what happens when we incorporate a numeric value as we build a string using the concatenation operator:
$val = 20; // integer
var_dump('I will be ready in ' . $val . ' minutes.');
// string(30) "I will be ready in 20 minutes."
The numeric value is seamlessly incorporated into the string.
Boolean to String Conversion
In boolean to string conversion, true
is converted to '1'
, while false
is converted to an empty string. The following verifies this by concatenating boolean values into a string:
var_dump('How many? ' . true); // string(11) "How many? 1"
var_dump('How many? ' . false); // string(10) "How many? "
Notice that false
does not add to the length of the string since it is converted to an empty string.
Comparing Strings with Numbers
When you compare a string with a number using operators, the string is converted to a number.[1] Consider the following example:
// compare number to string that begins with number
if ( 123 == '123 Go' ) {
echo 'equal';
} else {
echo 'not equal';
}
// equal
Perhaps that result seems surprising. The two values are equal (not identical) [2] because when PHP converts a string to a number, the starting portion of the string is used if it is numeric. If not, the string converts to 0
.[3]
Contrast the above result with what happens when you compare a string with a number using a string comparison function. In this case, the number is converted to a string:
// compare number with string using strcmp
echo strcmp(123, '123 Go'); // < 0
The two are not equal. The number is less than the string. Find out more in our presentation on String Comparison in PHP.
Determining a Variable's Type
Before passing a variable to a string function, you might want to determine whether it really is a string. PHP's is_string
function returns true
or false
to indicate whether the value passed to it is a string or not:
$val = '44';
if ( is_string($val) ) {
echo 'string';
} else {
echo 'not a string';
}
// string
PHP also provides functions for determining other types, including: is_int
, is_array
, is_bool
, is_float
, and is_object
.
Setting a Variable's Type and Type Casting
PHP provides a variety of methods for changing the type of a variable or temporarily changing the type of the value it holds.
The settype Function
You can change the type of a variable by using PHP's settype
function. For example, suppose you have a variable that is currently assigned a number and you would like to convert it to a string. You can do so as follows:
$val = 24; // integer
// set $val type to string
settype($val, 'string');
// check type and value of $val
var_dump($val); // string(2) "24"
The variable will retain that type unless you either change the value of the variable to another type, or use settype
again to assign another type to it. You can use the settype
function to set the following types in addition to string: integer, float, boolean, array, object, and null.
Type Conversion Functions
PHP provides several functions that can be used to convert a value to a specific type. For example, the strval
function can be used to convert a value to a string as we demonstrate here:
$val = 88; // integer
// pass $val to strval and check return value with var_dump
var_dump( strval($val) ); // string(2) "88"
// check type and value of $val
var_dump($val); // int(88)
Notice that although we can see that the strval
function has returned a string value, when we check the value of the variable again, we see it is not a string but an integer. In other words, the strval
function returns a converted value but does not change the type of the variable itself.
Related functions are available for converting to other types as well: intval
, floatval
, and boolval
(for converting to integers, floating point numbers, and booleans).
Type Casting
You can also use type casting to change the type of a variable. In order to cast a variable (or value) to another type, specify the desired type in parentheses immediately before the variable you wish to convert. The following demonstrates casting a boolean to a string:
$val = true; // boolean
// cast $val to string and check return value with var_dump
var_dump( (string)$val ); // string(1) "1"
// check type and value of $val
var_dump($val); // bool(true)
Notice that casting does not change the type of the variable. It only returns the converted value and makes no lasting change, just as the conversion functions like strval
.
In addition to (string)
, the following casts are also supported by PHP: (int)
, (bool)
, (float)
, (array)
, and (object)
.
PHP Manual Resources
See the PHP Manual for more information on converting various types to string and on type juggling and type casting.
- See the PHP Manual presentation on comparsion operators. ^
- Find out more about the equality vs. identity operators in our discussion on Comparing Strings in PHP. ^
- See the PHP Manual for more information on converting strings to numbers. ^